經過測試,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());
}