版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/86324985
@Configuration
@EnableScheduling
public class TaskConfig2 {
}
上面是配置类.
某个其他类中:
@Scheduled(cron = "0/2 * * * * ?")
public void scheduleMethod() {
logger.info(new Date() + ",EnableScheduling ScheduleMethod");
}
输出:
INFO: 2019-01-11 15:02:44 [com.WeatherService:304] Fri Jan 11 15:02:44 CST 2019,EnableScheduling ScheduleMethod
INFO: 2019-01-11 15:02:46 [com.WeatherService:304] Fri Jan 11 15:02:46 CST 2019,EnableScheduling ScheduleMethod
INFO: 2019-01-11 15:02:48 [com.WeatherService:304] Fri Jan 11 15:02:48 CST 2019,EnableScheduling ScheduleMethod
INFO: 2019-01-11 15:02:50 [com.WeatherService:304] Fri Jan 11 15:02:50 CST 2019,EnableScheduling ScheduleMethod
@EnableScheduling的作用与<task:...../>的作用一样,会扫描整个项目中的@Scheduled注解
@Configuration类似于xml配置文件中的<beans...../>