Spring 定時任務配置,Spring任務配置

來源:互聯網
上載者:User

Spring 定時任務配置,Spring任務配置

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”,否則註解會失效

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.