Master SpringBoot PropertySourcesPlaceholderConfigurer & PropertyOverrideConfigurer
This guide explains how to use SpringBoot's PropertySourcesPlaceholderConfigurer and PropertyOverrideConfigurer to handle missing placeholders, set default values, customize prefixes, load custom property files, and override bean properties—including nested properties—while avoiding common pitfalls like startup errors and NPEs.
Environment: SpringBoot 3.2.5
1. Introduction
Before SpringBoot, developers often manually configured PropertySourcesPlaceholderConfigurer and PropertyOverrideConfigurer to load property files and resolve ${xxx} placeholders. SpringBoot now auto‑configures these classes, so explicit definitions are rarely needed.
2. Practical Examples
2.1 PropertySourcesPlaceholderConfigurer
Missing property issue
When a placeholder like @Value("${pack.title}") refers to a property that does not exist, the application fails to start:
@Value("pack.title")
private String title;You can avoid the error by providing a default value:
@Value("${pack.title:xx}")
private String title;Even an empty default works ( ${pack.title:}).
To globally ignore unresolved placeholders, define a PropertySourcesPlaceholderConfigurer bean:
@Bean
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
// ignore unresolvable placeholders
placeholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return placeholderConfigurer;
}After this configuration the service starts correctly.
Custom placeholder syntax (not recommended)
@Bean
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
// set custom prefix and suffix, e.g. @[xxx]
placeholderConfigurer.setPlaceholderPrefix("@[");
placeholderConfigurer.setPlaceholderSuffix("]");
return placeholderConfigurer;
}With this setting you must use @[xxx] in your properties.
Custom property file
@Bean
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
// load a specific properties file
placeholderConfigurer.setLocations(new ClassPathResource("pack.properties"));
return placeholderConfigurer;
}You can also load multiple files.
2.2 PropertyOverrideConfigurer
This class is less frequently used. It is a BeanFactoryPostProcessor that overrides bean property values.
@Component
public class PropertyOverrideBean {
@Value("${pack.title}")
private String title;
@Value("${pack.os}")
private String os;
// getters, setters
}
@Resource
private PropertyOverrideBean pob;
System.out.println(pob);Output example: PropertyOverrideBean [title=xxxooo, os=window] Define the override configurer:
@Bean
PropertyOverrideConfigurer propertyOverrideConfigurer() {
PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
propertyOverrideConfigurer.setLocation(new ClassPathResource("pack.properties"));
return propertyOverrideConfigurer;
}The pack.properties file must use the bean name as a prefix:
propertyOverrideBean.title=my title
propertyOverrideBean.os=linuxResulting bean: PropertyOverrideBean [title=my title, os=linux] Nested properties
@Component
public class PropertyOverrideBean {
// other properties
private User user = new User();
}
public class User {
@Value("pack.age")
private Integer age;
// getters, setters
}Add to pack.properties: propertyOverrideBean.user.age=88 Output:
PropertyOverrideBean [title=my title, os=linux, user=User [age=88]]Note: The nested User object must be instantiated beforehand, otherwise a NullPointerException occurs.
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
