SpringBoot supports configuration files in two formats: properties and YAML by default. The former has a simple format, but only supports key-value pairs. If you need to express a list, it's best to use YAML format. SpringBoot supports automatically loading configuration files with agreed names, such as application.yml. If it is a configuration file with a custom name, you have to find another method. Unfortunately, unlike the former which has a convenient loading method like @PropertySource, the loading of the latter must be achieved with the help of coding logic.
bootstrap.yml (bootstrap.properties) is used to execute when the program boots. Application For earlier configuration information reading, for example, it can be used to configure the parameters used in application.yml.
application.yml (application.properties) The application-specific configuration information can be used to configure the parameters required in subsequent modules. Public parameters used, etc.
bootstrap.yml is loaded before application.yml
When using Spring Cloud Config Server, You should specify spring.application.name and spring.cloud.config.server.git.uri
and some encryption/decryption information
Technically, bootstrap.yml is loaded by a parent Spring ApplicationContext. The parent's Spring ApplicationContext is loaded first, before the ApplicationContext of application.yml is loaded.
Why do you need to put the config server information in bootstrap.yml?
When using Spring Cloud, configuration information is generally loaded from the config server. In order to obtain configuration information (such as passwords, etc.), you need some early boot configuration. Therefore, put the config server information in bootstrap.yml to load the configuration information really needed during this period.
Start the context
Spring Cloud will create a Bootstrap Context as the parent context of the Spring application's Application Context. During initialization, Bootstrap Context is responsible for loading configuration properties from external sources and parsing configurations. The two contexts share an Environment obtained from the outside. Bootstrap properties have high priority and by default they are not overridden by local configuration. Bootstrap context and Application Context have different conventions, so a new bootstrap.yml file is added instead of application.yml (or application.properties). Ensure the separation of Bootstrap Context and Application Context configurations. The following is an example:
bootstrap.yml
spring: application: name: foo cloud: config: uri: ${SPRING_CONFIG_URI:http://localhost:8888}
It is recommended to configure spring.application.name in bootstrap.yml or application.yml. You can set spring.cloud.bootstrap.enabled= false to disable bootstrap.
Application context hierarchy
If you create an Application Context through SpringApplication or SpringApplicationBuilder, the parent context Bootstrap Context will be created for the Application Context of the spring application. There is a feature in Spring that the child context will inherit the property sources and profiles of the parent class, so the main application context will add additional property sources compared to not using Spring Cloud Config. Additional property sources are:
"bootstrap": If the PropertySourceLocator is scanned in the Bootstrap Context and there is a property, it will be added to the CompositePropertySource. Spirng Cloud Config adds properties in this way. See the source code ConfigServicePropertySourceLocator` for details. There is also an example of customization below.
"applicationConfig: [classpath:bootstrap.yml]" , (if there is spring.profiles.active=production, then for example applicationConfig: [classpath:/bootstrap.yml]#production): If you use bootstrap.yml to configure Bootstrap Context, it has a lower priority than application.yml. It will be added to the child context as part of the Spring Boot application. Introduced below.
Due to priority rules, the Bootstrap Context does not contain data from bootstrap.yml, but it can be used as the default setting.
You can easily extend any context hierarchy you create by using the interface it provides, or using the methods included in SpringApplicationBuilder (parent(), child(), sibling()). Bootstrap Context will be the highest level parent class. Each extended Context has its own bootstrap property source (which may be empty). Each extended Context has a different spring.application.name. In principle, parent-child contexts at the same level also have different names, and therefore, there will also be different Config Server configurations. Properties of the child context will override those of the parent context if they have the same name.
Note that SpringApplicationBuilder allows sharing Environment to all levels, but it is not the default. Therefore, sibling contexts at the same level may not necessarily have the same profiles or property sources when they do not share something with the parent class.
Modify Bootstrap attribute configuration
Source code location BootstrapApplicationListener.
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}"); String configLocation = environment.resolvePlaceholders("${spring.cloud.bootstrap.location:}"); Map<String, Object> bootstrapMap = new HashMap<>();bootstrapMap.put("spring.config.name",configName); if(StringUtils.hasText(configLocation)){ bootstrapMap.put("spring.config.location", configLocation); }
bootstrap.yml是由spring.cloud.bootstrap.name(默认:”bootstrap”)或者spring.cloud.bootstrap.location(默认空)。这些属性行为与spring.config.*类似,通过它的Environment来配置引导ApplicationContext。如果有一个激活的profile(来源于spring.profiles.active或者Environment的Api构建),例如bootstrap-development.properties 就是配置了profile为development的配置文件.
覆盖远程属性
property sources被bootstrap context 添加到应用通常通过远程的方式,比如”Config Server”。默认情况下,本地的配置文件不能覆盖远程配置,但是可以通过启动命令行参数来覆盖远程配置。如果需要本地文件覆盖远程文件,需要在远程配置文件里设置授权
spring.cloud.config.allowOverride=true(这个配置不能在本地被设置)。一旦设置了这个权限,你可以配置更加细粒度的配置来配置覆盖的方式,
比如:
spring.cloud.config.overrideNone=true 覆盖任何本地属性
spring.cloud.config.overrideSystemProperties=false 仅仅系统属性和环境变量
源文件见PropertySourceBootstrapProperties
自定义启动配置
bootstrap context是依赖/META-INF/spring.factories文件里面的org.springframework.cloud.bootstrap.BootstrapConfiguration条目下面,通过逗号分隔的Spring @Configuration类来建立的配置。任何main application context需要的自动注入的Bean可以在这里通过这种方式来获取。这也是ApplicationContextInitializer建立@Bean的方式。可以通过@Order来更改初始化序列,默认是”last”。
# spring-cloud-context-1.1.1.RELEASE.jar # spring.factories # AutoConfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ org.springframework.cloud.autoconfigure.RefreshAutoConfiguration,\ org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration,\ org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration # Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.cloud.bootstrap.BootstrapApplicationListener,\ org.springframework.cloud.context.restart.RestartListener # Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\ org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\ org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
警告
小心,你添加的自定义BootstrapConfiguration类没有错误的@ComponentScanned到你的主应用上下文,他们可能是不需要的。使用一个另外的包不被@ComponentScan或者@SpringBootApplication注解覆盖到。
bootstrap context通过spring.factories配置的类初始化的所有的Bean都会在SpingApplicatin启动前加入到它的上下文里去。
自定义引导配置来源:Bootstrap Property Sources
默认的property source添加额外的配置是通过配置服务(Config Server),你也可以自定义添加property source通过实现PropertySourceLocator接口来添加。你可以使用它加配置属性从不同的服务、数据库、或者其他。
下面是一个自定义的例子:
@Configuration public class CustomPropertySourceLocator implements PropertySourceLocator { @Override public PropertySource<?> locate(Environment environment) { return new MapPropertySource("customProperty", Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended")); } }
Environment被ApplicationContext建立,并传入property sources(可能不同个profile有不同的属性),所以,你可以从Environment寻找找一些特别的属性。比如spring.application.name,它是默认的Config Server property source。
如果你建立了一个jar包,里面添加了一个META-INF/spring.factories文件:
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
那么,”customProperty“的PropertySource将会被包含到应用。
The above is the detailed content of What is the difference between SpringBoot application.yml and bootstrap.yml. For more information, please follow other related articles on the PHP Chinese website!