1.ApplicationContext.xml配置
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(别忘了最后的引号)
c)、加入配置
<context:component-scan base-package="com.netease.lee" /><!--需要扫描的包-->
<context:annotation-config /><!--开启注解-->
<task:annotation-driven/> <!-- 这句是重点 定时器开关-->
d)、web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
< /listener>
2.完全注解方式
@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.注解加配置方式
@Component(如果你的项目使用的是注解而不是配置文件中写bean,那么需要加上@Component,确保这个类已经注入)
public class AnnotationQuartz {
public void test()
{
System.out.println("0.0");
}
}
spring的ApplicationContext.xml中的配置:
<task:scheduled-tasks>
<task:scheduled ref="chartsService" method="test" cron="0 0,15,30,45 * * * ?"/>
< /task:scheduled-tasks>
4.相关文章
http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/
http://jooben.blog.51cto.com/253727/406277
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>
相关推荐
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
Spring+mybatis annotation形式 进行增删查改,使用maven容器,配置完整,自己运行多次无问题
NULL 博文链接:https://bijian1013.iteye.com/blog/2142528
Spring annotation 样例
配置整合DWR3.0和Spring2.5使用annotation注解
Spring Annotation
NULL 博文链接:https://wsjjasper.iteye.com/blog/1570448
spring的Annotation注解.
Spring_Annotation_AOP
详解Spring_3.0基于Annotation的依赖注入实现
Spring MVC internationalization annotation example 这个是annotation 方式的国际化 访问http://localhost:8080/login.htm
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
jquery easyUI+struts2+spring+hibernate基于annotation实现的CRM
spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记
Spring的Annotation配置相关讲义
Spring IOC Annotation 注入 学习实例
spring加入hibernate annotation方式第一部分,需要其他部分请查看本人其他资源
SpringMVC Spring MyBatis 框架整合 Annotation Maven Project
spring+hibernate annotation 完整示例,带数据库脚本 mysql,tomcat,myeclipse项目