1

我对 Spring Environment 的工作方式感到困惑。我认为它基本上是一个单例 bean,ApplicationContext每当我加载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,但其中没有我期望存在的属性。

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

<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 日志中也看到了 2 次:

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

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

4

2 回答 2

1

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

于 2015-01-07T15:13:46.117 回答
0

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

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

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

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

对于 Spring Active Profile = "foo",它将自动加载此文件(如果存在):

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

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

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

它基本上最终是这样的:对于“开发” Spring Active Profile:

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

对于“测试” Spring Active Profile:

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

对于“stage” Spring Active Profile:

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

等等

我不必重新编写任何属性文件。我只需要按照 Spring Boot“约定”想要查找/加载它们的方式重新打包它们。然后,当然,停止通过 XML 方式加载。而且效果很好。

于 2015-01-11T17:35:10.533 回答