在開發中,有時候我們會想在項目啟動時就執行某些操作,如將某些存在數據庫里的數據刷到內存里以便在項目里快速使用這些數據、跑一些批處理。
此處介紹兩類方法:
第一類:
項目啟動時,利用spring容器初始化bean來實現。
共3種方法: (1)通過@PostConstruct方法實現初始化bean進行操作
(2)通過在bean相關的xml配置文件中配置init-method方法
(3)通過bean實現InitializingBean接口
第二類:
項目啟動后,通過quartz,立即執行該操作。
(4)通過org.springframework.scheduling.quartz.SimpleTriggerBean方式,可以配置間隔多長時間執行一次任務,如<property name="repeatInterval" value="3000" />就是指定3秒執行一次任務。
下面逐一介紹這4種方法:
方法一:@PostConstruct方法
在實現類和方法上加注解,類上加bean注解,方法上加@PostConstruct注解。
1 //本人此類是在將黑名單從數據庫中查詢出來,並放到內存 2 @Service("phoneBlacklistCache") 3 public class PhoneBlacklistCache { 4 public List<String> phoneBlacklist = new ArrayList<String>(); 6 //次注解是操作的關鍵 9 @PostConstruct 10 public void init(){ 11 //想進行的操作 12 //比如:查詢數據庫,寫到內存 13 } 14 }
方法二:init-method方法
在xml文件里配置bean,bean的配置中配置init-method方法(該方法配置成你想要執行的操作方法)。
<bean id="PoneBlacklistCache" class="com.jd.qrx.trade.cache.impl.PhoneBlacklistCache" scope="singleton" init-method="init"></bean>
定義類文件。
1 public class PhoneBlacklistCache { 2 public List<String> phoneBlacklist = new ArrayList<String>(); 3 public void init(){ 4 //想進行的操作 5 //比如:查詢數據庫,寫到內存 6 } 7 }
方法三:InitializingBean方法
定義相應類實現InitializingBean接口。
1 public class PhoneBlacklistCache implements InitializingBean{ 2 public List<String> phoneBlacklist = new ArrayList<String>(); 3 @Override 4 public void afterPropertiesSet() throws Exception { 5 //想進行的操作 6 //比如:查詢數據庫,寫到內存 7 } 8 }
其中,afterPropertiesSet()方法里寫想要操作的代碼。
方法四:quartz方法
通過將SimpleTriggerBean配置成項目啟動后立即執行,且重復執行次數配置成0,不重復執行。即可達到項目啟動立即執行一此操作的目的。
具體配置如下:
1、我的項目是springMVC框架,在web.xml中作如下配置:
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value> 4 classpath:conf/spring-config.xml, 5 classpath:conf/quartz-config.xml 6 </param-value> 7 </context-param
2、然后在quartz-config.xml中做如下配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 7 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 8 default-lazy-init="false"> 9 <bean id="quartzJob" class="xx.xx.xx.QuartzJob"/> 10 <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 11 <property name="targetObject"> 12 <ref bean="quartzJob"/> 13 </property> 14 <property name="targetMethod"> 15 <value>xxxxxx(你在QuartzJob中的方法)</value> 16 </property> 17 </bean> 18 19 <!-- 項目啟動后任務就執行一次 --> 20 <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 21 <property name="jobDetail" ref="jobDetail"/> 22 <property name="startDelay" value="500"/> 23 <property name="repeatInterval" value="0"/> 24 <property name="repeatCount" value="0"/> 25 </bean> 26 27 <span style="white-space:pre"></span> 28 <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 29 <property name="triggers"> 30 <list> 31 <ref bean="simpleTrigger"/> 32 </list> 33 </property> 34 </bean> 35 </beans>