【问题标题】:Spring Environment - multiple?Spring 环境 - 多个?
【发布时间】:2015-03-04 14:07:07
【问题描述】:

我对 Spring Environment 的工作方式感到困惑。我认为它基本上是ApplicationContext 中的一个单例 bean,每当我将 PropertySources 加载到我的 AppCtx 中时,它们都会自动组合到这个 single 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 日志中也看到了 2 次:

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

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

【问题讨论】:

  • 您通常会看到 2 个上下文:servlet 和 root。但是,我会怀疑那个“Spring Boot”XML 配置文件中可能还有什么以及它是如何加载的。此类文件的存在可能表明它不是 Spring Boot 应用程序。如果它是一个 Spring Boot 应用程序并且是一个 Spring XML 配置文件,那么您可能会基于该文件加载两个上下文(servlet 和根),并且 Spring Boot 也将加载一个 servlet 和根上下文。跨度>
  • 在使用 Spring Boot 时通常会忽略您的 xml,您应该只使用 @PropertySource 来加载其他属性文件。然而,最简单的解决方案是简单地将您的属性添加到默认加载的 application.properties 文件中。
  • @Steve - 哦,这绝对是一个 Spring Boot 应用程序。事实上,您可能会说这是一个案例研究,如何将遗留应用程序从 JBoss EAP 和 Spring 迁移到单独的 Spring Boot(带有嵌入式 JMS、JNDI、JTA 和 JCA)。我的应用程序已有 10 年历史,约 500K 行,以及超过 250 个 XML 中的 Spring bean,没有时间将它们全部重构为 Java。没有 Web 或 Dispatcher XML。但是,旧的基于 XML 的 bean 使用 Configuration 注释类上的 ImportResource 加载到 Spring Boot 中。他们加载得很好!正是这个环境细节让我感到困惑。这是唯一不能正常加载的东西。

标签: spring-boot spring-environment


【解决方案1】:

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

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

由于 ${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

对于“舞台”Spring Active Profile:

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

等等

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 2012-12-19
      • 2012-01-21
      • 2017-10-26
      • 2015-03-05
      相关资源
      最近更新 更多