Why @ConfigurationPropertiesScan Can Break Your Spring Boot 2.2 Upgrade
This article explains how @ConfigurationProperties and @ConfigurationPropertiesScan work in Spring Boot, the differences from @Value, and why upgrading from 2.2.0 to 2.2.1 may cause configuration beans to stop loading.
In the previous post we discussed the compatibility issue between Spring Boot 2.2.0 and MyBatis; today we dive into the "boom change" that can become a huge pitfall.
@ConfigurationProperties Annotation
When the configuration file contains a group of related properties such as username, password, and age, we can extract them into a User POJO and bind them to a bean using @ConfigurationProperties. This bean can then be injected anywhere like any other Spring bean.
application.properties
user.username=lengleng
user.password=123456
user.age=26User POJO
/**
* @author lengleng
* @date 2019-11-08
* <p>
* Before version 2.2, you must declare the class with @Component or @Configuration to make it a Spring Bean.
*/
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String username;
private String password;
private Integer age;
// ...
}When using Spring Boot 2.2 (or earlier), the class must be annotated with @Component or @Configuration; otherwise it cannot be injected.
Note the difference between @ConfigurationProperties and @Value.
@ConfigurationPropertiesScan
In 2.2.0 the feature is enabled by default. @ConfigurationPropertiesScan automatically scans the package of the main class and registers all ConfigurationProperties beans, so you no longer need to add @Component or @Configuration.
In 2.2.1 the feature is disabled by default and must be explicitly enabled. Compatibility issues arise with @Profile, so Spring Boot 2.2.1 turns the feature off.
To enable it, simply add the annotation to your main application class.
Summary
When upgrading from 2.1.x to 2.2, you won’t encounter this issue because the default compatibility with @Component scanning is retained.
If you upgrade from 2.2.0 to 2.2.1, be extra careful; the change may cause all your configuration classes to become ineffective.
Whether to add the annotation or not can be guided by the hints provided by your current version of IDEA.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
