1.applicationContext.xml 中 加入task 的聲明與xsd
1xmlns:task="http://www.springframework.org/schema/task"
1
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
配置中加入
1<task:annotation-driven/>
這個是用來啟用自動的注解解析。
2.編寫POJO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22@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”,否則注解會失效