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
UserPOJO and bind them to a bean using
@ConfigurationProperties. This bean can then be injected anywhere like any other Spring bean.
application.properties
<code>user.username=lengleng
user.password=123456
user.age=26
</code>User POJO
<code>/**
* @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;
// ...
}
</code>When using Spring Boot 2.2 (or earlier), the class must be annotated with
@Componentor
@Configuration; otherwise it cannot be injected.
Note the difference between
@ConfigurationPropertiesand
@Value.
@ConfigurationPropertiesScan
In 2.2.0 the feature is enabled by default.
@ConfigurationPropertiesScanautomatically scans the package of the main class and registers all
ConfigurationPropertiesbeans, so you no longer need to add
@Componentor
@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
@Componentscanning 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.
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.