spring boot 配置文件总结

最近在用spring的配置文件 做一下简单的实现总结:

 Spring Java based bean definition file (

@Configuration):
 
01package root;
02 
03...
04 
05@Configuration
06@PropertySource("classpath:root/test.props")
07public class SampleConfig {
08 
09 @Value("${test.prop}")
10 private String attr;
11 
12 @Bean
13 public SampleService sampleService() {
14  return new SampleService(attr);
15 }
16}

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:

01@ContextConfiguration(classes=SampleConfig.class)
02@RunWith(SpringJUnit4ClassRunner.class)
03public class ConfigTest {
04 @Autowired
05 private SampleService sampleService;
06 @Test
07 public void testConfig() {
08  assertThat(sampleService.aMethod(), is("testproperty"));
09 }
10}

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:

01@Configuration
02@PropertySource("classpath:root/test.props")
03public class SampleConfig { 
04 @Value("${test.prop}")
05 private String attr;
06 
07 @Bean
08 public SampleService sampleService() {
09  return new SampleService(attr);
10 }
11 
12 @Bean
13 public PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
14  return new PropertySourcesPlaceholderConfigurer();
15 }
16}

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:

1WARN : org.springframework.context.annotation.ConfigurationClassEnhancer - @Beanmethod 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 forcomplete details

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

01@Configuration
02@PropertySource("classpath:root/test.props")
03public class SampleConfig { 
04 @Value("${test.prop}")
05 private String attr;
06 
07 @Bean
08 public SampleService sampleService() {
09  return new SampleService(attr);
10 }
11 
12 @Bean
13 public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
14  return new PropertySourcesPlaceholderConfigurer();
15 }
16}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页