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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值