【发布时间】: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