项目用到了ApplicationContextAware,通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。
我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。
使用方法如下:
1.实现ApplicationContextAware接口:
import org.apache.commons.lang.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); //取得存储在静态变量中的ApplicationContext. public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } //从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } //从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } //清除SpringContextHolder中的ApplicationContext为Null. public static void clearHolder() { logger.debug("clean SpringContextHolder ApplicationContext:" + applicationContext); applicationContext = null; } //实现ApplicationContextAware接口, 注入Context到静态变量中. @Override public void setApplicationContext(ApplicationContext applicationContext) { logger.debug("inject ApplicationContext to SpringContextHolder:{}", applicationContext); if (SpringContextHolder.applicationContext != null) { logger.warn("SpringContextHolder ApplicationContext covered, original ApplicationContext :" + SpringContextHolder.applicationContext); } SpringContextHolder.applicationContext = applicationContext; } //实现DisposableBean接口, 在Context关闭时清理静态变量. @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } //检查ApplicationContext不为空. private static void assertContextInjected() { Validate.notNull(applicationContext != null, "applicaitonContext attribute not injected , applicationContext.xml assign SpringContextHolder."); } }
2.在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法:
<bean id="springContextHolder" class="com.qing.utils.SpringContextHolder" />
在项目中即可通过这个SpringContextHolder调用getBean()方法得到Spring容器中的对象了。
3. 这里同时也实现了DisposableBean 接口,主要是为了实现destroy()方法。
相关推荐
springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源
1.继承ApplicationContextAware 2.代码如下 3.记得要把这个工具类加入到Spring的扫描路径下,这样才能在初始化时注入 4.使用方法
com-spring-ioc-demo:源码主要是学习Spring IOC的原理,以及对Bean的注册及控制,主要运用以下类对Spring进行扩展学习:BeanPostProcessor,BeanFactoryAware,BeanNameAware,ApplicationContextAware,FactoryBean...
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
通过aware接口,可以对spring相应资源(可能包含相关核心资源)进行操作(一定要慎重) 首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递...
通过设定jvm的spring.profiles.active参数 web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器...
Spring boot 基础 1 1.什么是spring boot 1 2.spring boot的优点 2 3.环境搭建 3 3.1 添加依赖 3 3.2 创建目录和配置文件 4 3.3 创建启动类 5 3.4 案例演示 5 4 . 配置介绍 6 4.1 Spring boot 配置文件 6 4.2 ...
第一部分 Spring framework 概述..5 第1 章 Spring Framework 介绍..6 1.1 依赖注入和控制反转.6 1.2 模块6 1.2.1 核心容器.7 1.2.2 数据访问/ 整合..7 1.2.3 Web ..8 1.2.4 AOP...
ApplicationContextAware ApplicationContextAwareProcessor ApplicationContextException ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ApplicationEventPublisherAware ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
Spring学习 介绍 Spring轻松学习demo 1.弹簧IOC容器 1.弹簧注入方式 set方法注入 构造方法注入 2.Bean的装配 1.豆的作用域 单例单例获取 原型每次请求都创建实例 2.豆的生命周期 定义,初始化,使用,销毁 待补充......
(5)如果Bean实现了ApplicationContextAware接口,则Spring调用setApplicationContext()方法传入当前ApplicationContext实例的引用。 (6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法...
自定义标签中@Autowired的属性...1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加<bean id="" class="com............SpringContext"> 3.使用SpingContext.getBean("bean名");获取
主要介绍了Spring注解@Resource和@Autowired区别对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
异常时在监视终端上报警,同时发送报警SMS至管理员手机的功能。 */ public class LoginAction implements ApplicationContextAware { private ApplicationContext applicationContext; public void ...
为何@Autowired可以注入ApplicationContext, 一般来说,我们可以通过实现ApplicationContextAware接口来获取ApplicationContext的引用。但是根据官方文档,发现也可以通过 @Autowired来注入ApplicationContext,这...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。