SpringBoot ApplicationContextAware拓展接口怎么使用

开发技术 作者:iii 2024-05-10 23:30:01
这篇文章主要介绍“SpringBoot ApplicationContextAware拓展接口怎么使用”,在日常操作中,相信很多人在SpringBoot ApplicationContextAware拓展接口...

这篇文章主要介绍“SpringBoot ApplicationContextAware拓展接口怎么使用”,在日常操作中,相信很多人在SpringBoot ApplicationContextAware拓展接口怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot ApplicationContextAware拓展接口怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

ApplicationContextAware接口:

public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

首先Aware接口就知道这是springboot扩展给用户使用的,这里提供了方法setApplicationContext,参数就是传递spring容器上下文对象进来,我们可以接收这个上下文对象,我们要想知道获取spring容器上下文ApplicationContext具体有什么作用,这才是扩展接口的目的所在,获取上下文根据上下文的特性做一些事情。

我们来看ApplicationContext对象的方法:

SpringBoot ApplicationContextAware拓展接口怎么使用

来看看AbstractApplicationContext实现类的方法:

    public Object getBean(String name) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(name);}
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(name, requiredType);}
    public Object getBean(String name, Object... args) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(name, args);}
    public <T> T getBean(Class<T> requiredType) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(requiredType);}
    public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(requiredType, args);}
    public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {this.assertBeanFactoryActive();return this.getBeanFactory().getBeanProvider(requiredType);}
    public <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType) {this.assertBeanFactoryActive();return this.getBeanFactory().getBeanProvider(requiredType);}
    public boolean containsBean(String name) {return this.getBeanFactory().containsBean(name);}
    public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {this.assertBeanFactoryActive();return this.getBeanFactory().isSingleton(name);}
    public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {this.assertBeanFactoryActive();return this.getBeanFactory().isPrototype(name);}

这里我们可以发现 getBean()方法很眼熟,因为在最最开始学习spring时没有用spring的脚手架创建项目,我们获取bean的方法通常是classPathContextLoader扫描bean的xml文件解析组成ApplicationCOntext对象,再调用它的getBean方法获取实例bean。

由此可以发现我们主要的应用途径就是使用这个getBean的方法,那么动态的注入bean我们通过很多方法就能实现,所以这里不难想到,静态方法中无法使用注入的bean的问题。

其次我们来复现这个问题,大家来看如下的代码:

public class JsonGetter {
@Resource
private UuidGetter uuidGetter;
public static string Test(){
       return uuidGetter.getUuid();
}
public static JsONobject set0bjectToJsonObject(object data){
       return JsoNobject.parseObject(String.valueof(JsONObject.toJSON(data)));
}
public static JsONObject setStringTO3son0bject(String data) { return JsONObject.parseObject(data);
}

我们发现在静态的Test方法中调用注入的bean直接报错,这里解释一下:归功于类的加载机制与加载顺序,静态属性与静态代码块最先加载(static静态优先),这里加载静态方法是没有bean实例给你用的,自然会报错。

如何解决?我们可以采取Spring获取bean对象时调用getBean方法的思路,在容器加载时将spring容器的上下文进行静态存储:

@Component
@Lazy(value = false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
    /**
     * 将上下文静态设置,在初始化组件时就进行静态上下文的覆盖(这个覆盖是将远spring容器的上下文对象引用加到我们预定设置)
     */
    private static ApplicationContext applicationContext = null;
    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }
    public static  <T> T getBean(Class<T> beanType) {
        assertContextInjected();
        return applicationContext.getBean(beanType);
    }
    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }
    @Override
    public void destroy() {
        applicationContext = null;
    }
    private static void assertContextInjected() {
        Assert.notNull(applicationContext,
                "applicationContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
    }
    public static void pushEvent(ApplicationEvent event){
        assertContextInjected();
        applicationContext.publishEvent(event);
    }
}

这里只需要关注的是静态成员变量ApplicationContext的定义、赋值与验证:

    /**
     * 将上下文静态设置,在初始化组件时就进行静态上下文的覆盖(这个覆盖是将远spring容器的上下文对象引用加到我们预定设置)
     */
    private static ApplicationContext applicationContext = null;

重写扩展接口的方法,实现静态上下文的覆盖:

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

将获取它的方法公有修饰,便于共享:

    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

写到这里还是不明白,这么定义一个组件,将spring上下文对象静态覆盖到底有何作用?

不要慌,我们来看看这个类的这个方法:

public class AppContext {
    static transient ThreadLocal<Map<String, String>> contextMap = new ThreadLocal<>();
    ......省略n行业务代码
    public static void fillLoginContext() {
        DingAppInfo appInfo = SpringContextHolder.getBean(DingAppInfoService.class).findAppInfo(APP_CODE);
        setDingVerifyInfo(appInfo);
        CloudChatAppInfo cloudChatAppInfo = SpringContextHolder.getBean(CloudChatAppInfoService.class).findAppInfo(APP_CODE);
        setCloudChatInfo(cloudChatAppInfo);
    }
    public static void clear() {
        contextMap.remove(); //本地线程的remove方法极其重要,注意每次给它使用之后一定要调用remove清理,防止内存泄露。
    }
}

我们发现上例代码中进行了查库的操作:

DingAppInfo appInfo = SpringContextHolder.getBean(DingAppInfoService.class).findAppInfo(APP_CODE);

到此,关于“SpringBoot ApplicationContextAware拓展接口怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注捷杰建站网站,小编会继续努力为大家带来更多实用的文章!

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_26435.html