Skip to content

Spring PropertySourcesPlaceholderConfigurer beans: resolve property value at runtime

I am loading properties with multiple PropertySourcesPlaceholderConfigurer beans and setting placeholder prefix-es:

public static PropertySourcesPlaceholderConfigurer fooPropertyConfigurer() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource(""));
    return propertySourcesPlaceholderConfigurer;

While I can inject a property value by specifying it’s index and key like this:

private String value;

Sometimes I need to determine the value of the prefix (‘foo’) at runtime and dynamically resolve the property value. Is this possible? If not, which alternative solutions are recommended for this use case?


Thanks to this post, I’ve found a solution:

public class PropertyPlaceholderExposer implements BeanFactoryAware {  
    ConfigurableBeanFactory beanFactory; 

    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = (ConfigurableBeanFactory) beanFactory;

    public String resolveProperty(String prefix, String key) {
        String rv = beanFactory.resolveEmbeddedValue("$" + prefix + "{" + key + "}");
        return rv;



private PropertyPlaceholderExposer ppe;
    String v = ppe.resolveProperty("bar", "key");