- 浏览: 2242343 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (423)
- FileNet相关 (3)
- 应用服务器相关 (22)
- Java综合 (77)
- 持久层 (24)
- struts (11)
- webservice (8)
- 虚拟机 (2)
- 光盘刻录 (0)
- AD及AD集群 (1)
- JS (33)
- F5 (0)
- loadrunner8.1 (0)
- Java 反编译工具 (2)
- DataBase (62)
- ant (1)
- 操作系统 (29)
- 我的任务 (3)
- 平台架构 (16)
- 业务规则引擎 (2)
- 模板 (1)
- EJB (5)
- spring (24)
- CMMI (1)
- 项目管理 (20)
- LDAP (13)
- JMS (10)
- JSP (19)
- JBPM (2)
- web MVC框架设计思想 (2)
- 第三方支付平台 (2)
- BUG管理工具 (1)
- 垃圾站 (2)
- php (1)
- swing (1)
- 书籍 (1)
- QQ qq (2)
- 移动互联网 (26)
- 爱听的歌曲 (0)
- hadoop (4)
- 数据库 (9)
- 设计模式 (1)
- 面试经验只谈 (1)
- 大数据 (9)
- sp (1)
- 缓存数据库 (8)
- storm (2)
- taobao (2)
- 分布式,高并发,大型互联网,负载均衡 (6)
- Apache Ignite (0)
- Docker & K8S (0)
最新评论
-
wangyudong:
新版本 Wisdom RESTClienthttps://gi ...
spring rest mvc使用RestTemplate调用 -
wangyudong:
很多API doc生成工具生成API文档需要引入第三方依赖,重 ...
spring rest mvc使用RestTemplate调用 -
zhaoshijie:
cfying 写道大侠,还是加载了两次,怎么解决啊?求。QQ: ...
spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) -
xinglianxlxl:
对我有用,非常感谢
spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) -
k_caesar:
多谢,学习了
利用maven的resources、filter和profile实现不同环境使用不同配置文件
关键字:spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用了其他类(可能是更复杂的关联),所以当我们去使用这个类做事情时发现包空指针错误,这是因为我们这个类有可能已经初始化完成,但是引用的其他类不一定初始化完成,所以发生了空指针错误,解决方案如下:
1、写一个类继承spring的ApplicationListener监听,并监控ContextRefreshedEvent事件(容易初始化完成事件)
2、定义简单的bean:<bean id="beanDefineConfigue" class="com.creatar.portal.webservice.BeanDefineConfigue"></bean>
或者直接使用@Component("BeanDefineConfigue")注解方式
完整的类如下:
package com.creatar.portal.webservice;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component("BeanDefineConfigue")
public class BeanDefineConfigue implements
ApplicationListener<ContextRefreshedEvent> {//ContextRefreshedEvent为初始化完毕事件,spring还有很多事件可以利用
// @Autowired
// private IRoleDao roleDao;
/**
* 当一个ApplicationContext被初始化或刷新触发
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// roleDao.getUserList();//spring容器初始化完毕加载用户列表到内存
System.out.println("spring容易初始化完毕================================================");
}
}
或者使用xml配置方式(非注解),简单配置个bean即可
<bean id="beanDefineConfigue" class="com.creatar.portal.webservice.BeanDefineConfigue"></bean>
其他定义方式:
完整的类如下:
package com.creatar.portal.webservice;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component("BeanDefineConfigue2")
public class BeanDefineConfigue2 implements ApplicationListener<ApplicationEvent> {
List<String> list = new ArrayList<String>();
/**
* 当一个ApplicationContext被初始化或刷新触发
*/
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
System.out.println("spring容易初始化完毕================================================888");
}
}
}
spring其他事件:
spring中已经内置的几种事件:
ContextClosedEvent 、ContextRefreshedEvent 、ContextStartedEvent 、ContextStoppedEvent 、RequestHandleEvent
后续研究:
applicationontext和使用MVC之后的webApplicationontext会两次调用上面的方法,如何区分这个两种容器呢?
但是这个时候,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。
这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码
如下:
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){//root application context 没有parent,他就是老大.
//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
}
}
后续发现加上以上判断还是能执行两次,不加的话三次,最终研究结果使用以下判断更加准确:event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")
看博客的最后一句话
应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用了其他类(可能是更复杂的关联),所以当我们去使用这个类做事情时发现包空指针错误,这是因为我们这个类有可能已经初始化完成,但是引用的其他类不一定初始化完成,所以发生了空指针错误,解决方案如下:
1、写一个类继承spring的ApplicationListener监听,并监控ContextRefreshedEvent事件(容易初始化完成事件)
2、定义简单的bean:<bean id="beanDefineConfigue" class="com.creatar.portal.webservice.BeanDefineConfigue"></bean>
或者直接使用@Component("BeanDefineConfigue")注解方式
完整的类如下:
package com.creatar.portal.webservice;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component("BeanDefineConfigue")
public class BeanDefineConfigue implements
ApplicationListener<ContextRefreshedEvent> {//ContextRefreshedEvent为初始化完毕事件,spring还有很多事件可以利用
// @Autowired
// private IRoleDao roleDao;
/**
* 当一个ApplicationContext被初始化或刷新触发
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// roleDao.getUserList();//spring容器初始化完毕加载用户列表到内存
System.out.println("spring容易初始化完毕================================================");
}
}
或者使用xml配置方式(非注解),简单配置个bean即可
<bean id="beanDefineConfigue" class="com.creatar.portal.webservice.BeanDefineConfigue"></bean>
其他定义方式:
完整的类如下:
package com.creatar.portal.webservice;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component("BeanDefineConfigue2")
public class BeanDefineConfigue2 implements ApplicationListener<ApplicationEvent> {
List<String> list = new ArrayList<String>();
/**
* 当一个ApplicationContext被初始化或刷新触发
*/
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
System.out.println("spring容易初始化完毕================================================888");
}
}
}
spring其他事件:
spring中已经内置的几种事件:
ContextClosedEvent 、ContextRefreshedEvent 、ContextStartedEvent 、ContextStoppedEvent 、RequestHandleEvent
后续研究:
applicationontext和使用MVC之后的webApplicationontext会两次调用上面的方法,如何区分这个两种容器呢?
但是这个时候,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。
这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码
如下:
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){//root application context 没有parent,他就是老大.
//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
}
}
后续发现加上以上判断还是能执行两次,不加的话三次,最终研究结果使用以下判断更加准确:event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")
评论
8 楼
zhaoshijie
2017-11-16
cfying 写道
大侠,还是加载了两次,怎么解决啊?求。
QQ:342913415
QQ:342913415
看博客的最后一句话
7 楼
xinglianxlxl
2017-10-20
对我有用,非常感谢
6 楼
ykj123
2017-04-20
写一个RefreshEvent 继承 ContextRefreshedEvent,
@Component
public class RefreshListener implements ApplicationListener<RefreshEvent>{
@Override
public void onApplicationEvent(final RefreshEvent event) {
System.out.println("ContextRefreshedEvent 测试!" );
}
}
这样就可以保证一次。
@Component
public class RefreshListener implements ApplicationListener<RefreshEvent>{
@Override
public void onApplicationEvent(final RefreshEvent event) {
System.out.println("ContextRefreshedEvent 测试!" );
}
}
这样就可以保证一次。
5 楼
青春如风铃
2017-04-18
加一个标记变量,参考这篇文章,http://josh-persistence.iteye.com/blog/1988226
4 楼
hwb13138song
2017-01-11
为毛我这只有一次
3 楼
feiwanglantian
2016-07-09
在初始化的时候,加载了dao时会报空指针,这是为什么呢,大神
2 楼
cfying
2015-03-21
cfying 写道
大侠,还是加载了两次,怎么解决啊?求跪。
QQ:342913415
QQ:342913415
1 楼
cfying
2015-03-21
大侠,还是加载了两次,怎么解决啊?求。
QQ:342913415
QQ:342913415
发表评论
-
Dubbo Main启动方式浅析
2015-05-27 13:54 15403关键字:Dubbo Main启动方式浅析 服务容器是一个s ... -
spring中的数据源配置信息加密方案
2014-10-22 10:36 1031关键字:spring中的数据源配置信息加密方案 附件是完整的 ... -
基于Spring可扩展Schema提供自定义配置支持
2014-07-21 18:38 955关键字:基于Spring可扩展Schema提供自定义配置支持 ... -
spring 自定义注解实现(MVC层)
2014-07-21 17:32 831关键字:spring 自定义注解实现(MVC层) 前言 在 ... -
Spring MVC 单元测试
2013-12-10 17:56 7939关键字:Spring MVC 单元测试 下面一步一步带领大家 ... -
优化程序之前,可用Jamon来监测你的Spring应用
2013-11-28 23:52 2007关键字:优化程序之前,可用Jamon来监测你的Spring应 ... -
Java MVC框架性能比较 jsp、struts1、struts2、springmvc3
2013-10-30 17:41 2243关键字:Java MVC框架性能比较 jsp、struts1 ... -
spring JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器)
2013-03-26 15:07 9159关键字:spring JTA集成JOTM或Atomikos配置 ... -
mongodb spring支持
2012-12-17 10:11 987关键字:mongodb spring支持 相关链接: ... -
spring aop实现日志功能
2012-11-22 14:48 3296关键字 :sping aop实现日志功能 附件是spin ... -
Spring + CXF + 注解方式(webService)
2012-11-02 11:34 6732关键字:Spring + CXF + 注解方式(webS ... -
Spring + Xfire + 注解方式
2012-11-01 15:08 1132关键字:Spring 与集成 Xfire 注解方式( Spr ... -
spring rest mvc使用RestTemplate调用
2012-11-01 12:25 70319关键字:RestTemplate使用 可参考博客: ... -
java面向切面编程
2011-09-24 15:37 2302关键字:java面向切面编程 原理简述:java面向切面编 ... -
SpringMVC添加支持Http的delete、put请求
2011-06-02 23:28 3369浏览器form表单只支持GET与POST请求,而DEL ... -
springMVC有关RESTfull
2011-06-02 22:58 5286关键字:springMVC有关RESTfull Sp ... -
spring文件上传(3.0)
2011-06-02 22:44 2917关键字:Spring CommonsMultipartReso ... -
spring3.0多文件上传例子
2011-06-02 22:43 6641关键字:spring3.0多文件 ... -
spring3.0 mvc rest 注解之@
2011-05-16 22:17 3508关键字:spring mvc rest 注解之@ 本文转自h ... -
spring3.0 mvc和rest入门例子
2011-05-07 19:22 2836关键字:spring3.0 mvc和rest入门例子 现在 ...
相关推荐
spring容器启动和关闭时事件监听;spring容器启动和关闭时事件监听;spring容器启动和关闭时事件监听
主要介绍了详解spring boot容器加载完后执行特定操作,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring容器 .ppt Spring容器 .ppt
基于接口、抽象类实现不停机动态调整代码的目的,将修改后的源码文件放置于指定目录下,读取文件后执行动态编译方法,即可将该类重新加载,新的类可以在Spring容器从新注册,且仅在当前窗口生效。如果重启了服务或...
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
本文档是很专业的 Spring动态加载配置文件,实现热部署方案。
常用的获取spring容器的方法
Spring容器的通俗理解及简单写法 手动写一个非常粗犷且简单的IOC容器。 用到了java的一些基础知识,比如说读取文件资源,反射,自定义注解的实现及使用等
实现将一个接口动态代理,并将该代理对象在spring容器初始化完成前注册到spring容器中。实现可以通过@Autowired等注释或其他方法从spring容器中获取该代理对象
主要介绍了Spring Boot容器加载时执行特定操作及spring内置的事件,需要的朋友可以参考下
Spring源代码解析(一):IOC容器.doc
Spring Bean 加载顺序 实验小例子
Spring 容器后处理器
spring Ioc容器配置 IOC容器数据源配置 <!-- 配置数据源 --> destroy-method="close"> <value>org.gjt.mm.mysql.Driver <value>jdbc:mysql://localhost:3306/demo <value>root ...
他们都可代表Spring容器,Spring容器是生成Bean实例的工厂,并且管理容器中的Bean。 Bean是Spring管理的基本单位,在基于Spring的Java EE应用中,所有的组件都被当成Bean处理,包括数据源、Hibernate的...
NULL 博文链接:https://huangminwen.iteye.com/blog/1874200
spring动态向容器中添加bean和删除指定bean,不需要重启应用
spring容器学习笔记
spring简单实例,一看就懂,介绍spring基本配置
使用了dom4j相关包,spring通过反射机制实现bean的创建等,完成bean的管理