星空网 > 软件开发 > Java

转:如何取得Spring管理的bean

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

1、servlet方式加载时,
web.

  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.</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配置的值,注意适时修改。

Java代码

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

 

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

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

 【jsp/servlet】可以这样取得

Java代码

  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" />  

 

Java代码

    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. /** 
    11. * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. 
    12. */  
    13. public void setApplicationContext(ApplicationContext applicationContext) {  
    14. SpringContextHolder.applicationContext = applicationContext; // NOSONAR  
    15. }  
    16.   
    17. /** 
    18. * 取得存储在静态变量中的ApplicationContext. 
    19. */  
    20. public static ApplicationContext getApplicationContext() {  
    21. checkApplicationContext();  
    22. return applicationContext;  
    23. }  
    24.   
    25. /** 
    26. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
    27. */  
    28. @SuppressWarnings("unchecked")  
    29. public static <T> T getBean(String name) {  
    30. checkApplicationContext();  
    31. return (T) applicationContext.getBean(name);  
    32. }  
    33.   
    34. /** 
    35. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
    36. */  
    37. @SuppressWarnings("unchecked")  
    38. public static <T> T getBean(Class<T> clazz) {  
    39. checkApplicationContext();  
    40. return (T) applicationContext.getBeansOfType(clazz);  
    41. }  
    42.   
    43. /** 
    44. * 清除applicationContext静态变量. 
    45. */  
    46. public static void cleanApplicationContext() {  
    47. applicationContext = null;  
    48. }  
    49.   
    50. private static void checkApplicationContext() {  
    51. if (applicationContext == null) {  
    52. throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.
    53. }  
    54. }  
    55. }  




原标题:转:如何取得Spring管理的bean

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊卖家们到底该怎么选品呢?:https://www.ikjzd.com/articles/1578261564353126401
卖家注意!美国著名品牌Supreme近年频繁发案!代理律所GBC持续维权,原告已申请延长TRO禁令!:https://www.ikjzd.com/articles/1578357941242404865
德国VAT如何申请?:https://www.ikjzd.com/articles/15784
注意了!这些亚马逊卖家千万不要开启“假期模式”!:https://www.ikjzd.com/articles/15785
亚马逊计划招聘15万人!外贸旺季即将到来!中国玩具出口世界第一:https://www.ikjzd.com/articles/1578543976882270210
一个卖家案例,一番运营建议:https://www.ikjzd.com/articles/1578549310372159490
九月初新疆旅游服装搭配(新疆游玩必备衣服清单):https://www.vstour.cn/a/408257.html
黄果树瀑布景区景点 - 黄果树瀑布景区景点分布图:https://www.vstour.cn/a/408258.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流