版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/maiyikai/article/details/70199668
许久没有写博文了,今天来写写,毕业设计中遇到的某个小问题,及解决方法。
毕业设计中涉及到spring的定时器quartzy的使用,
遇到的问题是:quartzy中不能使用spring注入的方式注入dao层和service层,原因是spring在加载的时候先加载quarzy,后加载bean。查看了一下源代码:org.springframework.scheduling.quartz.JobDetailFactoryBean。
这个类我们只看他的实现:
![JobDetailFactoryBean类的实现](https://img-blog.csdn.net/20170416232235638?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFpeWlrYWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
实现了ApplicationContextAware,这个类是我当时在实现rpc服务的时候了解过的一个类,适用于异步加载的,可以理解为分开执行,所以我的的bean并不能被注入到quartzy中。
不过利用了一个晚上的时间查找了一些资料,终于皇天不负有心人,找到了一篇:原文地址
接下来我们看代码:
先重写一下AdaptableJobFactory类中的createJobInstance(TriggerFiredBundle bundle)方法:
public class JobFactory extends AdaptableJobFactory{
//这个对象Spring会帮我们自动注入进来,也属于Spring技术范畴.
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//AutowireCapableBeanFactory这个接口一般在applicationContext的内部是较少使用的,
//它的功能主要是为了装配applicationContext管理之外的Bean。
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
spring配置文件中的配置:
<!-- quartz定时器,定时器的类--自己用的类 -->
<bean id="jobDetailFactoryBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.myk.oe.utils.QuartzTask"></property>
</bean>
<!-- 配置执行时间和规则 -->
<bean id="quartzTask" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetailFactoryBean"></property>
<!-- 5秒一次 -->
<property name="cronExpression" value="0/5 * * * * ?"></property>
</bean>
<!-- 将bean注入到定时器的类 -->
<bean id="jobFactory" class="com.myk.oe.quartz.util.JobFactory"></bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="triggers">
<list>
<ref bean="quartzTask" /><!-- 任务 -->
</list>
</property>
<property name="jobFactory" ref="jobFactory"></property>
<property name="autoStartup" value="true"/><!-- 自动开始 -->
</bean>
配置完成之后,看定时器的执行类:
@DisallowConcurrentExecution
public class QuartzTask extends QuartzJobBean{
@Resource(name="appointmentServiceImpl")
private AppointmentService appointmentService;//这是服务类
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
List<Appointment> appointments = appointmentService.getAppointments();//获取数据
System.err.println(appointments);
}
}
然后执行结果:
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5436b941] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@24e6e99e] will not be managed by Spring
==> Preparing: SELECT * FROM onlineexamination.appointment where app_date >=date_sub(now(),interval 2 day) and app_date < now()
==> Parameters:
<== Columns: app_id, jiasy_id, kemu_type_id, app_price, app_date
<== Row: 1000004, 1000005, k001, 200.00, 2017-04-15
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5436b941]
[Appointment [app_id=1000004, jiasy_id=1000005, kemu_type_id=k001, app_price=200.0, app_date=2017-04-15, jsyxxb=null, tmlxb=Tmlxb [kemu_type_id=k001, kemu_price=0.0, kemu_name_string=null]]]
看到这个结果的,还是比较欣慰的,感谢那个大牛。