spring自带task定时
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-3.1.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("进入测试");
- }
- }