spring @Scheduled注解 定时任务 详解

scheduled的使用注解的方式进行调度

先要配置spring.xml

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

然后xsi:schemaLocation

http://www.springframework.org/schema/task  
http://www.springframework.org/schema/task/spring-task-3.1.xsd 

再加上扫描注解

<task:annotation-driven/>  

然后扫描位置

<context:annotation-config/>  
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
    <context:component-scan base-package="com.zyt.test"/>  

调用

import org.springframework.stereotype.Component;  

@Component  
public class TestService {  
    // 5秒钟执行一次 @Scheduled(cron
="0/5 * * * * ? ") //每5秒执行一次
    public void test(){ System.out.println("每隔五秒打印一次"); } }

@Scheduled 注解要写在实现方法上

定时器的任务方法不能有返回值(如果有返回值的话,spring初始化的时候会报错,需要设定一个proxytargetclass的某个值为true,请自行百度google)

剩下的就是corn表达式了

请自行百度google

原文地址:https://www.cnblogs.com/zhouyantong/p/6904177.html