经过测试,spring的定时任务需要以下配置:
1.修改applicationContext.xml
xmlns部分增:xmlns:task="http://www.springframework.org/schema/task" xsi部分增加:http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
下面是测试:
@Component
public class TaskScheduled {
@Scheduled(cron = "*/5 * * * * ?")
public void listDetails() {
System.out.println("result");
}
}
springboot定时任务开启:
在程序的入口类中添加@EnableScheduling注解
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
测试:
@Component
public class ScheduledTask {
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* 每间隔10秒输出时间
*/
@Scheduled(fixedRate = 10000)
public void logTime(){
logger.info("定时任务,现在时间:"+System.currentTimeMillis());
}