Spring 中 ApplicationContextAware使用与理解

 

接口的作用

       当一个类实现了这个接口(ApplicationContextAware)之后,Aware接口的Bean在被初始之后,可以取得一些相对应的资源,这个类可以直接获取spring 配置文件中 所有引用(注入)到的bean对象。

项目中的用法

1.因为spring要建立属于自己的容器,就必须要加载自己的配置文件。这个时候,需要注册ContextLoaderListener或者这个类的子类。在web.xml中配置:

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

这个配置只会读取默认路径下的application.xml配置文件,如果需要读取特定路径下的配置文件,需要在web.xml中添加:

contextConfigLocation:classpath:spring-config.xml,

classpath:spring-config.xml,classpath:spring-cache.xml,classpath:spring-shiro.xml

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/spring-*.xml
        </param-value>
    </context-param>

2.创建一个类实现 这个接口ApplicationContextAware, 在该类中定义ApplicationContext静态变量,ApplicationContext 的getBean()方法可以获得Spring容器中已初始化的bean。完整代码如下:

public class SpringContextHolder implements ApplicationContextAware {

    /**
     * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicationContext.
     */
    private static ApplicationContext applicationContext;

    /**
     * 取得存储在静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        checkApplicationContext();
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        return (T) applicationContext.getBeansOfType(clazz);
    }

    /**
     * 清除applicationContext静态变量.
     */
    public static void cleanApplicationContext() {
        applicationContext = null;
    }


    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicationContext未注入,请在applicationContext.xml中定义SpringContextHolder");
        }
    }

}

3.创建完了之后 ,需要在spring的配置文件中,注册方法类SpringContextHolder

SpringContextHolder其实是一个bean,之所以类SpringContextHolder能够灵活自如地获取ApplicationContext,就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以需要通过注册方法类SpringContextHolder的方式告知spring有这样子一个类的存在。SpringContextHolder 的注册和一般的扫描注册一样。

然后就可以在其他类中调用了:DictionaryBuilder是自己写的一个类,可以通过SpringUtils获得

DictionaryServer server = (DictionaryServer )SpringContextHolder.getApplicationContext().getBean("dictionaryServer");

参考链接:https://blog.csdn.net/bailinbbc/article/details/76446594

               https://blog.csdn.net/weixin_41558061/article/details/80839131

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值