在项目中经常遇见需要在Listener中或者工具中使用Spring容器中的bean实例,由于bean不能在stataic的类中使用。

介绍一种方式:

public class SpringTool {

    public static Object getObjectFromApplication(HttpSession session,String beanName){
ServletContext servletContext= session.getServletContext();
//通过WebApplicationContextUtils 得到Spring容器的实例。
WebApplicationContext application=WebApplicationContextUtils.getWebApplicationContext(servletContext);
//返回Bean的实例。
return application.getBean(beanName);
}
}

 

使用举例:

LoginService loginService=(LoginService) SpringTool.getObjectFromApplication(session, "loginService");

在listener或者工具中使用spring容器中的bean实例的更多相关文章

  1. 如何在自定义Listener(监听器)中使用Spring容器管理的bean

    正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...

  2. Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)

    在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...

  3. 普通类中获取spring容器中的javabean对象

    spring提供了一系列的*Aware 接口,用于获取相应的对象,进行一系列的对象设置操作,此处实现ApplicationContextAware来获取ApplicationContext. 其他Aw ...

  4. SpringBoot中实现Spring容器中注入类型相同但名不同Bean

    @Bean(autowire = Autowire.BY_NAME,value = "kaptchaProducer") public Producer kaptchaProduc ...

  5. 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean

    1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...

  6. 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;

    7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...

  7. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  8. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  9. SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)

    上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...

随机推荐

  1. Objective-C基本数据类型、表达式和语句

    一.基本数据类型 1.一般的计算机语言在定义变量的时候,需要提供给两个内容:类型和名字. 比如:int myClassID; 2.在Xcode中,无论你使用的是GCC编译器还是LLVM编译器,如果我们 ...

  2. CSS中的content和attr的用法

    问题缘起 在闲看别人网站时发现了这样的代码 <span class="hamburger icon" data-icon="C"> .icon:be ...

  3. WordPress 主题开发 - (七) 让主题更安全 待翻译

    We're just about ready to start building our theme's template files. Before we do this, however, it' ...

  4. Lambda Grinding Miller From Zenith

    data = """ The Basic Things About Grinding Mill A grinding mill is a unit operation d ...

  5. 【转】代码编辑器(一)-TSynCompletionProposal用法

    注意,本系列均转载自http://blog.163.com/zom1995@126/ 网上有人给我一个SynEdit这个东西,因为我很喜欢自己编个代码编辑器,但要是用Delphi直接弄的,就我现在这样 ...

  6. Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值

    感谢原作者:破剑冰-Oracle中Clob类型处理解析 上一篇分析:ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 最近为Clob字段在插入数据时发现当字符的字节数(一个半角字符一 ...

  7. NOJ1018-深度遍历二叉树

    题目要求很简单,前中后序遍历一棵二叉树.坑爹的是这道题的输入数据和测试数据压根不一样,找了好久原因,去讨论区看见有别人发的测试样例,修改了一下就AC了 测试样例是这个:DEH##FJ##G#CK### ...

  8. 菜鸟学习Spring——初识Spring

    一.概念. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Develop ...

  9. Transact-SQL 语句

    当流程控制语句必须执行一个包含两条或两条以上Transact-SQL语句块时,可以使用BEGIN...END语句进行控制 use testDB; go declare @name varchar() ...

  10. How to create QTP Shared Object Repository

    How to create QTP Shared Object Repository To create a shared object repository by performing follow ...