spring listener引用spring中bean
在SSH项目开发中,会使用到监听器Listener,并且有时需要在监听器中完成数据库的操作等动作,此时需要在Listener中使用到Spring容器中的Bean。Spring容器本身就是在web.xml中使用listener的方式启动的。想在例如HttpSessionListener中使用依赖注入的方式完成Bean实例的注入,不能完成。
一种解决方案:在HttpSessionListener中通过new的方式得到Spring容器的实例。如下代码:
//通过new的方式得到Spring容器的实例
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
结果是:可以取得Spring的容器,但是是重新生成了一个新的Spring的容器。SSH项目启动的时候已经自动生成了一个Spring的容器,这样就存在了两个Spring的容器。不可取。
最好的解决方案:通过Spring提供的WebApplicationContextUtils 得到Spring容器的实例。代码如下:
public class MySessionListener implements HttpSessionListener {
private Logger logger=Logger.getLogger(MySessionListener.class);
@Override
public void sessionCreated(HttpSessionEvent event) {
logger.debug("新的session的产生!!");
HttpSession session=event.getSession();
session.setAttribute(InitUtil.ISNEWSESSION, "true");
//通过抽象的私有方法得到Spring容器中Bean的实例。
UsersDao userDao=(UsersDao)this.getObjectFromApplication(session.getServletContext(), "usersDaoHibernate");
System.out.println("取得的Dao的实例="+userDao);
}
/**
* 通过WebApplicationContextUtils 得到Spring容器的实例。根据bean的名称返回bean的实例。
* @param servletContext :ServletContext上下文。
* @param beanName :要取得的Spring容器中Bean的名称。
* @return 返回Bean的实例。
*/
private Object getObjectFromApplication(ServletContext servletContext,String beanName){
//通过WebApplicationContextUtils 得到Spring容器的实例。
ApplicationContext application=WebApplicationContextUtils.getWebApplicationContext(servletContext);
//返回Bean的实例。
return application.getBean(beanName);
}
}
本文转载自http://wxinpeng.iteye.com/blog/1317659
spring listener引用spring中bean的更多相关文章
- Spring重点—— IOC 容器中 Bean 的生命周期
一.理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助. 二.在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理. 三.在没有添加后置处理器的情况 ...
- spring BeanFactory及ApplicationContext中Bean的生命周期
spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- 7 -- Spring的基本用法 -- 9...容器中Bean的生命周期
7.9 容器中Bean的生命周期 Spring可以管理singleton作用域的Bean的生命周期,Spring可以精确地知道该Bean何时被创建,何时被初始化完成.容器何时准备销毁该Bean实例. ...
- 复习Spring第一课--Spring的基本知识及使用
关于Spring: spring容器是Spring的核心,该容器负责管理spring中的java组件, ApplicationContext ctx = new ClassPathXmlApplic ...
- Spring中bean的scope详解
如何使用spring的作用域: <bean id="role" class="spring.chapter2.maryGame.Role" scope=& ...
- 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;
7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- Spring配置文件中<bean>标签的scope属性
转自:https://fj-sh-chz.iteye.com/blog/1775149 singleton (默认属性) Spring将Bean放入Spring IOC容器的缓存池中,并将Bean引 ...
随机推荐
- unity shader random number
http://gamedev.stackexchange.com/questions/32681/random-number-hlsl
- USACO/gift1
描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人. 然而,在任何一群朋友中 ...
- HTTP接口功能自动化测试入门
无论是浏览器上运行的Web应用还是移动端的H5应用,都离不开HTTP接口.Web应用通常是分为前后台开发的,后台提供接口调用返回Json对象,前台使用JS框架去加载后台返回的Json.而H5页面动态获 ...
- .NET分布式事务未提交造成6107错误或系统被挂起的问题分析定位
问题描述: 系统中多个功能不定期出现“Unable to get error message (6107) (0).”错误,即分布式事务超时,但报出错误的部分功能根本没有使用分布式事务. 原因分析: ...
- Dynamics CRM 2011 JScript
if (!this.JSON) { this.JSON = {}; } (function () { function f(n) { return n < 10 ? '0' + n : n; } ...
- 如何处理JSON数据中含有双引号
{"quality": "B"A"D"} 实际上要 value.replace("\"","\\\& ...
- Mysql 模糊匹配和转义字符
首先创建一个测试表: insert into test(tt) values('\\\\172.18.28.153'); 现在我想使用模糊匹配,查出以 “\\172” 开头的字符串. 需要使用like ...
- Innodb 表修复(转)
摘要: 突然收到MySQL报警,从库的数据库挂了,一直在不停的重启,打开错误日志,发现有张表坏了.innodb表损坏不能通过repair table 等修复myisam的命令操作.现在记录下 ...
- Android 中Fragment使用
Android 中Fragment使用 public class MainActivity extends Activity { public static String[] array = { &q ...
- nginx ssi 配置小细节(一)
最近工作需要使用nginx的ssi (server side include)技术,在这里,将使用中的一点心得分享一下,也是一种备忘! 首先,nginx的ssi启用很简单,就只有三个最基本的指令: s ...