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
}
}
}
spring 初始化时注入bean实现listener的方法的更多相关文章
- Spring - 运行时获取bean(ApplicationContextAware接口)
默认情况下,我们的bean都是单例模式(即从容器初始化到销毁只保持一个实例).当一个bean需要引用另外一个bean,我们往往会通过bean属性的方式通过依赖注入来引用另外一个bean.那么问题就来了 ...
- spring注解方式注入bean
用注解的方式注入bean,spring的配置文件也要增加一些约束和导入注解所在的包 applicationContext.xml <?xml version="1.0" en ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- spring boot 动态注入bean
方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...
- Spring学习笔记--注入Bean属性
这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...
- springboot中@webfilter注解的filter时注入bean都是null
在使用@Webfilter注解Filter的情况下,不上外部tomcat时是没有问题的.但是在tomcat下运行时,filter中注入的bean就都是null 解决办法: 一:去掉@Webfilter ...
- Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)
在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...
- spring注入bean的三种方法
在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
- Spring基于xml注入bean的几种方式?
(1)Set方法注入: (2)构造器注入:①通过index设置参数的位置:②通过type设置参数类型: (3)静态工厂注入: (4)实例工厂:
随机推荐
- 想调试,装了个Zend Server
本来apache+mysql+php+zend studio+ svn + ftp +ssh跑的挺好,总想跑个断点调个程序能爽些,白瞎这么强的GUI集成开发环境,只能System.out.printl ...
- HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn
Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Jav ...
- Java课程作业1
模仿JavaAppArguments.java实例,编写一个程序,此程序从命令行接受多个数字,求和之后输出. 设计思想:命令行参数都是字符串,必须将其转化成数字才能相加,定义一个数组接收字符串转化的数 ...
- HDU 3791
http://acm.hdu.edu.cn/showproblem.php?pid=3791 建立二叉树,对比是否相同 #include <iostream> #include <c ...
- WCF服务端行为的一些设置
[ServiceBehavior( InstanceContextMode = InstanceContextMode.Single, //表示所有的请求都用一个服务实例来处理 Concurren ...
- 实例讲解Oracle数据库设置默认表空间问题
实例讲解Oracle数据库设置默认表空间问题 实例讲解Oracle数据库设置默认表空间问题,阅读实例讲解Oracle数据库设置默认表空间问题,DBA们经常会遇到一个这样令人头疼的问题:不知道谁在O ...
- git 恢复工作区删除的所有文件
/********************************************************************* * git 恢复工作区删除的所有文件 * 说明: * 今天 ...
- VS2010 安装使用STLPort
VS2010 安装使用STLport 1.本机环境 win7 64位 visual studio 2010 中文旗舰版 STLport-5.2.1.tar.bz2 2.下载STLport http:/ ...
- 五 Servlet 技术
一 Servlet 基础 1. 定义相关: a) 是运行在 Web 服务器上得 Java 小程序 b) 只将处理结果返回给客户 c) 是实现接口 Servlet 的 java 类,能被服务器调用. d ...
- C# 接受邮件 两种方式
有些累了,不想写太多,直接把代码贴上 EWS 源码 POP协议 源码 PS:如果我们发现引入的一个dll,能够添加引用,但是一编译又找不到,那么很有可能是.net framework 版本不同. 不如 ...