两种方法:

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. Ubuntu 14.10 下卸载MySQL

    前面讲了Mysql的简单安装方式,通过sudo apt-get install mysql-server 等脚本,安装之后如何卸载? 1 通过下面命令删除MySQL sudo apt-get auto ...

  2. C/C++ memmove 和 memcpy

    这两个函数用于拷贝字符串或者一段连续的内存,函数原型: void * memcpy ( void * destination, const void * source, size_t num ); v ...

  3. 谈谈 Mifare Classic 破解

    2008 年的时候,荷兰恩智浦(NXP)公司开发的 RFID 产品 Mifare Classic 就被破解了,黑历史在这里就不在具体说了,想详细了解可以自己 Google 百度.现在还是重点说说关于 ...

  4. 网络图片下载缓存库SDWebImage的使用

    SDWebImage导入问题 最新的SDWebImage由于是基于ARC模式写的,如果创建的是非ARC醒目的童鞋们注意,导入文件夹之后,先添加ImageIO.framework,mapKit.fram ...

  5. Squid代理之普通代理

    1.配置Squid 代理服务器IP地址 将eth1的IP地址修改为200.168.10.1

  6. 搬家后Magento前台只有产品的缩略图不显示

    第一种可能:缓存不足 http://blog.csdn.net/ddjohn/article/details/6648199 最近发现一个怪异的现象,Magento前台只有产品的缩略图不显示.我检查了 ...

  7. php 数组 添加元素、删除元素

    拆分数组 PHP数组添加一个元素的方式: push(), arr[], Php代码 $arr = array(); array_push($arr, el1, el2 ... eln); 但其实有一种 ...

  8. LeetCode OJ String to Integer (atoi) 字符串转数字

    #include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...

  9. native

    源博客:http://blog.csdn.net/jiakw_1981/article/details/3073613 一. 什么是Native Method   简单地讲,一个Native Meth ...

  10. hadoop常用基础命令

    1.日志查询: 用户可使用以下命令在指定路径下查看历史日志汇总$ bin/hadoop job -history output-dir 这条命令会显示作业的细节信息,失败和终止的任务细节. 关于作业的 ...