• spring 初始化时注入bean实现listener的方法


    两种方法:

    1、实现ApplicationListener<ContextRefreshedEvent>的onApplicationEvent(ContextRefreshedEvent event)方法

    并在application.xml或者spring-servlet.xml中注入bean,如下所示

    <bean class="com.ss.pts.util.ContextParaInitialize"></bean>

    2、直接使用@postConstruct注解,只要是spring能扫描到的类即可。

    @Component
    public class ContextParaInitialize implements ApplicationListener<ContextRefreshedEvent> {
    private static Logger log = LoggerFactory.getLogger(ContextParaInitialize.class);
    public static List<MachineInfoModel> machineList = null;
    @Resource IMachineDao machineDao;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
    log.info("----start load machine info");
    try {
    if(event.getApplicationContext().getParent() == null){
    machineList = machineDao.getAllMachineInfo();
    }
    log.info("machine info : "+machineList.get(0).getIp());
    } catch (Exception e) {
    e.getStackTrace();
    log.error("machine info load error :"+e.getMessage());
    // TODO: handle exception
    }
    }

    @PostConstruct
    public void test (){
    log.info("...............................");
    log.info("----start load machine info");
    try {
    machineList = machineDao.getAllMachineInfo();
    log.info("machine info : "+machineList.get(0).getIp());
    } catch (Exception e) {
    e.getStackTrace();
    log.error("machine info load error :"+e.getMessage());
    // TODO: handle exception
    }
    }
    }

  • 相关阅读:
    取指定长度的字符串(包括中英文),以"..."的方式显示
    js 常用函数
    js 规范
    js高级编程笔记2
    js高级编程笔记
    WinJS开发div中元素的水平和垂直居中metro
    WinJS开发iframe中Javascript执行错误metro
    MySql乱码
    正则表达式符号系统
    Java替换字符串中的回车换行
  • 原文地址:https://www.cnblogs.com/anruy/p/5749476.html
Copyright © 2020-2023  润新知