Why @Value Is Discouraged in Spring Boot and How @ConfigurationProperties Solves It

This article explains the drawbacks of using Spring Boot's @Value for configuration injection, demonstrates how @ConfigurationProperties provides a cleaner, maintainable alternative, and shows how to add validation with @Validated and standard bean validation annotations.

Programmer DD
Programmer DD
Programmer DD
Why @Value Is Discouraged in Spring Boot and How @ConfigurationProperties Solves It

The @Value annotation is widely known among Spring Boot developers for quickly loading configuration values into beans.

For example, you can inject the configuration key com.didispace.title directly into a service:

@Service
public class TestService {
    @Value("${com.didispace.title}")
    private String title;
}

Although convenient, using @Value is discouraged because it fragments configuration loading; the same property may be scattered across multiple services or controllers, making updates error‑prone and hard to maintain.

Instead, it is recommended to use @ConfigurationProperties to group related settings. For the prefix com.didispace, you can define a dedicated properties class:

@Configuration
@ConfigurationProperties(prefix = "com.didispace")
public class DidispaceProperties {
    private String title;
}

The DidispaceProperties class will load all configurations that start with com.didispace. Other services or controllers can simply inject this bean, avoiding scattered @Value annotations and centralizing configuration changes.

Configuration validation can also be added by including the spring-boot-starter-validation dependency and annotating the properties class with @Validated and standard bean‑validation constraints:

@Validated
@Configuration
@ConfigurationProperties(prefix = "com.didispace")
public class DidispaceProperties {
    @NotNull
    private String title;
}

This approach enables automatic validation of configuration values, ensuring they meet required criteria before the application starts.

Overall, replacing @Value with @ConfigurationProperties and optional validation leads to cleaner, more maintainable Spring Boot configuration management.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaconfigurationSpring BootConfigurationProperties@Value
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

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.