我对 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?