@Scheduled注解 在spring 和springboot 中的配置

使用spring @Scheduled注解-执行定时任务

配置spring.xml

  1. <beans
  2.  

        xmlns:task="http://www.springframework.org/schema/task"  

        xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/task

        http://www.springframework.org/schema/task/spring-task-3.2.xsd">   

  存在多个定时时,由于Spring默认是串行执行,所以可以添加如下,使之并行执行

  <task:annotation-driven scheduler="scheduler"/> 

  <task:scheduler id="scheduler" pool-size="5"/>

  <context:annotation-config/>

  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

  <task:annotation-driven/>

  <!--扫描的是com.crm.web这样的包下的内容-->

  <context:component-scan base-package="com.crm.web" />

 

  在扫描的web 包下 添加@Component注解 

  @Component("task")

  public class task(){

    //在需要定时的方法上使用

    @Scheduled(cron="0/5 * *  * * ? ")

    public void demo(){}

  }

 

 

使用springboot @Scheduled注解-执行定时任务

1、在Application启动类上添加@EnableScheduling注解启动定时

2、使用@Component和@Scheduled(cron="0/5 * * * * ?")启动定时任务。(和spring使用@Component 和@Scheduled 用法一样)

 

 

 

  

posted @ 2020-07-02 16:15  深海可乐啤酒  阅读(473)  评论(0编辑  收藏  举报