如何取得Spring管理的bean (请用第3种方法)

  如何取得Spring管理的bean  (请用第3种方法):

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

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

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

 

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

Xml代码
  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两种方法并不通用,可以抛弃了。
在配置文件中加入:

Xml代码
  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.xml中定义SpringContextHolder");  
  53. }  
  54. }  
  55. }  
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
@Bean注解是Spring Framework中的一个注解,用于在Java配置文件中声明bean。通过使用@Bean注解,我们可以将第三方库中的bean加入到Spring应用程序上下文中进行管理。 具体来说,我们可以在@Configuration类中使用@Bean注解来声明一个方法,该方法返回一个对象,Spring容器会将该对象作为bean添加到应用程序上下文中。这个方法可以返回任何一个Java对象,包括第三方库中的对象。 例如,我们想要将第三方库中的一个类的实例化对象添加到Spring应用程序上下文中,可以按照以下步骤操作: 1. 导入第三方库的jar包到项目中; 2. 在@Configuration类中声明一个@Bean注解的方法,该方法返回第三方库中的类的实例化对象; 3. 在需要使用该对象的地方注入该bean。 例如,假设我们要使用Google Guava库中的CacheBuilder类创建一个缓存对象并将其添加到Spring应用程序上下文中,我们可以编写以下代码: ```java @Configuration public class AppConfig { @Bean public Cache<String, Object> cache() { return CacheBuilder.newBuilder() .expireAfterWrite(10, TimeUnit.SECONDS) .maximumSize(1000) .build(); } } ``` 然后,在需要使用该缓存的地方,我们可以将其注入到目标类中: ```java @Service public class MyService { @Autowired private Cache<String, Object> cache; // ... } ``` 这样,我们就可以使用Google Guava库中的CacheBuilder类创建的缓存对象,并将其纳入Spring应用程序上下文的管理之下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值