简单的 Spring Boot 应用程序中使用了哪个 ApplicationContext 实现?

我学过:

“应用程序上下文”的三个常用实现是 -

FileSystemXmlApplicationContext− 此容器从 XML 文件加载 bean 的定义。这里需要将 XML bean 配置文件的完整路径提供给构造函数。

ClassPathXmlApplicationContext− 此容器从 XML 文件加载 bean 的定义。这里不需要提供 XML 文件的完整路径,但需要正确设置 CLASSPATH,因为这个容器会在 CLASSPATH 中查找 bean 配置 XML 文件。

WebXmlApplicationContext− 此容器从 Web 应用程序中加载包含所有 bean 定义的 XML 文件。

那么 Spring Boot 怎么样呢?我已经阅读了一些文章,如何获取 ApplicationContext:

> public class A implements ApplicationContextAware {

>     private ApplicationContext applicationContext;

>     @Override

>     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

>         this.applicationContext = applicationContext;

>     }

> }

但是在 Spring Boot 中使用了 Application Context 的哪个实现呢?


慕尼黑的夜晚无繁华
浏览 288回答 3
3回答

墨色风雨

Spring Boot 应用程序的入口点是一个SpringApplication对象。您可以通过其setApplicationContextClass(Class)方法选择要使用的实现。它的 javadoc 状态ApplicationContext设置将要创建的 Spring 的类型。如果未指定,则默认DEFAULT_SERVLET_WEB_CONTEXT_CLASS为基于 Web 的应用程序AnnotationConfigApplicationContext或非基于 Web 的应用程序。如果您不使用该方法,它会列出默认值,即。org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext用于基于 Web 的应用程序和org.springframework.context.annotation.AnnotationConfigApplicationContext用于非基于 Web 的应用程序。反应式网络环境也有一个默认值。

富国沪深

Spring boot 创建了一种与嵌入式服务器集成的新型ApplicationContext调用。WebServerApplicationContext它进一步分为两种实现类别,一种用于 Servlet 堆栈(ServletWebServerApplicationContext),另一种用于 Webflux 反应式堆栈(ReactiveWebServerApplicationContext)。这个新上下文的显着区别在于它将在引导上下文期间创建和管理嵌入式服务器。所以你可以看到这个上下文将返回一个WebServer具有以下接口的。public interface WebServer {    void start() throws WebServerException;    void stop() throws WebServerException;    int getPort();}WebServer可以是、、或等JettyWebServer,具体取决于在类路径中找到的嵌入式服务器。NettyWebServerTomcatWebServerUndertowWebServer

ibeautiful

这取决于您的类路径,例如,如果您正在创建一个 Web 应用程序,它可能是AnnotationConfigWebApplicationContext获取 ApplicationContext 并在其上应用 getClass() 方法来检查它的实现。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java