升级Spring Boot 2后bootstrap.properties占位符解析及启动报错咨询
Hey there, I’ve run into these exact issues before when upgrading and combining Spring Boot 2 with Spring Cloud—let’s work through them step by step:
1. Placeholder Parsing Failures in bootstrap.properties After Spring Boot 2 Upgrade
This usually boils down to a key change in how Spring Boot 2.x handles the bootstrap context, especially if you’re using newer Spring Cloud versions.
Why it happens:
Starting with Spring Cloud 2020.0.0 (codename Ilford), the bootstrap context (which loads bootstrap.properties/bootstrap.yml) is disabled by default. In older versions, this context was automatically enabled to pull configuration from sources like Config Server, but now you need to explicitly turn it on.
Fixes:
- Add the bootstrap starter dependency:
For Maven projects, drop this into yourpom.xml:
For Gradle projects, add this line to your<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>build.gradle:implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap' - Double-check placeholder syntax:
Make sure your placeholders use the standard${}format (e.g.,${spring.application.name}) and that the referenced properties exist either in local config or your remote config source. If you rely on custom placeholder syntax, you’ll need to configure aPropertyPlaceholderConfigurerbean explicitly.
2. Bootstrap.properties Related Startup Errors After Adding Spring + Spring Cloud Dependencies
This is almost always caused by version mismatches or missing required dependencies.
Why it happens:
- Version incompatibility: Spring Boot and Spring Cloud have strict version compatibility rules. Using mismatched versions (like Spring Boot 2.7 with Spring Cloud 2020.0.x) will break context initialization and throw errors related to bootstrap loading.
- Missing bootstrap starter: As mentioned earlier, newer Spring Cloud versions don’t include bootstrap support out of the box. Without the starter, the app can’t process
bootstrap.propertiesat all. - Invalid config syntax: A typo, unclosed placeholder, or malformed property in
bootstrap.propertieswill crash the startup process.
Fixes:
- Align Spring Boot and Spring Cloud versions:
Stick to the official compatibility pairs. For example:- Spring Boot 2.7.x ↔ Spring Cloud 2021.0.x (Jubilee)
- Spring Boot 3.x ↔ Spring Cloud 2022.0.x (Kilburn)
- Add the spring-cloud-starter-bootstrap dependency:
Same as the fix for issue 1—this re-enables the bootstrap context to load your config file. - Validate your bootstrap.properties:
Scan for typos, unclosed${}placeholders, or invalid property names. For example, avoid empty values likespring.cloud.config.uri=or misspelled keys likespring.applicaiton.name. - Confirm config file location:
Make surebootstrap.propertieslives in the standardsrc/main/resourcesdirectory. If you’re using a custom path, specify it with thespring.config.locationsystem property or environment variable.
内容的提问来源于stack exchange,提问作者Righto

