Spring是否支持从Spring Cloud Config/HTTP URL加载bootstrap.yml?是否需类路径存在该文件?
First, a quick clarification: The bootstrap.yml (or bootstrap.properties) file is a Spring Cloud-specific feature, not part of the core Spring Framework. Let's break down how it handles configuration loading, since behavior varies by version:
For Spring Boot 2.3.x and earlier (paired with older Spring Cloud releases like Hoxton, Greenwich):
- By default,
bootstrap.ymlloads during the early "bootstrap" phase of application startup, beforeapplication.yml. - It doesn’t have to live in your project’s classpath. Here’s what’s supported:
- Local classpath (your project’s
src/main/resourcesfolder) is the default source. - Spring Cloud Config Server: Add basic Config Server settings (like
spring.cloud.config.uri) to your localbootstrap.yml, and the client will pull additional configuration from the Config Server during the bootstrap phase—merging it with local bootstrap settings. - HTTP URLs: Specify external bootstrap config via startup parameters or environment variables. For example, start your app with:
This tells the bootstrap phase to load config from that HTTP endpoint alongside any local bootstrap files.java -jar your-app.jar --spring.config.location=https://your-server.com/bootstrap.yml
- Local classpath (your project’s
- By default,
For Spring Boot 2.4.x+ (paired with Spring Cloud 2020.0.0+ / Ilford and later):
- Spring Cloud disabled the bootstrap phase by default, replacing it with Spring Boot’s standard
spring.config.importfeature. - You don’t strictly need
bootstrap.ymlanymore. To load from Config Server, add this to yourapplication.yml:spring.config.import: configserver:https://your-config-server-url - To load from a raw HTTP URL, use:
spring.config.import: https://your-server.com/your-config.yml - If you want to keep using
bootstrap.ymlfor backward compatibility, add thespring-cloud-starter-bootstrapdependency to your project. Once added, the old bootstrap phase logic applies again—supporting local classpath, Config Server, and HTTP URL sources just like before.
- Spring Cloud disabled the bootstrap phase by default, replacing it with Spring Boot’s standard
In short: bootstrap.yml doesn’t have to exist in your classpath. Spring Cloud supports loading configuration from Spring Cloud Config Server or external HTTP URLs, though the exact setup depends on your Spring Boot/Spring Cloud version.
内容的提问来源于stack exchange,提问作者programmer

