用的maven开发
pom.xml里引入jar包
<properties>
<quartz.version>2.2.1</quartz.version>
</properties>
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
</dependencies>
创建quartz.xml文件,引入xml文件;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<!--检查还款信息并发短信定时器-->
<bean id="repaymentCheckMethod" class="com.ymicoin.app.service.impl.RepaymentCheckServiceImpl"></bean>
<bean id="repaymentCheck" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="repaymentCheckMethod"/>
<property name="targetMethod" value="run"/>
</bean>
<!-- ======================== 调度触发器 ======================== -->
<bean id="triggerRepaymentCheck" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="repaymentCheck"/>
<property name="cronExpression">
<value>00 00 00 * * ?</value>
</property>
</bean>
<!-- 定时器列表-->
<!-- ======================== 调度工厂 ======================== -->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="triggerRepaymentCheck"/>
</list>
</property>
</bean>
</beans>
然后创建com.ymicoin.app.service.impl.RepaymentCheckServiceImpl这个类
此类里面的方法名必须与
<bean id="repaymentCheck" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="repaymentCheckMethod"/>
<property name="targetMethod" value="run"/>
</bean>
的方法名一致(这里我自己同时取名为run)
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="triggerRepaymentCheck"/>
</list>
</property>
</bean>
里面的 <ref bean="triggerRepaymentCheck"/>可以有多个;
例如:
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="triggerRepaymentCheck"/>
<ref bean="triggerTest"/>//一定要跟上面那个类一样注册;
</list>
</property>
</bean>
遇到的问题:
一开始放在了web.xml文件中;
由于com.ymicoin.app.service.impl.RepaymentCheckServiceImpl这个类中注入了Service,然后运行时;显示service没有扫描到;后来在spring.xml中才有效。注解扫描时放在spring.xml中的
特别注意
我用的maven配置时 配置1.5.2版本的时候 竟然没有引入;一开始我还没发现。。。
引入2.2.1版本的时候,就好了!!!醉了