日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring动态物业管理

發(fā)布時間:2023/12/3 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring动态物业管理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
靜態(tài)和動態(tài)屬性對于運營管理以及在生產(chǎn)級別更改系統(tǒng)行為都非常重要。 特別地,動態(tài)參數(shù)減少了服務(wù)中斷。 本文展示了如何使用Quartz在Spring Applications中管理動態(tài)屬性。 有關(guān)使用 Spring和Quartz集成提供“ 使用Spring和Quartz的多作業(yè)計劃服務(wù)”的文章。

讓我們看一下Spring中的Dynamic Property Management。

二手技術(shù):

JDK 1.6.0_31
春天3.1.1
石英1.8.5 Maven的3.0.2

步驟1:建立已完成的專案

如下創(chuàng)建一個Maven項目。 (可以使用Maven或IDE插件來創(chuàng)建它)。

步驟2:圖書館

Spring依賴項已添加到Maven的pom.xml中。

<properties><spring.version>3.1.1.RELEASE</spring.version></properties><dependencies><!-- Spring 3 dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><!-- Quartz dependency --><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>1.8.5</version></dependency><!-- Log4j dependency --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency></dependencies>

步驟3:創(chuàng)建DynamicPropertiesFile.properties

DynamicPropertiesFile涵蓋了應(yīng)用程序的動態(tài)屬性。

# This property defines message content # Possible values = Text. Default value : Welcome Message_Content = Welcome Visitor# This property defines minimum visitor count # Possible values = positive integer. Default value : 1 Minimum_Visitor_Count = 1# This property defines maximum visitor count # Possible values = positive integer. Default value : 10 Maximum_Visitor_Count = 10

步驟4:創(chuàng)建applicationContext.xml

應(yīng)用程序上下文的創(chuàng)建如下:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Beans Declaration --><!-- Core Dynamic Properties Bean Declaration --><bean id="CoreDynamicPropertiesBean" class="org.springframework.beans.factory.config.PropertiesFactoryBean" scope="prototype"><property name="location" value="classpath:DynamicPropertiesFile.properties" /></bean><!-- Dynamic Properties Map Declaration --><bean id="DynamicPropertiesMap" class="java.util.HashMap"/><!-- Dynamic Properties File Reader Task Declaration --><bean id="DynamicPropertiesFileReaderTask" class="com.otv.dynamic.properties.task.DynamicPropertiesFileReaderTask"><property name="dynamicPropertiesMap" ref="DynamicPropertiesMap"/></bean><!-- End of Beans Declaration --><!-- Scheduler Configuration --><!-- Job Detail--><bean id="DynamicPropertiesFileReaderTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="DynamicPropertiesFileReaderTask" /><property name="targetMethod" value="start" /></bean><!-- Simple Trigger --><bean id="DynamicPropertiesFileReaderTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail" ref="DynamicPropertiesFileReaderTaskJobDetail" /><property name="repeatInterval" value="60000" /><property name="startDelay" value="0" /></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="jobDetails"><list><ref bean="DynamicPropertiesFileReaderTaskJobDetail" /></list></property><property name="triggers"><list><ref bean="DynamicPropertiesFileReaderTaskTrigger" /></list></property></bean><!-- End of Scheduler Configuration --> </beans>

步驟5:建立SystemConstants類別

創(chuàng)建一個新的SystemConstants類。 此類涵蓋所有系統(tǒng)常數(shù)。

package com.otv.common;/*** System Constants** @author onlinetechvision.com* @since 26 May 2012* @version 1.0.0**/ public class SystemConstants {//Names of Dynamic Properties...public static final String DYNAMIC_PROPERTY_MESSAGE_CONTENT = "Message_Content";public static final String DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT = "Minimum_Visitor_Count";public static final String DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT = "Maximum_Visitor_Count";//Default Values of Dynamic Properties...public static final String DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE = "Welcome";public static final String DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE = "1";public static final String DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE = "10";public static final String BEAN_NAME_CORE_DYNAMIC_PROPERTIES_BEAN = "CoreDynamicPropertiesBean";public static final String APPLICATION_CONTEXT_FILE_NAME = "applicationContext.xml"; }

步驟6:創(chuàng)建DynamicPropertiesFileReaderTask類

DynamicPropertiesFileReaderTask類已創(chuàng)建。 此類由Quartz管理。 它通過每分鐘調(diào)用“ start”方法,通過DynamicPropertiesFile讀取所有動態(tài)屬性。 可以通過applicationContext.xml更改閱讀時間。

請注意,默認情況下coreDynamicPropertiesBean的作用域為Singleton 。 它必須在運行時返回動態(tài)屬性的新值,因此應(yīng)將其范圍設(shè)置為Prototype 。 否則,將無法接收新值。

package com.otv.dynamic.properties.task;import java.util.HashMap; import java.util.Properties;import org.apache.log4j.Logger; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware;import com.otv.common.SystemConstants;/*** Dynamic Properties File Reader Task** @author onlinetechvision.com* @since 26 May 2012* @version 1.0.0**/ public class DynamicPropertiesFileReaderTask implements BeanFactoryAware {private static Logger logger = Logger.getLogger(DynamicPropertiesFileReaderTask.class);private Properties coreDynamicPropertiesBean;private HashMap<String, String> dynamicPropertiesMap;private BeanFactory beanFactory;/*** Starts reading the dynamic properties**/public void start() {setCoreDynamicPropertiesBean(createCoreDynamicPropertiesBeanInstance());logger.info("**** Dynamic Properties File Reader Task is being started... ****");readConfiguration();logger.info("**** Dynamic Properties File Reader Task is stopped... ****");}/*** Reads all the dynamic properties**/private void readConfiguration() {readMessageContent();readMinimumVisitorCount();readMaximumVisitorCount();}/*** Reads Message_Content dynamic property**/private void readMessageContent() {String messageContent = getCoreDynamicPropertiesBean().getProperty(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT,SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE);if (messageContent.equals("")){getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT,SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE);logger.error(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT +" value is not found so its default value is set. Default value : " +SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE);} else {messageContent = messageContent.trim();getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT, messageContent);logger.info(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT + " : " +getDynamicPropertiesMap().get(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT));}}/*** Reads Minimum_Visitor_Count dynamic property**/private void readMinimumVisitorCount() {String minimumVisitorCount = getCoreDynamicPropertiesBean().getProperty(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT,SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE).trim();try {if (Integer.parseInt(minimumVisitorCount) > 0){getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT, minimumVisitorCount);logger.info(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT + " : " +getDynamicPropertiesMap().get(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT));} else {getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT,SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE);logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT +" value encountered. Must be greater than 0. Its default value is set. Default value : "+ SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE);}} catch (NumberFormatException nfe) {logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT +" value encountered. Must be numeric!", nfe);logger.warn(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT +" default value is set. Default value : " +SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE);}}/*** Reads Maximum_Visitor_Count dynamic property**/private void readMaximumVisitorCount() {String maximumVisitorCount = getCoreDynamicPropertiesBean().getProperty(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT,SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE).trim();try {if (Integer.parseInt(maximumVisitorCount) > 0){getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT, maximumVisitorCount);logger.info(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT + " : " +getDynamicPropertiesMap().get(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT));} else {getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT,SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE);logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT +" value encountered. Must be greater than 0. Its default value is set. Default value : " +SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE);}} catch (NumberFormatException nfe) {logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT +" value encountered. Must be numeric!", nfe);logger.warn(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT +" default value is set. Default value : " +SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE);}}/*** Gets CoreDynamicPropertiesBean** @return Properties coreDynamicPropertiesBean*/public Properties getCoreDynamicPropertiesBean() {return coreDynamicPropertiesBean;}/*** Sets CoreDynamicPropertiesBean** @param Properties coreDynamicPropertiesBean*/public void setCoreDynamicPropertiesBean(Properties coreDynamicPropertiesBean) {this.coreDynamicPropertiesBean = coreDynamicPropertiesBean;}/*** Gets DynamicPropertiesMap** @return HashMap dynamicPropertiesMap*/public HashMap<String, String> getDynamicPropertiesMap() {return dynamicPropertiesMap;}/*** Sets DynamicPropertiesMap** @param HashMap dynamicPropertiesMap*/public void setDynamicPropertiesMap(HashMap<String, String> dynamicPropertiesMap) {this.dynamicPropertiesMap = dynamicPropertiesMap;}/*** Gets a new instance of CoreDynamicPropertiesBean** @return Properties CoreDynamicPropertiesBean*/public Properties createCoreDynamicPropertiesBeanInstance() {return (Properties) this.beanFactory.getBean(SystemConstants.BEAN_NAME_CORE_DYNAMIC_PROPERTIES_BEAN);}/*** Sets BeanFactory** @param BeanFactory beanFactory*/public void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory = beanFactory;}}

步驟7:創(chuàng)建應(yīng)用程序類

應(yīng)用程序類啟動項目。

package com.otv.starter;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.otv.common.SystemConstants;/*** Application Starter Class** @author onlinetechvision.com* @since 26 May 2012* @version 1.0.0**/ public class Application {/*** Main method of the Application**/public static void main(String[] args) {new ClassPathXmlApplicationContext(SystemConstants.APPLICATION_CONTEXT_FILE_NAME);} }

第8步:運行項目

如果運行應(yīng)用程序類,則顯示以下控制臺日志:

26.05.2012 17:25:09 INFO (DefaultLifecycleProcessor.java:334) - Starting beans in phase 2147483647 26.05.2012 17:25:09 INFO (SchedulerFactoryBean.java:648) - Starting Quartz Scheduler now 26.05.2012 17:25:09 INFO (PropertiesLoaderSupport.java:177) - Loading properties file from class path resource [DynamicPropertiesFile.properties] 26.05.2012 17:25:09 INFO (DynamicPropertiesFileReaderTask.java:36) - **** Dynamic Properties File Reader Task is being started... **** 26.05.2012 17:25:09 INFO (DynamicPropertiesFileReaderTask.java:63) - Message_Content : Welcome Visitor 26.05.2012 17:25:09 INFO (DynamicPropertiesFileReaderTask.java:76) - Minimum_Visitor_Count : 1 26.05.2012 17:25:09 INFO (DynamicPropertiesFileReaderTask.java:96) - Maximum_Visitor_Count : 10 26.05.2012 17:25:09 INFO (DynamicPropertiesFileReaderTask.java:38) - **** Dynamic Properties File Reader Task is stopped... ****26.05.2012 17:26:09 INFO (PropertiesLoaderSupport.java:177) - Loading properties file from class path resource [DynamicPropertiesFile.properties] 26.05.2012 17:26:09 INFO (DynamicPropertiesFileReaderTask.java:36) - **** Dynamic Properties File Reader Task is being started... **** 26.05.2012 17:26:09 INFO (DynamicPropertiesFileReaderTask.java:63) - Message_Content : Welcome Visitor, Bruce! 26.05.2012 17:26:09 INFO (DynamicPropertiesFileReaderTask.java:76) - Minimum_Visitor_Count : 2 26.05.2012 17:26:09 INFO (DynamicPropertiesFileReaderTask.java:96) - Maximum_Visitor_Count : 20 26.05.2012 17:26:09 INFO (DynamicPropertiesFileReaderTask.java:38) - **** Dynamic Properties File Reader Task is stopped... ****

步驟9:下載

OTV_SpringDynamicPropertyManagement

參考資料:

Spring Framework參考3.x

參考: Online Technology Vision博客中的JCG合作伙伴 Eren Avsarogullari提供的Spring動態(tài)物業(yè)管理 。


翻譯自: https://www.javacodegeeks.com/2012/05/dynamic-property-management-in-spring.html

總結(jié)

以上是生活随笔為你收集整理的Spring动态物业管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。