今天周四,从周二开始我就在断断续续的干一件事情:使用Spring + quartz 做一个简单的例子。不幸的是一直没弄好,直到今天早上才搞定!痛定思痛,为什么这么简单的一个demo弄了这么久,总结暂且不说,先看我想做的例子:
-------------application Context文件开始的分割线----------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"
default-lazy-init="true">
<!-- 普通的业务Bean -->
<bean name="testService" class="quartz.quartzTest"/>
<!-- 作业 -->
<bean id="jobDetail_test" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testService"/>
<property name="targetMethod" value="SayHello"/>
</bean>
<!-- 触发器 -->
<bean id="cronTriggers" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobDetail_test"/>
</property>
<!-- 每5秒运行一次 -->
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<!-- 计划 -->
<bean lazy-init = "false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="schedulerName">
<value>cronScheduler</value>
</property>
<property name="triggers">
<list>
<ref bean="cronTriggers"/>
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
</beans>
-----------application Context文件结束业务bean开始的分割线-----------
package quartz;
public class quartzTest {
public void SayHello() {
try {
System.out.println("I'm here!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
-----------业务bean结束测试类开始的分割线-------------------------
package quartz;
import java.io.IOException;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) throws IOException {
ApplicationContext ac = new ClassPathXmlApplicationContext("test_spring.xml");
System.out.println(new Date());
//SchedulerFactoryBean test = (SchedulerFactoryBean) ac.getBean("testScheduler");
}
}
注意就是以上所描述的这样一个简单的例子。一直断断续续困扰了我两天,总结了下:
1. 做这个例子的时候在一个有问题的工程里,例子的问题和工程的其它问题混杂在一起。
2. 标红的spring属性。default-lazy-init 这个属性一直是 true 的。也就是说这个context文件中的bean都默认处在延迟初始化的状态,也即是说如果没有显示的请求一个bean那么这些bean一直都不会被初始化,这也就是运行 test 程序瞬间完成但是并没有将任务触发的原因。
好吧,我承认自己确实傻,google了半天才发现是这个问题,一直纠结于quartz是否使用的不对,算了,最后share下关于lazy-init的知识:
1. 在context中两个地方可以定义这个属性<beans default-lazy-init = "" 和 <bean lazy-init = "", 但是最终 <bean lazy-init = ""说了算,当然这个要是没定义那么<beans default-lazy-init = "" 说了算。
2. 如果在一个spring配置文件中引入另外的配置文件,如:<import resource="classpath:beanss.xml"/>
则以被引入文件(beanss.xml)中设置的<beans />里的设置为准,与引入文件中的设置无关。
3. 关于延迟初始化有一点需要了解的是:加入你对一个bean设置了延迟初始化,但是恰巧这个bean是另外一个没有延迟初始化单例bean所依赖的。当Spring开始初始化的时,为了加载那个没有设置为延迟初始化的bean,被设置为延迟初始化的那个bean也因为被依赖一起加载了,这一点需要注意。这一点可以关注第二处标红:<bean lazy-init = "false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 如果第一处标红改为了default-lazy_init = "true", 这个例子也能正常运行。
OK, that's all, 记录我的一次犯傻。