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

标签 java spring spring-boot

我学到了:

The three commonly used implementation of 'Application Context' are −

FileSystemXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.

ClassPathXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.

WebXmlApplicationContext − This container loads the XML file with definitions of all beans from within a web application.

那么 Spring Boot 怎么样?我读过一些文章,如何获取ApplicationContext:

> public class A implements ApplicationContextAware {
> 
>     private ApplicationContext applicationContext;
> 
>     @Override
>     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
>         this.applicationContext = applicationContext;
>     }
> 
> }

但是 Spring Boot 中到底使用了哪种应用程序上下文实现?

最佳答案

Spring Boot 应用程序的入口点是 SpringApplication目的。您可以通过其 setApplicationContextClass(Class) 选择要使用的实现。方法。它的 javadoc 指出

Sets the type of Spring ApplicationContext that will be created. If not specified defaults to DEFAULT_SERVLET_WEB_CONTEXT_CLASS for web based applications or AnnotationConfigApplicationContext for non web based applications.

其中列出了 defaults如果你不使用该方法,即。

org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext

对于基于网络的应用程序和

org.springframework.context.annotation.AnnotationConfigApplicationContext

适用于非基于网络的应用程序。

还有一个default for reactive web environments .

关于java - 简单的 Spring Boot 应用程序中使用了哪种 ApplicationContext 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54331402/

相关文章:

java - 如何在 Spring Boot 中将属性文件值读入字符串集

spring - 是否有任何工具或库可以创建用于 Controller 和测试的模拟数据?

java - 执行某些代码时,我在 android studio 中的应用程序崩溃

java - 如何确保在禁用 JavaScript 的情况下运行网站

java - JAXB、XJC : Generating field to Class Outline

java - 为什么我在一个地方得到 "No unique bean is defined",而在另一个地方却没有?

java - Spring Data Rest 中的关联

java - 使用 SpringBoot CrudRepository 查找一个已知条目

java - 如何在 DateField 中设置默认日期?

java - Maven - 为什么运行多个包的构建会产生不同的结果?