spring 使用注解来调度定时任务

1.在需要加载spring的配置文件里spring.xml / applicationContext.xml 添加

[html]  view plain copy
  1. xmlns:task="http://www.springframework.org/schema/task"  
  2. xsi:schemaLocation="  
  3. http://www.springframework.org/schema/task          
  4. http://www.springframework.org/schema/task/spring-task-3.0.xsd  

2.配置自动调度的包和定时开关

[html]  view plain copy
  1.   
  2.     <context:component-scan base-package="*" />   
  3.     <task:executor id="executor" pool-size="5" />      
  4.     <task:scheduler id="scheduler" pool-size="10" />    
  5.     <task:annotation-driven executor="executor" scheduler="scheduler" />  

3.配置调度和注解调度

[html]  view plain copy
  1.   
  2.       

4.在web.xml配置里添加启动时要扫描的配置文件和监听

[html]  view plain copy
  1. <context-param>  
  2.    <param-name>contextConfigLocationparam-name>  
  3.    <param-value>/WEB-INF/applicationContext*.xmlparam-value>  
  4.  context-param>  
  5.  <listener>  
  6.    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  7.  listener>  

5.添加调度的包

6.类测试

[java]  view plain copy
  1. @Service  
  2. public class demo {  
  3.     @Scheduled(cron="0/5 * * * * ? ")  
  4.     public void myTestWork(){  
  5.           
  6.         System.out.println("ssss");  
  7.     }  
  8.   
  9. }  

你可能感兴趣的:(定时任务调度)