我用的是Spring3.2.4版本,系统用SpringMVC注解实现

1:在applicationContext.xml中加入下面的配置,

这是spring的组件扫描

<context:component-scan base-package="com.test.task">

这是定时任务的配置

<task:executor id="executor" pool-size="1" />
<task:scheduler id="scheduler" pool-size="1" />
<task:annotation-driven executor="executor" scheduler="scheduler" />

加上下面两个配置143352936.png

2:上面基于注解的定时任务的配置完了,下面就写一个类测试,类必须满足以下几个条件

一、用@Component标注类

二、用@Lazy(value=false)标注类(这个是关键,找了很多例子就是因为这个没加不能运行)

3:写具体的方法

@Scheduled(cron="0/5 * * * * *")
public void testTask(){
System.out.println("定时任务测试,每5秒执行一次");
}

4:配置完成后基本上就可以运行了。。。。