Spring Boot默认兼容properties和YAML两种配置文件格式。properties格式简洁,但仅支持键值对;若需表达列表结构,推荐使用YAML。Spring Boot可自动加载约定名称(如application.yml)的配置文件,而自定义名称的文件则需额外处理。不同于@PropertySource能便捷加载自定义文件,后者必须通过编码逻辑实现加载。
一、bootstrap.yml(bootstrap.properties)与application.yml(application.properties)执行顺序
bootstrap.yml(或bootstrap.properties)用于程序引导阶段的早期配置信息读取,例如配置application.yml中引用的参数等前置条件。
application.yml(或application.properties)包含应用程序特有的配置信息,用于定义后续各模块所需的公共参数。
在加载时序上,bootstrap.yml优先于application.yml被加载。
二、典型的应用场景如下:
- 当集成Spring Cloud Config Server时,应在bootstrap.yml中指定spring.application.name以及spring.cloud.config.server.git.uri等关键连接信息。
- 此外,还需在此文件中配置加密/解密的密钥等敏感信息。
从技术实现角度看,bootstrap.yml由一个父级的Spring ApplicationContext加载。该父级上下文在加载application.yml的子级ApplicationContext之前先行启动。
为何需要将Config Server的配置信息置于bootstrap.yml中?
在使用Spring Cloud时,配置信息通常源自Config Server。为了获取这些配置(如密码),需要提前的引导配置。因此,将Config Server的连接信息放在bootstrap.yml中,以便在早期阶段加载真正需要的配置数据。
三、高级使用场景
启动上下文
Spring Cloud会创建一个Bootstrap Context,作为Spring应用Application Context的父上下文。初始化期间,Bootstrap Context负责从外部源加载并解析配置属性。这两个上下文共享同一个从外部获取的Environment。Bootstrap属性具有更高优先级,默认情况下不会被本地配置覆盖。由于Bootstrap Context和Application Context遵循不同的约定,因此引入了独立的bootstrap.yml文件,而非复用application.yml,以确保两者配置的隔离。示例如下:
bootstrap.yml
spring:
application:
name: foo
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:8888}
建议在bootstrap.yml或application.yml中配置spring.application.name。可通过设置spring.cloud.bootstrap.enabled=false来禁用Bootstrap Context。
应用上下文层次结构
若通过SpringApplication或SpringApplicationBuilder创建Application Context,系统会自动为Spring应用创建父级Bootstrap Context。Spring特性允许子上下文继承父类的property sources和profiles,因此,相较于未使用Spring Cloud Config的情况,主Application Context会新增额外的property sources。这些额外的property sources包括:
- “bootstrap”:若在Bootstrap Context中扫描到PropertySourceLocator且存在属性,则会将其添加到CompositePropertySource中。Spring Cloud Config即通过此方式添加属性,具体实现可见源码ConfigServicePropertySourceLocator。下方有自定义示例。
- “applicationConfig: [classpath:bootstrap.yml]”:若使用bootstrap.yml配置Bootstrap Context,其优先级低于application.yml。它将被添加到子上下文中,作为Spring Boot应用程序的一部分。下文将对此进行介绍。
基于优先级规则,Bootstrap Context本身不包含来自bootstrap.yml的数据,但可将其用作默认设置。
你可以轻松扩展任何建立的上下文层次,利用提供的接口或SpringApplicationBuilder包含的方法(parent()、child()、sibling())。Bootstrap Context将是最高级别的父类。每个扩展的Context都拥有自己的bootstrap property source(可能为空)。每个扩展的Context都有独特的spring.application.name。同一层级的父子上下文原则上也有不同名称,因此对应不同的Config Server配置。同名情况下,子上下文的属性将覆盖父上下文的属性。
注意,SpringApplicationBuilder允许向所有层级共享Environment,但这并非默认行为。因此,同级的兄弟上下文在不与父类共享某些内容时,不一定拥有相同的profiles或property sources。
修改Bootstrap属性配置
相关源码位于BootstrapApplicationListener。
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
String configLocation = environment.resolvePlaceholders("${spring.cloud.bootstrap.location:}");
Map 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的配置文件。
覆盖远程属性
通常通过远程方式(如Config Server)将property sources添加到应用中。默认情况下,本地配置文件无法覆盖远程配置,但可通过启动命令行参数进行覆盖。若需本地文件覆盖远程文件,需在远程配置文件中设置授权:spring.cloud.config.allowOverride=true(此配置不能在本地设置)。一旦启用此权限,可进一步配置更细粒度的覆盖策略。
例如:
- spring.cloud.config.overrideNone=true:覆盖任何本地属性
- spring.cloud.config.overrideSystemProperties=false:仅系统属性和环境变量
自定义启动配置
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注解扫描到。
通过spring.factories配置的类初始化的所有Bean,都会在Spring Application启动前加入到Bootstrap Context中。
自定义引导配置来源:Bootstrap Property Sources 默认的property source通过配置服务(Config Server)添加,你也可以通过实现PropertySourceLocator接口来自定义添加property source,从而从不同服务、数据库或其他来源获取配置属性。
以下是一个自定义示例:
@Configuration
public class CustomPropertySourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
return new MapPropertySource("customProperty",
Collections.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将会被包含到应用中。
到此这篇关于SpringBoot application.yml和bootstrap.yml的区别的文章就介绍到这了,更多相关SpringBoot application.yml bootstrap.yml内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!