1.ApplicationContext.xml配置web
a)、須要在xmlns裏面加入:
xmlns:task="http://www.springframework.org/schema/task"
b)、在xsi:schemaLocation中加入
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd(別忘了最後的引號)spring
c)、加入配置
<context:component-scan base-package="com.netease.lee" /><!--須要掃描的包-->
<context:annotation-config /><!--開啓註解-->
<task:annotation-driven/> <!-- 這句是重點 定時器開關-->app
d)、web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
< /listener>
.net
2.徹底註解方式component
@Service
public class AnnotationQuartz {
@Scheduled(cron="0,10,20,30,40,50 * * * * ?") //須要注意@Scheduled這個註解,它可配置多個屬性:cron\fixedDelay\fixedRate
public void test(){
System.out.println("0.0");
}
}
3.註解加配置方式xml
@Component(若是你的項目使用的是註解而不是配置文件中寫bean,那麼須要加上@Component,確保這個類已經注入)
public class AnnotationQuartz {
public void test()
{
System.out.println("0.0");
}
}
blog
spring的ApplicationContext.xml中的配置:
<task:scheduled-tasks>
<task:scheduled ref="chartsService" method="test" cron="0 0,15,30,45 * * * ?"/>
< /task:scheduled-tasks>
4.相關文章get
http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/it
http://jooben.blog.51cto.com/253727/406277io
5.實際使用狀況
按如上配置以後,定時器根本就沒反應,不知道哪裏的問題,仍是用老方法,配置方式吧
<bean id="smsSenderTimerQuarz" class="sunit.app.timer.SMSSenderTimer"/> <bean id="quartzSMSSender" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="smsSenderTimerQuarz"/> </property> <property name="targetMethod"> <value>smsSender</value> </property> </bean> <bean id="quartzSMSSenderTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="quartzSMSSender"/> </property> <property name="cronExpression"> <value>0 0/5 * * * ?</value> </property> </bean>