温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot如何获取上下文ApplicationContext

发布时间:2021-11-24 19:43:59 来源:亿速云 阅读:181 作者:柒染 栏目:开发技术

本篇文章给大家分享的是有关Springboot如何获取上下文ApplicationContext,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Springboot获取上下文ApplicationContext

在项目中遇到了一个场景,就是通过获得上下文然后获取特定的bean。在此遇到了不小的坑,故留下这个篇文章,做个记录。

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
@Component
public class SpringContextUtil implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;
 
    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
 
    /**
     * 获取applicationContext
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    /**
     * 通过name获取 Bean.
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }
 
    /**
     * 通过class获取Bean.
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }
 
    /**
     * 通过name,以及Clazz返回指定的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

看上面的代码,可以看到在工具类中一开始是声明了一个ApplicationContext类型的静态变量,但是由于静态变量是不能被Spring容器管理的,一开始用正常的getter和setter方法不能获取到特定的bean,实践证明,需要在此变量的setter方法上加上@Autowired注解,并且去除setter方法中的static关键字。才可实现特定bean的注入。

springboot的应用上下文

springboot上下文有两个

  • ServletWebServerApplicationContext

  • AnnotationConfigServletWebServerApplicationContext(继承上面)

ServletWebServerApplicationContext

该类属于spring-boot-2.1.1Release.jar中,是自springboot诞生就衍生出来的,是spring框架的应用上下文Application的子类。

多说无益,show me code

扩展的功能

首先让我们来看一下,这个类到底做了什么,有什么存在的价值?

private volatile WebServer webServer;
 @Override
 protected void onRefresh() {
  super.onRefresh();
  try {
   createWebServer();
  }
  catch (Throwable ex) {
   throw new ApplicationContextException("Unable to start web server", ex);
  }
 }

在此类中有个WebServer成员变量,让我们用脚趾头想一下也应该可以知道,这其实就是web服务对象,也基本上可以猜测就是跟tomcat相关了(当然也可以是其他web服务器,如jetty)

然后我们又发现了onRefresh方法,相信我们并不陌生,这就是spring核心refresh方法的中一个钩子方法(即表明此时已经加载所有配置bean),进行WebServer对象的创建

 @Override
 protected void finishRefresh() {
  super.finishRefresh();
  WebServer webServer = startWebServer();
  if (webServer != null) {
   publishEvent(new ServletWebServerInitializedEvent(webServer, this));
  }
 }

我们又发现该类存在finishRefresh,仔细想一下,这个也是spring核心#refresh方法中的一个钩子方法(不过这个特别,因为该方法是refresh方法中的最后一步,即会去实例化spring容器中的所有beandefinition对象)

首先赞一个,这个很巧妙,调用了super.finishRefresh() ,并没有丢弃父类的逻辑功能(这点在多态中,我相信还是会有人犯错,本来是扩展功能,但是直接重写,丢弃了父类的方法,当然spring框架开发大佬肯定不会犯这种错误,对吧!)

第二点重点来了,就是startWebServer,也就是在spring完成实例化之后,就会去启动web服务。

AnnotationConfigServletWebServerApplicationContext

首先此类是springboot启动运行run()创建ApplicationContext的实现类,不过很可惜,该类并没有很强的实质性扩展。

唯一作用就是拥有了通过注解加载配置类的作用,即和AnnotationConfigApplication一样,只不过springboot的运行启动已经是通过注解加载bean类**

(虽然是鸡肋,不过这也符合spring创建类的一贯风格,就是每个类都是高内聚的,即每个类除了父类的功能之外,还都拥有其他扩展的作用。即使创建出来还没有用到就被遗弃,但仍然不能阻止spring开发大佬创建该类,哈哈)

以上就是Springboot如何获取上下文ApplicationContext,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI