Spring的application.xml文件加载监听器ApplicationListene

在一个项目中有很多地方会用到一些常用但是不易变更的数据,比如中国股票市场的交易市场日,这些是可以确定,并且一般在今年中是不会改变的,所以可以将这些数据从数据库中加载到项目当中去,用spring管理这些bean时,可以使用spring文件加载监听器。例子:

 

public class CalendarListener implements ApplicationListener<ContextRefreshedEvent> {  

  @Override

  public void onApplicationEvent(ContextRefreshedEvent ev) {
      if(ev.getApplicationContext().getParent() == null){
      Long startTime = System.currentTimeMillis();
    try {
      log.info("loading calendar time...start...");
      //需要预先加载的方法,此时spring已经所有的bean生成
      log.info("loading calendar time...end...");
    } catch (Exception e) {
      log.error("the calendar time is not loaded ,please check it!!!!!!",e);
    }
      log.info("loading china calendar time is :"+(System.currentTimeMillis()-startTime)+"ms");
    }
  }

}

 

posted @ 2016-03-28 09:37  聂鲁达  阅读(325)  评论(0编辑  收藏  举报