Spring/SpringMVC在启动完成后执行方法
http://www.icoolxue.com/blog/show/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
Java代码 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.storezhang.web; import com.storezhang.util.TimeUtils; import com.storezhang.video.util.SiteMapUtils; import java.util.Timer; import java.util.TimerTask; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Service; /** * 启动监听器 * * @author Storezhang */ @Service public class StartupListener implements ApplicationListener<ContextRefreshedEvent> { @Autowired private SiteMapUtils sites; @Override public void onApplicationEvent(ContextRefreshedEvent evt) { if (evt.getApplicationContext().getParent() == null) { createSitemap(); } } private void createSitemap() { Timer timer = new Timer("createSitemap", true); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("--->Create sitemap..."); sites.createSiteMap(); System.out.println("--->Success create sitemap..."); } }, 1 * TimeUtils.MIN); } } |