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:
- Jira record for this issue
- @Bean Javadoc
- A related issue in Stackoverflow
Thanks! This worked for me.
ReplyDeleteThanx! Spent some hours investigating why properties are nulls. Adding static for bean declaration helped.
ReplyDeleteThanks! Helped a lot, especially adding static for bean.
ReplyDeleteThanks! Very useful for me!
ReplyDeleteI can't see SampleService claas in above example ?
ReplyDeleteThank you God for Biju Kunjummen's Blog
ReplyDelete