Using @ConfigurationProperties and @PropertySources in Spring Boot
This article explains the purpose and differences of Spring's @ConfigurationProperties and @PropertySources annotations, provides step‑by‑step guidance for creating POJOs, configuration classes, property files, and demonstrates how to bind and inject configuration values in a Spring Boot application with code examples.
1 Preface
In modern software development, configuration management is essential. Spring offers powerful annotations such as @ConfigurationProperties and @PropertySources to simplify handling of application configuration.
2 What are @ConfigurationProperties and @PropertySources
Both annotations relate to property configuration in Spring applications.
2.1 @ConfigurationProperties
This annotation binds external property values to a Java bean. It is typically used together with @Configuration or @Component so that properties defined in files like application.properties can be mapped to fields of a POJO.
@Configuration
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
private String name;
private String version;
// getters and setters
}The above example maps properties prefixed with myapp (e.g., myapp.name, myapp.version) to the fields of MyAppProperties.
2.2 @PropertySources
This annotation allows you to declare multiple property sources for a Spring application, similar to @PropertySource but accepting an array. It is useful for modularising configuration across several files.
@Configuration
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:custom.properties")
})
public class AppConfig {
// beans can use properties from both files
}3 Using @ConfigurationProperties
Step 1: Create a Java POJO
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
private String name;
private String version;
// constructors, getters, setters omitted for brevity
}Step 2: Create a configuration class
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MyAppProperties.class)
public class AppConfig {
}Step 3: Add entries to a property file
myapp.name=My Spring Boot App
myapp.version=1.0Or using YAML:
myapp:
name: My Spring Boot App
version: 1.0Step 4: Inject the bound properties
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() {
String appName = myAppProperties.getName();
String appVersion = myAppProperties.getVersion();
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}4 Using @PropertySources
In typical Spring Boot projects the default application.properties (or application.yml) is used, but you can still employ @PropertySources to load additional files.
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
@Configuration
@PropertySources({
@PropertySource("classpath:default.properties"), // default properties
@PropertySource("classpath:custom.properties") // custom overrides
})
public class AppConfig {
}Example default.properties:
app.name=My Spring Boot App (Default)
app.version=1.0 (Default)Example custom.properties:
app.name=My Custom Spring Boot AppInjecting values from the property sources
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;
}
}Exposing the information via a controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@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();
}
}Running the application and accessing /app-info will display the configuration values resolved from the defined property sources.
5 Differences between @PropertySources and @ConfigurationProperties
@PropertySourcesis used to declare multiple property files, providing modular and environment‑specific configuration, but it does not bind properties directly to Java beans. @ConfigurationProperties binds the values from those property files to a POJO, usually together with @Configuration or @Component. In practice you often define the sources with @PropertySources and then bind them with @ConfigurationProperties for type‑safe access throughout the application.
Thus, @PropertySources manages where the configuration comes from, while @ConfigurationProperties manages how the configuration is represented in code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
