配置springmvc在其他类中(spring容器外)获取注入bean
2016-09-20 17:43
549 查看
学习https://github.com/thinkgem/jeesite
今天在写JedisUtils的时候要注入JedisPool,而这个属性被设置为static,@Resource和@Autowired都不可以注入,因为spring不能为静态变量依赖注入。因此需要额外的方法获取spring管理的bean。本文即SpringContextHolder:
今天在写JedisUtils的时候要注入JedisPool,而这个属性被设置为static,@Resource和@Autowired都不可以注入,因为spring不能为静态变量依赖注入。因此需要额外的方法获取spring管理的bean。本文即SpringContextHolder:
1 package com.demo.common.utils; 2 3 import org.apache.commons.lang3.Validate; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import org.springframework.beans.BeansException; 7 import org.springframework.beans.factory.DisposableBean; 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.ApplicationContextAware; 10 import org.springframework.context.annotation.Lazy; 11 import org.springframework.stereotype.Service; 12 13 /** 14 * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. 15 * Created by Administrator on 2016/2/23. 16 */ 17 @Service 18 @Lazy(false) 19 public class SpringContextHolder implements ApplicationContextAware ,DisposableBean { 20 private static ApplicationContext applicationContext = null; 21 private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); 22 23 /** 24 * 去的存储在静态变量中的ApplicationContext 25 * @return 26 */ 27 public static ApplicationContext getApplicationContext(){ 28 assertContextInjected(); 29 return applicationContext; 30 } 31 32 /** 33 * 从静态变量applicationContext中去的Bean,自动转型为所复制对象的类型 34 * @param name 35 * @param <T> 36 * @return 37 */ 38 public static <T> T getBean(String name){ 39 assertContextInjected(); 40 return (T)applicationContext.getBean(name); 41 } 42 43 /** 44 * 从静态变量applicationContext中去的Bean,自动转型为所复制对象的类型 45 * @param requiredType 46 * @param <T> 47 * @return 48 */ 49 public static <T> T getBean(Class<T> requiredType){ 50 assertContextInjected(); 51 return (T)applicationContext.getBean(requiredType); 52 } 53 54 /** 55 * 清楚SpringContextHolder中的ApplicationContext为Null 56 */ 57 public static void clearHolder(){ 58 if(logger.isDebugEnabled()){ 59 logger.debug("清楚SpringContextHolder中的ApplicationContext:"+applicationContext); 60 } 61 applicationContext = null; 62 } 63 64 65 /** 66 * 检查ApplicationContext不为空 67 */ 68 private static void assertContextInjected() { 69 Validate.validState(applicationContext!=null,"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); 70 } 71 72 /** 73 * 实现ApplicationContextAware接口,注入Context到静态变量 74 * @param applicationContext 75 * @throws BeansException 76 */ 77 @Override 78 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 79 SpringContextHolder.applicationContext = applicationContext; 80 } 81 82 /** 83 * 实现DisposableBean接口,在Context关闭时清理静态变量 84 * @throws Exception 85 */ 86 @Override 87 public void destroy() throws Exception { 88 SpringContextHolder.clearHolder(); 89 } 90 }
相关文章推荐
- 配置springmvc在其他类中(spring容器外)获取注入bean
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例
- spring整合的项目中监听器Listener如何获取注入的bean(监听器获取Spring配置文件)
- Spring (二)依赖注入原理 , bean 作用域, 其他配置
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
- Java获取Tomcat启动中的spring管理的容器注入的实体,用来获取相应的bean
- java 从spring容器中获取注入的bean对象
- 关于SpringBoot获取IOC容器中注入的Bean(推荐)
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
- Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
- Spring 学习笔记(4)—— Bean 的基本配置、依赖注入(DI)、注入参数引用其他Bean
- 获取spring容器,以访问容器中定义的其他bean
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式
- 获取Spring 注入容器、(注解的也可以获取bean)
- Spring通过容器获取配置对象及事件注入(学习笔记二)
- 获取spring容器,以访问容器中定义的其他bean
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- spring开发_注入其他Bean的方法返回值_MethodInvokingFactoryBean
- Spring动态获取IoC容器中管理的Bean