https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Externalized Configuration
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration. Property values can be injected directly into your beans by using the @Value annotation, accessed through Spring’s Environment abstraction, or be bound to structured objects through @ConfigurationProperties.
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.propertieswhen devtools is active). @TestPropertySourceannotations on your tests.propertiesattribute on your tests. Available on@SpringBootTestand the test annotations for testing a particular slice of your application.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON(inline JSON embedded in an environment variable or system property). ServletConfiginit parameters.ServletContextinit parameters.- JNDI attributes from
java:comp/env. - Java System properties (
System.getProperties()). - OS environment variables.
- A
RandomValuePropertySourcethat has properties only inrandom.*. - Profile-specific application properties outside of your packaged jar (
application-{profile}.propertiesand YAML variants). - Profile-specific application properties packaged inside your jar (
application-{profile}.propertiesand YAML variants). - Application properties outside of your packaged jar (
application.propertiesand YAML variants). - Application properties packaged inside your jar (
application.propertiesand YAML variants). @PropertySourceannotations on your@Configurationclasses.- Default properties (specified by setting
SpringApplication.setDefaultProperties).
本文详细介绍了Spring Boot如何实现外部配置,使应用能在不同环境中使用相同的代码。通过属性文件、YAML、环境变量等方式,Spring Boot允许配置的灵活覆盖与注入。文章还深入探讨了Spring Boot的PropertySource顺序,确保配置的正确加载与覆盖。
3万+

被折叠的 条评论
为什么被折叠?



