一个简单的Spring定时器例子 注解方式

首先在applicationContext.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.xsd
<beans xmlns:task="http://www.springframework.org/schema/task"
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd" >

<!-- 定义使用注解自动扫描的包 -->
<context:component-scan base-package="xxx" /> 定时器必须属于扫描的包中

<!-- 打开定时器开关 -->

<task:annotation-driven/>

下面是定时器的写法

fixedDelay = 5000 表示每隔5秒执行
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component  
public class TestJob {
    @Scheduled(fixedDelay = 5000) 
    public void test()
    {
        System.out.println("job 开始执行"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    } 
}
原文地址:https://www.cnblogs.com/mingf123/p/3861094.html