两种方法:

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
}
}
}

spring 初始化时注入bean实现listener的方法的更多相关文章

  1. Spring - 运行时获取bean(ApplicationContextAware接口)

    默认情况下,我们的bean都是单例模式(即从容器初始化到销毁只保持一个实例).当一个bean需要引用另外一个bean,我们往往会通过bean属性的方式通过依赖注入来引用另外一个bean.那么问题就来了 ...

  2. spring注解方式注入bean

    用注解的方式注入bean,spring的配置文件也要增加一些约束和导入注解所在的包 applicationContext.xml <?xml version="1.0" en ...

  3. Spring中注解注入bean和配置文件注入bean

    注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...

  4. spring boot 动态注入bean

    方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...

  5. Spring学习笔记--注入Bean属性

    这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...

  6. springboot中@webfilter注解的filter时注入bean都是null

    在使用@Webfilter注解Filter的情况下,不上外部tomcat时是没有问题的.但是在tomcat下运行时,filter中注入的bean就都是null 解决办法: 一:去掉@Webfilter ...

  7. Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)

    在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...

  8. spring注入bean的三种方法

    在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

  9. Spring基于xml注入bean的几种方式?

    (1)Set方法注入: (2)构造器注入:①通过index设置参数的位置:②通过type设置参数类型: (3)静态工厂注入: (4)实例工厂:

随机推荐

  1. 03-树2 Tree Traversals Again

    这题是第二次做了,两次都不是独立完成,不过我发现我第一次参考的程序,也是参考老师(陈越)的范例做出来的.我对老师给的做了小幅修改,因为我不想有全局变量的的存在,所以我多传了三个参数进去.正序遍历每次都 ...

  2. C++ offsetof

    这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...

  3. Android库Volley的使用介绍

    Android Volley 是Google开发的一个网络lib,可以让你更加简单并且快速的访问网络数据.Volley库的网络请求都是异步的,你不必担心异步处理问题. Volley的优点: 请求队列和 ...

  4. iOS log第三方库之间的对比

  5. Android crash特殊位置定位

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 通常情况下,在我们开发的过程中遇到的crash,可以到logcat中找原因:如果做定制App,对方用 ...

  6. POJ2449 (k短路)

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  7. Web Browser使用技巧

    无论是在桌面级开发中,还是在手机端开发中,WebBrowser都是一个经常会用到的控件.Windows Phone中的WebBrowser虽然远远没有桌面版那么强大,但依然足够应付常规用途.本文就来介 ...

  8. url重写技术

    URL 重写是截取传入 Web 请求并自动将请求重定向到其他 URL 的过程.比如浏览器发来请求 hostname/101.aspx ,服务器自动将这个请求中定向为http://hostname/li ...

  9. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  10. Intellij IDEA 工具快捷键

    IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类 ...