此版本仍在开发中,尚未视为稳定版。如需最新稳定版本,请使用 Spring Boot 4.0.4spring-doc.cadn.net.cn

Spring Boot 应用程序

本节包含与 Spring Boot 应用程序直接相关的主题。spring-doc.cadn.net.cn

创建您自己的 FailureAnalyzer

FailureAnalyzer 是一种在启动时拦截异常并将其转换为人类可读消息的好方法,该消息被封装在 FailureAnalysis 中。 Spring Boot 为与应用上下文相关的异常、JSR-303 验证等提供了此类分析器。 您也可以创建自己的分析器。spring-doc.cadn.net.cn

AbstractFailureAnalyzerFailureAnalyzer 的一个便捷扩展,用于检查待处理的异常中是否存在指定类型的异常。 您可以从中进行扩展,以便您的实现仅在异常实际存在时才有机会处理该异常。 如果由于任何原因您无法处理该异常,请返回 null,以便让其他实现有机会处理该异常。spring-doc.cadn.net.cn

FailureAnalyzer 实现必须在 META-INF/spring.factories 中注册。 以下示例注册了 ProjectConstraintViolationFailureAnalyzerspring-doc.cadn.net.cn

org.springframework.boot.diagnostics.FailureAnalyzer=\
com.example.ProjectConstraintViolationFailureAnalyzer
如果您需要访问 BeanFactoryEnvironment,请在您的 FailureAnalyzer 实现中将它们声明为构造函数参数。

自动配置故障排除

Spring Boot 的自动配置会尽最大努力“做正确的事”,但有时仍会失败,而且很难弄清楚原因。spring-doc.cadn.net.cn

在任何 Spring Boot ApplicationContext 中,都有一个非常有用的 ConditionEvaluationReport。 如果您启用了 DEBUG 日志输出,就可以看到它。 如果您使用了 spring-boot-actuator(请参阅 Actuator 部分),还有一个 conditions 端点可以以 JSON 格式呈现该报告。 使用该端点来调试应用程序,并查看 Spring Boot 在运行时添加了哪些功能(以及哪些未添加)。spring-doc.cadn.net.cn

通过查看源代码和 API 文档,可以解答更多问题。 在阅读代码时,请记住以下经验法则:spring-doc.cadn.net.cn

  • 查找名为 *AutoConfiguration 的类并阅读其源代码。 特别注意其中的 @Conditional* 注解,以了解它们启用了哪些功能以及在什么条件下启用。 在命令行中添加 --debug 参数,或设置系统属性 -Ddebug,即可在控制台输出应用程序中所有自动配置决策的日志。 在启用了 Actuator 的运行中的应用程序中,可通过 conditions 端点(/actuator/conditions 或其对应的 JMX 接口)查看相同的信息。spring-doc.cadn.net.cn

  • 查找标记为 @ConfigurationProperties 的类(例如 ServerProperties),并从那里读取可用的外部配置选项。 @ConfigurationProperties 注解拥有一个 name 属性,该属性作为外部属性的前缀。 因此,ServerProperties 具有 prefix="server",其配置属性包括 server.portserver.address 等。 在启用了 Actuator 的运行应用程序中,请查看 configprops 端点。spring-doc.cadn.net.cn

  • Binder 上查找 bind 方法的使用,以便以宽松的方式从 Environment 中显式提取配置值。 它通常与前缀一起使用。spring-doc.cadn.net.cn

  • 查找直接绑定到 Environment@Value 注解。spring-doc.cadn.net.cn

  • 查找 @ConditionalOnExpression 注解,这些注解可根据 SpEL 表达式(通常使用从 Environment 解析的占位符进行求值)来开启或关闭功能。spring-doc.cadn.net.cn

在环境或 ApplicationContext 启动之前对其进行自定义

一个 SpringApplication 拥有 ApplicationListenerApplicationContextInitializer 实现,用于应用对上下文或环境的自定义。 Spring Boot 会从 META-INF/spring.factories 加载许多此类自定义供内部使用。 注册额外自定义的方式不止一种:spring-doc.cadn.net.cn

  • 以编程方式,针对每个应用程序,在运行之前调用 SpringApplication 上的 addListenersaddInitializers 方法。spring-doc.cadn.net.cn

  • 通过添加一个 META-INF/spring.factories 文件,并打包成所有应用程序都作为库使用的 jar 文件,以声明式方式应用于所有应用程序。spring-doc.cadn.net.cn

SpringApplication 会向监听器发送一些特殊的 ApplicationEvents(有些甚至在上下文创建之前),同时也会为 ApplicationContext 发布的事件注册监听器。 完整列表请参阅“Spring Boot 特性”章节中的 应用事件与监听器spring-doc.cadn.net.cn

在应用程序上下文刷新之前,也可以使用 Environment 通过 EnvironmentPostProcessor 进行自定义。 每个实现都应在 META-INF/spring.factories 中注册,如下例所示:spring-doc.cadn.net.cn

org.springframework.boot.env.EnvironmentPostProcessor=com.example.YourEnvironmentPostProcessor

该实现可以加载任意文件并将它们添加到 Environment。 例如,以下示例从类路径加载一个 YAML 配置文件:spring-doc.cadn.net.cn

import java.io.IOException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

	private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();

	@Override
	public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
		Resource path = new ClassPathResource("com/example/myapp/config.yml");
		PropertySource<?> propertySource = loadYaml(path);
		environment.getPropertySources().addLast(propertySource);
	}

	private PropertySource<?> loadYaml(Resource path) {
		Assert.isTrue(path.exists(), () -> "'path' [%s] must exist".formatted(path));
		try {
			return this.loader.load("custom-resource", path).get(0);
		}
		catch (IOException ex) {
			throw new IllegalStateException("Failed to load yaml configuration from " + path, ex);
		}
	}

}
import org.springframework.boot.SpringApplication
import org.springframework.boot.env.EnvironmentPostProcessor
import org.springframework.boot.env.YamlPropertySourceLoader
import org.springframework.core.env.ConfigurableEnvironment
import org.springframework.core.env.PropertySource
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource
import org.springframework.util.Assert
import java.io.IOException

class MyEnvironmentPostProcessor : EnvironmentPostProcessor {

	private val loader = YamlPropertySourceLoader()

	override fun postProcessEnvironment(environment: ConfigurableEnvironment, application: SpringApplication) {
		val path: Resource = ClassPathResource("com/example/myapp/config.yml")
		val propertySource = loadYaml(path)
		environment.propertySources.addLast(propertySource)
	}

	private fun loadYaml(path: Resource): PropertySource<*> {
		Assert.isTrue(path.exists()) { "'path' [$path] must exist" }
		return try {
			loader.load("custom-resource", path)[0]
		} catch (ex: IOException) {
			throw IllegalStateException("Failed to load yaml configuration from $path", ex)
		}
	}

}
Environment 已经预装了 Spring Boot 默认加载的所有常规属性源。 因此,可以从环境中获取文件的位置。 前面的示例将 custom-resource 属性源添加到列表末尾,以便在任何其他常规位置定义的键具有更高优先级。 自定义实现可以定义另一种顺序。
虽然在使用 @PropertySource 时,在您的 @SpringBootApplication 上加载自定义资源看似是在 Environment 中的一种便捷方式,但我们不推荐这样做。 此类属性源直到应用程序上下文刷新时才会被添加到 Environment 中。 此时对于某些在刷新开始之前就被读取的属性(例如 logging.*spring.main.*)进行配置已经太晚了。

构建 ApplicationContext 层次结构(添加父上下文或根上下文)

您可以使用 SpringApplicationBuilder 类来创建父子 ApplicationContext 层次结构。 有关更多信息,请参阅spring-doc.cadn.net.cn

创建非 Web 应用程序

并非所有的 Spring 应用程序都必须是 Web 应用程序(或 Web 服务)。如果您想在 main 方法中执行某些代码,同时引导 Spring 应用程序以设置要使用的基础设施,您可以使用 Spring Boot 的 SpringApplication 功能。一个 SpringApplication 会根据其判断是否需要 Web 应用程序来更改其 ApplicationContext 类。你可以做的第一件事就是将服务器相关的依赖项(例如 Servlet API)从类路径中移除。如果无法执行此操作(例如,如果您从同一代码库运行两个应用程序),则可以在您的 SpringApplication 实例上显式调用 setWebApplicationType(WebApplicationType.NONE),或者设置 applicationContextClass 属性(通过 Java API 或使用外部属性)。您希望作为业务逻辑运行的应用程序代码可以实现为 CommandLineRunner,并作为 @Bean 定义放入上下文中。spring-doc.cadn.net.cn