spring boot 配置文件总结

原创 2018年04月14日 23:44:26

最近在用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}

在VC中实现程序在启动时隐藏

    最近经常见论坛上有人问,程序在启动时如何隐藏。以下是我总结的一些方法,欢迎大家讨论,找出更好的方法。    对于这类问题,大家最容易想到的可能就是在PreCreateWindow中添加cs.s...
  • AttaBoy
  • AttaBoy
  • 2002-03-03 09:18:00
  • 1216

spring boot配置文件参数详解

  • 2018年03月26日 13:34
  • 15KB
  • 下载

spring Boot 配置文件详解

概要       前面我们讲述了如何搭建一个简单的 spring Boot 应用(参见Spring Boot - 初探),这里,我们来学习如何对项目进行相关的配置,包括系统构建、自动配置、依赖注入、开...
  • xiangzhihong8
  • xiangzhihong8
  • 2016-11-25 22:43:33
  • 3507

Spring Boot 配置文件介绍

属性文件使用Application/YAMLApplication/YAML 属性文件,按优先级按高到低排序,位置高的将覆盖位置低的    1. 当前目录下的一个/config子目录    2. 当前...
  • Jerome_s
  • Jerome_s
  • 2017-01-10 20:56:45
  • 2475

Spring Boot 配置文件 – 在坑中实践

摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 仓廪实而知礼节,衣食足而知荣辱 - 管仲 』 本文提纲 一...
  • u010378410
  • u010378410
  • 2017-04-21 14:27:43
  • 929

Spring Boot属性配置文件详解

相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷。我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁...
  • u011955252
  • u011955252
  • 2016-11-22 19:41:55
  • 2230

spring boot 使用外部配置文件

一、springboot 启动时加载  application.properties的优先级 1.当前目录的./config目录 2.当前目录 3. classpath /confi...
  • jiashiwen
  • jiashiwen
  • 2017-10-19 16:12:42
  • 804

springboot总结

springboot总结
  • jornada_
  • jornada_
  • 2017-07-21 17:51:50
  • 701

Springboot 之 自定义配置文件及读取配置文件

本文章来自【知识林】读取核心配置文件核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,...
  • zsl129
  • zsl129
  • 2016-10-21 10:38:31
  • 36111

Spring Boot 读取配置文件的几种方式

Spring Boot 读取配置文件的方式可以分为 1. 注解 2. 获取 Spring Boot 的环境变量 来获取配置文件的信息,其中注解的方式又有集中表现形式。第一步:创建 Spring...
  • github_35169934
  • github_35169934
  • 2017-10-14 12:25:48
  • 1249
收藏助手
不良信息举报
您举报文章:spring boot 配置文件总结
举报原因:
原因补充:

(最多只允许输入30个字)