Spring 定时任务的配置

1.applicationContext.xml 中 加入task 的声明与xsd

1
xmlns:task="http://www.springframework.org/schema/task"
1
http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-4.0.xsd

配置中加入

1
<task:annotation-driven/>

这个是用来启用自动的注解解析。

 

2.编写POJO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Component 
public class DailyPiracyJob {   
    @Scheduled(cron = "0 0 23 * * ?")
    public void print() throws Exception {
    system.out.println(干点你想干的事!);      
    }
}

  @Compont 注解,是让Spring context 可以扫描到,并自动注入需要的bean

      @Scheudle 核心注解,不能有返回值,cron是定义了任务运行的间隔,具体,请参考网上其他教程

需要注意的是,在applicationContext.xml中不能启用 default-lazy-init=“true”,否则注解会失效

posted @ 2016-08-05 15:18  前度刘郎  阅读(233)  评论(0编辑  收藏  举报
欢迎来到戴建伟的博客!