VOOZH about

URL: https://www.javacodegeeks.com/2013/07/spring-bean-and-propertyplaceholderconfigurer.html

⇱ Spring @Bean and PropertyPlaceHolderConfigurer - Java Code Geeks


I was recently stumped by what I thought was going to be a fairly straightforward implementation – Consider the following Spring Java based bean definition file (
@Configuration):
 
 
 
 
 
 
 
 

package root;

...

@Configuration
@PropertySource("classpath:root/test.props")
public class SampleConfig {

 @Value("${test.prop}")
 private String attr;

 @Bean
 public SampleService sampleService() {
 return new SampleService(attr);
 }
}

Here a bean `sampleService` is being defined, this bean is initialized with an attribute that is populated using a @Value annotation using a property placeholder string ${test.prop}.

The test for this is the following:

@ContextConfiguration(classes=SampleConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ConfigTest {
 @Autowired
 private SampleService sampleService;
 @Test
 public void testConfig() {
 assertThat(sampleService.aMethod(), is("testproperty"));
 }
}

which with the current implementation of SampleConfig fails, as the place holder ${test.prop} is not resolved at all. The standard fix for this is to register a PropertySourcesPlaceholderConfigurer which is a BeanFactoryPostProcessor responsible for scanning through all the registered bean definitions and injecting in the resolved placeholders. With this change in place the @Configuration file looks like this:

@Configuration
@PropertySource("classpath:root/test.props")
public class SampleConfig { 
 @Value("${test.prop}")
 private String attr;

 @Bean
 public SampleService sampleService() {
 return new SampleService(attr);
 }

 @Bean
 public PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
 return new PropertySourcesPlaceholderConfigurer();
 }
}

However, after adding in the Property resolver, the test still fails, this time the value returned by the sampleService is null, not even the placeholder value!

The reason for the issue it turns out, is that with @Configuration which internally uses annotations like @Autowired, @Value, and @PostConstruct, any BeanFactoryPostProcessor beans have to be declared with a static, modifier. Otherwise the containing @Configuration class is instantiated very early and the BeanPostProcessors responsible for resolving annotations like @Value, @Autowired etc, cannot act on it.

This fix is well documented in the javadoc of @Bean, further a message is also logged which provides the workaround:

WARN : org.springframework.context.annotation.ConfigurationClassEnhancer - @Bean method RootConfig.placeHolderConfigurer is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details

So with this fix the new working configuration is the following:

@Configuration
@PropertySource("classpath:root/test.props")
public class SampleConfig { 
 @Value("${test.prop}")
 private String attr;

 @Bean
 public SampleService sampleService() {
 return new SampleService(attr);
 }

 @Bean
 public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
 return new PropertySourcesPlaceholderConfigurer();
 }
}

References:

Reference: Spring @Bean and PropertyPlaceHolderConfigurer from our JCG partner Biju Kunjummen at the all and sundry blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Spring
πŸ‘ Photo of Biju Kunjummen
Biju Kunjummen
July 18th, 2013Last Updated: July 17th, 2013
4 2,790 2 minutes read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Utku
12 years ago

Cool. Thank you very much for this. Especially for β€œstatic” keyword & java annotation config.

0
Reply
Miki
11 years ago

This was very helpful.
Thank you very much! :)

0
Reply
11 years ago

Thumbs up! Thanks.

0
Reply
Walid
9 years ago

Thanks

0
Reply
Back to top button
Close
wpDiscuz