原文链接:http://blog.csdn.net/a9529lty/article/details/42145545

1、servlet方式加载时,
【web.xml】

  1. <servlet>
  2. <servlet-name>springMVC</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contExtConfigLocation</param-name>
  6. <param-value>classpath*:/springMVC.xml</param-value>
  7. </init-param>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>

spring容器放在ServletContext中的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC
注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。

  1. ServletContext sc=略
  2. WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");

2、listener方式加载时:
【web.xml】

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>/WEB-INF/applicationContext</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7. </listener>

jsp/servlet】可以这样取得

  1. ServletContext context = getServletContext();
  2. WebApplicationContext applicationContext  = WebApplicationContextUtils .getWebApplicationContext(context);

3、通用的方法来了,神器啊,前的  1、2两种方法并不通用,可以抛弃了。
在配置文件中加入:

  1. <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 -->
  2. <bean class="com.xxxxx.SpringContextHolder" lazy-init="false" />
    1. import org.springframework.context.ApplicationContext;
    2. import org.springframework.context.ApplicationContextAware;
    3. /**
    4. * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
    5. *
    6. */
    7. public class SpringContextHolder implements ApplicationContextAware {
    8. private static ApplicationContext applicationContext;
    9. /**
    10. * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
    11. */
    12. public void setApplicationContext(ApplicationContext applicationContext) {
    13. SpringContextHolder.applicationContext = applicationContext; // NOSONAR
    14. }
    15. /**
    16. * 取得存储在静态变量中的ApplicationContext.
    17. */
    18. public static ApplicationContext getApplicationContext() {
    19. checkApplicationContext();
    20. return applicationContext;
    21. }
    22. /**
    23. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    24. */
    25. @SuppressWarnings("unchecked")
    26. public static <T> T getBean(String name) {
    27. checkApplicationContext();
    28. return (T) applicationContext.getBean(name);
    29. }
    30. /**
    31. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    32. */
    33. @SuppressWarnings("unchecked")
    34. public static <T> T getBean(Class<T> clazz) {
    35. checkApplicationContext();
    36. return (T) applicationContext.getBeansOfType(clazz);
    37. }
    38. /**
    39. * 清除applicationContext静态变量.
    40. */
    41. public static void cleanApplicationContext() {
    42. applicationContext = null;
    43. }
    44. private static void checkApplicationContext() {
    45. if (applicationContext == null) {
    46. throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
    47. }
    48. }
    49. }

转:如何取得Spring管理的bean的更多相关文章

  1. 将spring管理的bean使用注解的方式注入到servlet中

    Filter和Servlet中不能直接注解使用spring的bean,因为这两个都是servlet容器维护管理的,当然也有实现方法,如下: 1.创建一个AbstractServlet 抽象类,让你的所 ...

  2. 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean

    BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...

  3. 为何在新线程中使用注解获取不到Spring管理的Bean

    新建的线程类NewThread,在这个类中国使用Spring的注解获取Service,为null 网上已有这种问题的解决方案,但是为何在新线程中使用注解获取不到Spring管理的Bean? 问了老大, ...

  4. 手动获取被spring管理的bean对象工具

       在netty handler开发中,我们无法将spring的依赖注入到Handler中,无法进行数据库的操作,这时候我们就需要手动获取被spring管理的bean对象:    创建一个  imp ...

  5. (转)Spring管理的Bean的生命周期

    http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...

  6. (转)配置Spring管理的bean的作用域

    http://blog.csdn.net/yerenyuan_pku/article/details/52833477 Spring管理的bean的作用域有: singleton 在每个Spring ...

  7. Servlet中获取Spring管理的bean

    描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...

  8. 170630、springboot编程之普通类中调用spring管理的bean对象

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

  9. 如何在线程中获取spring 管理的bean

    转载自:https://my.oschina.net/skyline520/blog/181158?fromerr=GjtR6Wec spring xml中定义 <!--spring 工具类-- ...

  10. SpringContextHolder 静态持有SpringContext的引用(如何取得Spring管理的bean )

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

随机推荐

  1. 我被eclipse的tomcat坑的经历

    奇怪的乱码问题: 1.jsp页面utf-8 2.java代码utf-8 3.数据库编码utf-8 4.tomcat server.xml配置URIEncoding="UTF-8" ...

  2. IDF实验室-简单编程-字符统计 writeup

    题目地址:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=37 网站:http://ctf.idf.cn/gam ...

  3. cocos2d-x CCSpriteBatchNode

    转自:http://www.cnblogs.com/jiackyan/archive/2013/04/14/3019880.html 1.先说下渲染批次:这是游戏引擎中一个比较重要的优化指标,指的是一 ...

  4. 常见排序算法及其java实现

    最近学习了下java,感觉java在基本语法上与C++非常相似.作为练习,我用java实现了冒泡排序.选择排序.插入排序.基尔排序.快速排序.堆排序.计数排序.合并排序. 以下为实现代码: publi ...

  5. Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS

    D. Phillip and Trains Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/ ...

  6. ios开发——实战OC篇&SQLite3的实际应用

    SQLite3的实际应用 前面的文章中介绍了SQlite,并且介绍了他的各种语法及使用方法. 但是没有正在项目中使用特,今天就开始做一个小小的实例,就是使用SQLite3来实现数据库的相应操作并且把他 ...

  7. 学习笔记之Linux Shell脚本教程:30分钟玩转Shell脚本编程

    Linux Shell脚本教程:30分钟玩转Shell脚本编程 http://c.biancheng.net/cpp/shell/

  8. C#_Queue实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Queu ...

  9. if elseif else

    1)if,else if需要接判断表达式,只有在满足条件的时候执行而else语句是在前面条件都不满足的时候才能执行(2) 一个if...else if...else语句中可以有多个else if语句, ...

  10. Javascript学习总结三(Array对象的用法)

    javascript Array对象的常用API 1:concat concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本.举例:var a1 = [ ...