`
zhanshi258
  • 浏览: 47912 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

通过ApplicationContextAware加载Spring上下文环境

 
阅读更多

项目用到了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()方法。

分享到:
评论

相关推荐

    springmvc spring 两套上下文问题

    两套上下文之间的通信是通过ApplicationContextAware接口来实现的。如果某个Bean需要访问到Spring的全局ApplicationContext,它可以实现这个接口,Spring会在初始化时自动注入ApplicationContext实例。这样,...

    Spring特性——Aware感知特性

    在Spring框架中,Aware接口系列是其核心特性之一,它为Spring容器提供了向bean注入上下文信息的能力。这些接口让bean能够感知到Spring容器的存在,从而获取必要的服务或配置信息。下面我们将深入探讨Spring的Aware...

    Spring中关于Bean的管理的课件

    4. **Spring的应用上下文(ApplicationContext)**:ApplicationContext是Spring的主要接口之一,它提供了获取Bean、处理消息和事件等功能,是Spring应用中的主要入口点。 5. **构造注入(constructor injection)*...

    Spring Reference Core Technologies

    这些接口允许Bean访问上下文信息或自己的名称。 - **1.6.3 其他Aware接口** Spring还提供了一系列其他Aware接口,可以帮助Bean更好地融入Spring环境。 #### 六、Bean定义继承 通过继承Bean定义,可以复用现有...

    详解如何在低版本的Spring中快速实现类似自动配置的功能

    2. **XxxAware接口**:如ApplicationContextAware和BeanFactoryAware,可以让我们获取到ApplicationContext或BeanFactory,从而在Bean中直接访问到整个应用上下文或Bean工厂,进行更深入的定制。 3. **@Autowired...

    spring相关面试题.docx

    - **ApplicationContext**:扩展了BeanFactory,除了基本功能外,还支持资源加载、事件传播、国际化等功能,是Spring应用中的主要上下文接口。 - **BeanPostProcessor**:提供Bean实例化前后的处理能力,允许...

    spring-core.pdf

    - **1.6.2 ApplicationContextAware与BeanNameAware**: 提供了一些接口让Bean能够知道自身所在的上下文环境和名称。 - **1.6.3 其他Aware接口**: Spring还提供了其他的一些Aware接口,如`EnvironmentAware`等,使得...

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件2

    但是,如果你有多个XML配置文件需要加载,并且它们分布在不同的路径下,可以创建一个自定义的`BeanDefinitionReader`,并通过`ApplicationContextInitializer`来初始化应用上下文。以下是一个示例: ```java import...

    spring监听器共20页.pdf.zip

    - `ContextStartedEvent`: 应用程序上下文启动时触发。 - `ContextStoppedEvent`: 应用程序上下文停止时触发。 - `ContextClosedEvent`: 应用程序上下文关闭时触发。 - `ApplicationEvent`: 这是所有自定义事件...

    spring.net reference

    5. **Application 作用域**:在整个 Web 应用程序的上下文中有效。 #### 十、类型转换 Spring.NET 支持自动类型转换,可以方便地处理不同类型的转换需求。 1. **枚举类型转换**:为枚举类型提供了内置的支持。 2....

    spring中通过ApplicationContext getBean获取注入对象的方法实例

    在Spring框架中,`ApplicationContext` 是一个非常重要的接口,它提供了对整个应用上下文的访问,包括所有已初始化的Bean以及它们之间的依赖关系。通过`ApplicationContext`,我们可以获取到Spring容器管理的所有...

    Spring源码学习八:常用的扩展接口详解1

    实现这个接口的类能够在Bean加载过程中获取到Spring应用上下文`ApplicationContext`,从而可以访问容器中的其他Bean和服务,如读取配置信息、发布事件等。 4. **BeanFactoryAware接口**: `BeanFactoryAware`接口...

    5. 多个service实现同一个接口 或者抽象类 1

    `ApplicationContextAware`接口使类能够获取Spring应用上下文,从而可以访问所有bean。`BeanPostProcessor`接口则允许我们在bean初始化前后执行自定义逻辑,例如调整bean的加载顺序或进行额外的处理。 `...

    Spring中如何动态注入Bean实例教程

    // Spring 应用上下文环境 private static ApplicationContext applicationContext; / * 实现 ApplicationContextAware 接口的回调方法,设置上下文环境 * * @param applicationContext */ public void ...

    Spring入门

    - **定义**:是Spring框架中的顶级接口,提供了访问应用上下文的所有功能。 - **作用**:用于获取bean实例、发布事件、获取环境配置信息等。 - **示例**:通过`ClassPathXmlApplicationContext`加载XML配置文件,并...

    监听器获取Spring配置文件的方法

    这种方法是直接使用`ClassPathXmlApplicationContext`来加载配置文件,然后从上下文(ApplicationContext)中获取bean。例如: ```java ApplicationContext context = new ClassPathXmlApplicationContext("beans...

    Spring中多配置文件及引用其他bean的方式

    这会让Spring在启动时自动加载多个配置文件,将它们合并为一个配置上下文。 总结来说,Spring中的多配置文件管理和bean引用机制是其核心特性,提供了强大的灵活性和可扩展性。通过合理地组织和利用这些机制,开发者...

    Springboot实现多线程注入bean的工具类操作

    `ApplicationContextProvider`实现了`ApplicationContextAware`接口,这个接口是Spring提供的一种机制,使得我们的类能够感知到Spring应用上下文。当Spring容器初始化时,会自动调用`setApplicationContext`方法,将...

Global site tag (gtag.js) - Google Analytics