网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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 getBean(String name, Class 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 getBean(Class requiredType) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(requiredType);}
    public  T getBean(Class requiredType, Object... args) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(requiredType, args);}
    public  ObjectProvider getBeanProvider(Class requiredType) {this.assertBeanFactoryActive();return this.getBeanFactory().getBeanProvider(requiredType);}
    public  ObjectProvider 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 getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }
    public static   T getBean(Class 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> 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拓展接口怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


新闻名称:SpringBoot ApplicationContextAware拓展接口怎么使用
文章分享:http://bjjierui.cn/article/pjhech.html

其他资讯