Loading nested placeholders from a properties file using Spring

Can I load nested placeholders from a properties file? I am trying to load a URL dynamically.

For example, if the properties file contains

my.url=http://localhost:8888/service/{nestedProperty}/ 

Is there a way to load values ​​for {nestedProperty} at runtime? Similar to ResourceBundle behavior. If so, how would I effectively create an instance of String? Still i think

 <bean id="myURLString" class="java.lang.String" scope="prototype" lazy-init="true"> <property name="URL" value="${my.url}" /> </bean> 

... but I'm not sure what properties to attach. I would like to get a bean using annotations if possible, although I currently have something in the lines

 ctx.getBean("myURLString", String.class, new Object[] { nestedProperty} ); 

I looked at PropertyPlaceholderConfigurer and a few other questions with property files here, but I cannot figure out if this is possible.

It should also be noted that I want to dynamically load this nested property from my code, or at least manipulate it from there (perhaps via @PostConstruct?)

+7
source share
1 answer

Yes, it is possible:

 my.url=http://localhost:8888/service/${nestedProperty} nestedProperty=foo/bar/baz 

Add a dollar sign in front of the curly braces in your example, and you're set!

To use the fully resolved property, do the following:

 @Value("${my.url}") private String url; 

in spring bean.

+9
source

All Articles