一个简单的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()));
    } 
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM