java web程序启动自动运行方法(spring)
2017-01-09 16:38
441 查看
场景:将web项目部署到tomcat后,启动tomcat后自动运行程序重的某个方法。
环境:spring、springmvc
直接新建一个controller类,让该类实现ApplicationListener<ContextRefreshedEvent>,实现该接口的
public void onApplicationEvent(ContextRefreshedEvent arg0) 方法,在该方法内部编写你要启动后执行的方法。
例如:我写的这个是启动后加载域名到缓存中:
import javax.annotation.Resource;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Controller;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
@Controller
public class DomainCacheController implements ApplicationListener<ContextRefreshedEvent>{
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
// TODO Auto-generated method stub
CacheManager cache = CacheManager.getInstance();
Cache domainCache = cache.getCache("domainCache");
Element element = null;
List<TopLevelDomain> allTop = topService.obtainAllTopLevelDomainByCompanyId(0);
for (TopLevelDomain top : allTop) {
element = new Element(top.getTopUrl(), top.getCompanyId());
domainCache.put(element);
}
List<SecondLevelDomain> allSecond = secondService.obtainAllSecondLevelDomainByCompanyId(0);
for (SecondLevelDomain second : allSecond) {
element = new Element(second.getSecondUrl(), second.getCompanyId());
domainCache.put(element);
}
}
}
环境:spring、springmvc
直接新建一个controller类,让该类实现ApplicationListener<ContextRefreshedEvent>,实现该接口的
public void onApplicationEvent(ContextRefreshedEvent arg0) 方法,在该方法内部编写你要启动后执行的方法。
例如:我写的这个是启动后加载域名到缓存中:
import javax.annotation.Resource;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Controller;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
@Controller
public class DomainCacheController implements ApplicationListener<ContextRefreshedEvent>{
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
// TODO Auto-generated method stub
CacheManager cache = CacheManager.getInstance();
Cache domainCache = cache.getCache("domainCache");
Element element = null;
List<TopLevelDomain> allTop = topService.obtainAllTopLevelDomainByCompanyId(0);
for (TopLevelDomain top : allTop) {
element = new Element(top.getTopUrl(), top.getCompanyId());
domainCache.put(element);
}
List<SecondLevelDomain> allSecond = secondService.obtainAllSecondLevelDomainByCompanyId(0);
for (SecondLevelDomain second : allSecond) {
element = new Element(second.getSecondUrl(), second.getCompanyId());
domainCache.put(element);
}
}
}
相关文章推荐
- 让程序在Windows CE系统启动时自动运行的两种方法
- 让程序在Windows CE系统启动时自动运行-两种方法
- Java中如何让web服务器启动的时候自动运行web程序中某个类的某个方法
- 让程序在Windows CE系统启动时自动运行-两种方法
- Java中如何让web服务器启动的时候自动运行web程序中某个类的某个方法
- 让程序在Windows CE系统启动时自动运行
- 在linux下设置开机自动启动程序的方法
- 在linux下设置开机自动启动程序的方法(一)
- 在Mac OSX启动时运行程序的方法
- S60 3.0上的程序安装后启动和程序开机自动运行
- S60 3.0上的程序安装后启动运行和程序开机自动运行
- Windows自动启动程序方法
- 求助操作系统启动,自动运行程序,用DELPHI 要怎么写代码?
- 让程序在Windows启动时自动运行并自动隐藏
- 编写随Windows启动自动运行的程序
- 在windows xp启动前运行程序(如杀毒软件的bootscan)的方法
- [转帖]让程序在Windows CE系统启动时自动运行[转帖]让程序在Windows CE系统启动时自动运行
- 设置系统启动时程序自动运行
- S60 3.0上的程序安装后启动和程序开机自动运行
- 编写随Windows启动自动运行的程序