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引 ...
随机推荐
- 关于a标签的target属性
超级链接a的target属性已经是不被新规范支持了,其值有四个保留字: 1._blank <a href="document.html" target=" ...
- linux包之procps之pmap命令
名称: pmap - report memory map of a process(查看进程的内存映像信息)用法 pmap [ -x | -d ] [ -q ] pids... ...
- 透明度兼容性(ie8以上)
转载:http://www.cnblogs.com/PeunZhang/p/4089894.html demo代码:文件中,背景透明,文字不透明的研究和使用.zip
- 决策树模型组合之(在线)随机森林与GBDT
前言: 决策树这种算法有着很多良好的特性,比如说训练时间复杂度较低,预测的过程比较快速,模型容易展示(容易将得到的决策树做成图片展示出来)等.但是同时, 单决策树又有一些不好的地方,比如说容易over ...
- RMAN备份数据库与恢复数据库(整库)
1 准备 2 1.1 检查数据库归档状态 2 1.2 RMAN登陆目标 2 2 备份全库 2 2.1 创建备份数据存储目录 2 2.2 RMAN备份全库 2 2.3 试验(备份后,改变数据) 5 2. ...
- MySQL数据库表中有usage字段名后的后果
一个很奇怪的42000的错误,折腾了我一晚上.... 我的系统是Spring + SpringMVC + MyBatis结构, 数据库的mapper以及model等文件都是用MyBatisGenera ...
- session.flush加锁测试.
测试结论 1 session.flush (用于提交SQL执行计划. hibernate会给数据库加锁, 执行效果等同于select for update的锁级别.如果是oracle 默认为lock ...
- IOS开发-UITextField代理常用的方法总结
1.//当用户全部清空的时候的时候 会调用 -(BOOL)textFieldShouldClear:(UITextField *)textField: 2.//可以得到用户输入的字符 -(BOOL)t ...
- [转] matlab figure最大化
http://blog.163.com/yinhexiwen@126/blog/static/6404826620122942057214/ % figure 窗口最大化,坐标轴也随着窗口变大而相应变 ...
- WSUS目录本地迁移
生产环境中有一台win2003 server,安装了Microsoft Windows Server Update Services 3.0,作为所有windows server的内网补丁更新服务器, ...