1.applicationContext.xml 中 加入task 的聲明與xsd
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”,否則注解會失效
|
新聞熱點
疑難解答