转载请注明出处:http://blog.csdn.net/l1028386804/article/details/52865052
今天,给大家来一篇Spring的小插曲,用@Scheduled注解来实现Spring quartz的定时执行任务功能。
导入Spring与Spring quartz相关的jar包,配置applicationContext.xml文件
xmlns 多加下面的内容
xmlns:task="http://www.springframework.org/schema/task"
然后xsi:schemaLocation多加下面的内容
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
最后是我们的task任务扫描注解
<task:annotation-driven/>
我的配置扫描位置是
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="com.test"/>
扫描的是com.test这样的包下的内容、
下面需要接口和实现(我的这几个Java文件都是com.test的包下的、)
public interface IMyTestService {
public void myTest();
}
@Component //import org.springframework.stereotype.Component;
public class MyTestServiceImpl implements IMyTestService {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
@Override
public void myTest(){
System.out.println("进入测试");
}
}
这样就配置好了,然后我们就可以测试定时任务了。