春天环境 - 多个?

时间:2015-01-06 20:22:07

标签: spring-boot spring-environment

我对Spring Environment的运作方式感到困惑。我认为它基本上是ApplicationContext中的单例bean,只要我将PropertySources加载到我的AppCtx中,它们就会自动合并到这个 Environment中。但是,我在我的应用程序中看到这种情况多次记录,这意味着AbstractEnvironment的构造函数被多次调用:


2015-01-06 12:16:26,858 DEBUG (main) [org.springframework.core.env.StandardEnvironment] Adding [systemProperties] PropertySource with lowest search precedence
2015-01-06 12:16:26,858 DEBUG (main) [org.springframework.core.env.StandardEnvironment] Adding [systemEnvironment] PropertySource with lowest search precedence
2015-01-06 12:16:26,858 DEBUG (main) [org.springframework.core.env.StandardEnvironment] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]

结果是我做的事情如下:

@Autowire
Environment environment;

String propertyIExpect = environment.getProperty("myprop");

我得到Environment的一个实例,但我预计不存在任何属性。

当我将此XML添加到Spring Boot应用程序上下文中时,我预计它们已添加到此自动连接的Environment中:

<context:property-placeholder location="classpath:/spring/environment/${ctms.env}/application.properties" order="1"/>
<context:property-placeholder location="classpath:build.info" order="2"/>

然后,有时Environment属性就在那里,如此记录中所示:


2015-01-06 12:16:37,433 TRACE (main) [org.springframework.core.env.PropertySourcesPropertyResolver] getProperty("ctms.env", String)
2015-01-06 12:16:37,433 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'ctms.env' in [servletConfigInitParams]
2015-01-06 12:16:37,433 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'ctms.env' in [servletContextInitParams]
2015-01-06 12:16:37,433 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'ctms.env' in [systemProperties]
2015-01-06 12:16:37,433 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Found key 'ctms.env' in [systemProperties] with type [String] and value 'dev'
2015-01-06 12:16:37,438 DEBUG (main) [org.springframework.core.env.MutablePropertySources] Adding [environmentProperties] PropertySource with lowest search precedence
2015-01-06 12:16:37,438 INFO  (main) [org.springframework.context.support.PropertySourcesPlaceholderConfigurer] Loading properties file from class path resource [spring/environment/dev/application.properties]
2015-01-06 12:16:37,438 DEBUG (main) [org.springframework.core.env.MutablePropertySources] Adding [localProperties] PropertySource with lowest search precedence
2015-01-06 12:16:37,443 TRACE (main) [org.springframework.core.env.PropertySourcesPropertyResolver] getProperty("database.connection.url", String)
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [environmentProperties]
2015-01-06 12:16:37,443 TRACE (main) [org.springframework.core.env.PropertySourcesPropertyResolver] getProperty("database.connection.url", String)
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [servletConfigInitParams]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [servletContextInitParams]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [systemProperties]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [systemEnvironment]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [random]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [applicationConfig: [classpath:/application.properties]]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Could not find key 'database.connection.url' in any property source. Returning [null]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Searching for key 'database.connection.url' in [localProperties]
2015-01-06 12:16:37,443 DEBUG (main) [org.springframework.core.env.PropertySourcesPropertyResolver] Found key 'database.connection.url' in [localProperties] with type [String] and value 'jdbc:oracle:thin:@somehost:someport/foo'

注意:我也在Spring Boot日志中看到过两次:

12:19:03,387 INFO  [TomcatEmbeddedServletContainer] Tomcat started on port(s): 8080/http

我会在最后一次预料到这一次。也许这是相关的?我是否以某种方式在Spring Boot应用程序中创建了多个ApplicationContext

2 个答案:

答案 0 :(得分:0)

XML <context:property-placeholder/>未添加到Environment。 DEBUG日志只是噪音,所以可能是可以忽略的。如果您需要Environment,请使用Spring Boot API设置属性位置(或者可能是我们@PropertySource)。

答案 1 :(得分:0)

根据此处发布的建议,我不再从<context:property-placeholder> XML加载属性文件: classpath:/ spring / $ {ctms.env} /application.properties

这是我解决环境属性问题的方法:

由于$ {ctms.env}本质上是我们正在运行的环境,并且也反映在Spring活动配置文件中(例如,dev,test,stage,prod),我最终使用Spring Boot的功能来自动加载活动配置文件属性文件&#34;按照惯例&#34;为了我。

为了澄清,此Spring Boot功能将根据活动配置文件在特定位置查找.properties文件。像这样:

对于Spring Active Profile =&#34; foo&#34;,它会自动加载此文件(如果存在):

WEB-INF/classes/config/application-foo.properties

对于我的解决方案,我在Spring Boot WAR中重新定位并重命名属性文件,如下所示:

/WEB-INF/classes/config/application-${ctms-env}.properties

它基本上最终会像: 对于&#34; dev&#34; Spring Active Profile:

/WEB-INF/classes/config/application-dev.properties

对于&#34;测试&#34; Spring Active Profile:

/WEB-INF/classes/config/application-test.properties

for&#34; stage&#34; Spring Active Profile:

/WEB-INF/classes/config/application-stage.properties

我没有必要重写任何属性文件。我只需要按照Spring Boot&#34; convention&#34;的方式重新打包它们。想要找到/加载它们。然后,当然,停止通过XML方式加载。而且效果很好。