您的位置:首页 > 编程语言 > Java开发

使用xml配置Quartz,实现定时任务

2017-12-12 11:00 736 查看

使用xml配置Quartz,实现定时任务

使用xml配置前景

以前我是使用java代码配置Quartz任务的,到最好你会发现,这个类使用其他类成员变量会无法自动注入问题

始终是空指针问题,到最后只能手动到SpingContext中去取,这样过程很繁琐,不易于管理其他类成员变量

引入Maven依赖

<!-- quartz start -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<!-- quartz end -->


在Spring.xml导入spring-quartz.xml配置

<import resource="spring-quartz.xml" />


在spring-quartz.xml配置Quartz

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-lazy-init="false">
<!--配置任务类,也就是下面targetMethod所在的类-->
<bean id="myJob" class="com.founder.isp.fpay.tecsun.quartz.SycTecsunBalance" />
<!-- 配置job -->
<bean id="myJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob" />
</property>
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 配置触发器 -->
<bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0 * 23 * * ?</value>
</property>
</bean>
<bean name="schedulerFactoryBean" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="crontestJobTrigger" />
</list>
</property>
</bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xml java quartz