Mastering Spring’s @ConfigurationProperties and @PropertySources for Clean Config Management

This article explains how Spring's @ConfigurationProperties and @PropertySources annotations work, compares their purposes, and provides step‑by‑step examples for binding external configuration to Java beans and loading multiple property files in a Spring Boot application.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Mastering Spring’s @ConfigurationProperties and @PropertySources for Clean Config Management

1. Introduction

In modern software development, configuration management is essential. Spring provides powerful annotations @ConfigurationProperties and @PropertySources to simplify handling of configuration data.

2. What are @ConfigurationProperties and @PropertySources

@ConfigurationProperties

binds external properties to a Java bean, typically used with @Configuration or @Component. Example:

@Configuration
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
    private String name;
    private String version;
    // getters and setters
}
@PropertySources

(plural) allows specifying multiple property sources in a Spring application. It works similarly to @PropertySource but can list several files.

@Configuration
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:custom.properties")
})
public class AppConfig {
    // beans can use properties from both sources
}

3. Using @ConfigurationProperties

Steps: 1) Create a POJO with fields matching the properties. 2) Create a configuration class annotated with @Configuration and @EnableConfigurationProperties(MyAppProperties.class). 3) Add matching entries to application.properties or application.yml. 4) Inject the POJO into beans via @Autowired.

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MyAppProperties.class)
public class AppConfig {
}
myapp.name=My Spring Boot App
myapp.version=1.0
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    private final MyAppProperties myAppProperties;

    @Autowired
    public MyService(MyAppProperties myAppProperties) {
        this.myAppProperties = myAppProperties;
    }

    public void doSomething() {
        System.out.println("App Name: " + myAppProperties.getName());
        System.out.println("App Version: " + myAppProperties.getVersion());
    }
}

4. Using @PropertySources

In standard Spring (or Spring Boot) you can declare multiple property files with @PropertySources and inject values with @Value.

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources({
    @PropertySource("classpath:default.properties"),
    @PropertySource("classpath:custom.properties")
})
public class AppConfig {
}
app.name=My Spring Boot App (Default)
app.version=1.0 (Default)
app.name=My Custom Spring Boot App
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    @Value("${app.name}")
    private String appName;

    @Value("${app.version}")
    private String appVersion;

    public String getAppInfo() {
        return "App Name: " + appName + ", App Version: " + appVersion;
    }
}

5. Differences between @PropertySources and @ConfigurationProperties

@PropertySources

defines multiple property files, enabling modular and environment‑specific configuration. @ConfigurationProperties binds those properties to a strongly‑typed Java bean. They can be used together: @PropertySources loads the files, while @ConfigurationProperties maps the values to beans.

@PropertySources specifies multiple sources, supports modular configuration and priority ordering.

@ConfigurationProperties binds external properties to POJOs, simplifying access throughout the application.

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.

JavaspringSpring BootConfigurationPropertiesPropertySources
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.