Mastering @ConfigurationProperties and @PropertySources in Spring Boot

This article provides a comprehensive guide to using Spring Boot's @ConfigurationProperties and @PropertySources annotations, covering their purpose, usage steps, code examples, and the differences between them for effective configuration management in Java backend applications.

macrozheng
macrozheng
macrozheng
Mastering @ConfigurationProperties and @PropertySources in Spring Boot

1 Introduction

Configuration management is essential in modern software development. Spring offers powerful annotations such as @ConfigurationProperties and @PropertySources to simplify handling of configuration data.

2 What are @ConfigurationProperties and @PropertySources

@ConfigurationProperties

This Spring Boot annotation binds properties from configuration files to a Java bean, allowing easy access throughout the application. It is typically used together with @Configuration or @Component.

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

The annotation maps keys prefixed with myapp (e.g., myapp.name, myapp.version) to the fields of MyAppProperties.

@PropertySources

This Spring Framework annotation specifies multiple property sources so that Spring can resolve properties from several files. It works similarly to @PropertySource but allows a collection of sources.

@Configuration
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:custom.properties")
})
public class AppConfig {
    // configuration beans
}

The annotation tells Spring to load both application.properties and custom.properties as configuration sources.

3 Using @ConfigurationProperties

Step 1: Create POJO

@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
    private String name;
    private String version;
    // ...
}

Step 2: Configuration class

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

Step 3: Add entries to application.properties or application.yml

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

Step 4: Inject properties into a bean

@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

Although Spring Boot commonly relies on the default application.properties, you can still use @PropertySources to load multiple property files.

@Configuration
@PropertySources({
    @PropertySource("classpath:default.properties"),
    @PropertySource("classpath:custom.properties")
})
public class AppConfig { }

Place the files under src/main/resources. Example contents:

# default.properties
app.name=My Spring Boot App (Default)
app.version=1.0 (Default)

# custom.properties
app.name=My Custom Spring Boot App

Inject values with @Value in a service and expose them via a controller.

@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;
    }
}
@RestController
public class MyController {
    private final MyService myService;
    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
    }
    @GetMapping("/app-info")
    public String getAppInfo() {
        return myService.getAppInfo();
    }
}

5 Differences between @PropertySources and @ConfigurationProperties

@PropertySources

defines multiple property files, enabling modular configuration, environment‑specific overrides, and priority ordering. @ConfigurationProperties binds the loaded properties to a Java bean, usually together with @Configuration or @Component.

Typical usage: declare property files with @PropertySources, then bind needed values to beans using @ConfigurationProperties for clean, maintainable configuration management.

Author: 一只牛博 Source: blog.csdn.net/Mrxiao_bo/article/details/133637337
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.

JavaBackend DevelopmentConfiguration ManagementSpring BootConfigurationPropertiesPropertySources
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.