Backend Development 11 min read

Understanding @ConfigurationProperties and @PropertySources in Spring

This article explains the purpose and usage of Spring's @ConfigurationProperties and @PropertySources annotations, demonstrates how to bind configuration files to Java beans, shows step‑by‑step examples for both annotations, and compares their roles in managing application properties.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Understanding @ConfigurationProperties and @PropertySources in Spring

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

What are @ConfigurationProperties and @PropertySources? @ConfigurationProperties binds external property values to a Java bean, typically used with @Configuration or @Component . @PropertySources (or its singular form @PropertySource ) declares one or more property files that Spring should load.

Using @ConfigurationProperties

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

After defining the bean, enable it in a configuration class:

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

@Configuration
@EnableConfigurationProperties(MyAppProperties.class)
public class AppConfig {
}

Add matching entries to application.properties or application.yml :

myapp.name=My Spring Boot App
myapp.version=1.0

Inject the bean into a service:

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());
    }
}

Using @PropertySources

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 {
}

Define the property files (e.g., default.properties and custom.properties ) under src/main/resources and inject values with @Value :

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;
    }
}

Difference between the two annotations – @PropertySources focuses on declaring multiple property files, while @ConfigurationProperties binds the loaded properties to a strongly‑typed Java bean. They can be used together to achieve modular and maintainable configuration management.

backendJavaconfigurationSpringannotationsproperties
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.