Spring定时任务spring-Task

Spring Task提供两种方式进行配置还是一种是annotation(标注),另外一种就是XML配置。
第一种:注解方式
1.配置文件头部:
xmlns:task="http://www.springframework.org/schema/task"
2.xsi:schemaLocation添加:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
3.再加上前几节说道的 注解扫描的配置
<context:component-scan base-package="com.gl">   
</context:component-scan>    
 4.最后加上
<task:annotation-driven />  
搞定
spring task还有很多参数,参考xsd文档 http://www.springframework.org/schema/task/spring-task-4.0.xsd
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:task="http://www.springframework.org/schema/task"
  7.     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  8.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  10.         http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.0.xsd
    ">  
  11.     <!-- 配置自动扫描的包 -->  
  12.     <context:component-scan base-package="com.gl">
  13.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  14.     </context:component-scan>  
  15.    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
  16.    <!-- task-->
  17.     <task:annotation-driven />
  18. </beans>  

    5.任务类

                    @Component

         @Lazy(value=false)

        public class Test throws Exception {

             @Scheduled(cron = "*/5 * * * * ?")//每隔5秒执行一次具体执行间隔请百度

                public void test(){

             System.out.println("this is task test");

         }

第二种:配置文件方式

   1:配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:task="http://www.springframework.org/schema/task"
  7.     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  8.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  10.         http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.0.xsd
    ">  
  11.     <!-- 配置自动扫描的包 -->  
  12.     <context:component-scan base-package="com.gl">
  13.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  14.     </context:component-scan>  
  15. <task:scheduler id="scheduler" pool-size="5" /> 
  16. <task:scheduled-tasks scheduler="scheduler"> 
  17.                   <task:scheduled ref="Test" method="test" cron="0 * * * * ?"/>     
  18. </task:scheduled-tasks>
  19. </beans>  

ref : 定时任务类名 method:执行的方法名

2.测试类

@Service 
public  class  Test { 
     public  void test () { 
         System.out.println(“this is task test”); 
    
}

猜你喜欢

转载自blog.csdn.net/qq_36967136/article/details/80734860