1.applicationContext.xml 中 加入task 的声明与xsd
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
配置中加入
<task:annotation-driven/>
这个是用来启用自动的注解解析。
2.编写POJO
@Component public class DailyPiracyJob { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private AppInfoService appInfoService; @Scheduled(cron = "0 0 23 * * ?") public void scan() throws Exception { try { List<AppInfo> allAppList = appInfoService.selectAllAppInfo(); if(null != allAppList && allAppList.size() > 0){ for(AppInfo appInfo : allAppList){ appInfoService.insertDailyPiracy(appInfo.getAppMd5()); } } } catch (Exception e) { logger.error("error when Channel Monitoring.", e); } } }
@Compont 注解,是让Spring context 可以扫描到,并自动注入需要的bean
@Scheudle 核心注解,不能有返回值,cron是定义了任务运行的间隔,具体,请参考网上其他教程
需要注意的是,在applicationContext.xml中不能启用 default-lazy-init=“true”,否则注解会失效