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”); 
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringTaskSpring框架提供的一个用于实现定时任务的模块。它可以帮助开发者在应用中创建、调度和管理定时任务。 要使用SpringTask,首先需要在Spring配置文件中配置一个任务调度器。可以使用`@EnableScheduling`注解来启用SpringTask,并且在需要执行定时任务的方法上使用`@Scheduled`注解来指定任务的触发条件。 例如,下面的代码展示了一个简单的定时任务配置: ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableScheduling public class MyTask { @Scheduled(fixedRate = 5000) // 每隔5秒执行一次任务 public void myTask() { // 执行定时任务的逻辑代码 System.out.println("定时任务执行中..."); } } ``` 在上述代码中,使用`@Component`注解将`MyTask`类注册为Spring的组件,并使用`@EnableScheduling`注解启用SpringTask。然后,在`myTask()`方法上使用`@Scheduled`注解指定了定时任务的触发条件,这里是每隔5秒执行一次。 通过以上配置,当应用启动后,定时任务就会按照指定的触发条件自动执行。 除了`fixedRate`之外,`@Scheduled`注解还支持其他的触发条件配置,如`fixedDelay`、`cron`等,开发者可以根据具体需求选择合适的触发条件。 希望以上内容对你有帮助,如果还有其他问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值