Backend Development 4 min read

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.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Why @ConfigurationPropertiesScan Can Break Your Spring Boot 2.2 Upgrade
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

<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

@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.

JavaBackend DevelopmentSpringSpring BootVersion UpgradeConfigurationProperties
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.