如何在项目启动是执行某些操作
来源:互联网 发布:kettle java jndi 编辑:程序博客网 时间:2024/06/05 08:51
在开发中,有时候我们会想在项目启动时就执行某些操作,如将某些存在数据库里的数据刷到内存里以便在项目里快速使用这些数据、跑一些批处理。
此处介绍两类方法:
第一类:
项目启动时,利用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 @PostConstruct10 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 @Override4 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.xml6 </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>
- 如何在项目启动是执行某些操作
- 在项目启动时就执行某些操作
- 在view关闭时执行某些操作
- 【j2ee系列】springmvc中使用quartz,项目启动就执行某些任务
- 代码是如何在栈中执行
- 使用企业库在某些站点会报试图执行安全策略不允许的操作异常的解决方法
- .net全局定时定期执行某些操作在Global.asax中具体实现
- .net全局定时定期执行某些操作在Global.asax中具体实现
- .net全局定时定期执行某些操作在Global.asax中具体实现
- 项目启动是执行某个方法(监听器的使用)
- Spring加载的时候执行某些操作
- 如何在 BroadcastReceiver 中执行耗时操作?
- 某些项目在eclipse上部署之后,启动tomcat时报错:“java.lang.NoClassDefFoundError:... ”
- 如何禁止某些使用者执行su
- ~/.bash_profile在某些情况下没有执行
- 如何在org.eclipse.ui.part.ViewPart失去焦点时完成某些操作
- 在web项目启动时,执行某个方法
- 在web项目启动时,执行某个方法
- javascript自执行匿名函数(function(){})()
- 专为渗透测试人员设计的 Python 工具大合集
- 网易春招[编程题]工作安排@Java
- 深入理解RunLoop
- 【oracle】位图索引 bitmap
- 如何在项目启动是执行某些操作
- Numpy,SciPy和MatplotLib 安装 -- python
- nginx学习随笔--ssl_session_timeout
- 消息代理 RabbitMQ 一、概念简介
- 7
- 2017秋招-技术岗-海能达-Java开发(2轮)
- 10 个最基本的JS面试问题及答案
- HDU 1072Nightmare(dfs+剪枝)
- Java集合类详解