我已将我的应用程序从1.2.7(和第4.2.2版)升级到1.3.0(使用4.2.3)。
现在我已经看到我的一些外部化配置没有按预期工作。
例如,我有一个属性值my.property=aaa
,它在我的application.yml
中定义(包含在类路径根目录下的jar文件中)。
现在,如果我从命令行启动我的应用程序,我使用这样的外部化配置文件:
java -jar my-app.jar --spring.config.location=file:/mytest.properties
包括并覆盖my.property=bbb
。在Spring Boot 1.2.7中,结果是bbb
- 正如预期的那样。但是在1.3.0中我得到了my.property=aaa
!?
如果我调用GET:/env
端点,我的配置顺序发生了变化。
在1.2.7我得到*):
...
"applicationConfig: [classpath:/application-test.yml]": {...},
"applicationConfig: [file:/mytest.properties]": {...},
"applicationConfig: [classpath:/application.yml]": {...}
...
*)这也不是我所期望的,因为file:/
应该是第一个。因此,在我看来,JSON响应中的排序与弹簧属性解析器机制不完全相同。
而不是:
...
"applicationConfig: [classpath:/application.yml]#default": {...},
"applicationConfig: [classpath:/application-test.yml]": {...},
"applicationConfig: [file:/mytest.properties]": {...}
...
我在更改日志中遗漏了一件事吗?还有其他属性可以设置为先前的订购吗?
编辑:
启用一些弹簧调试记录后,我发现它似乎没有特定的1.3.0问题 - 而且我的配置工作的情况(即使是1.2.7)只是幸运的巧合......每次重启时订购更改。有一次我得到了我期望的配置。第二次完全不同。代码或我的启动脚本没有变化 - 只需停止/启动我的应用程序......
我在日志中看到了这一点:
1.2.7中的(相关是此日志中的最后一行 - 这正是我所期望的):
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [commandLineArgs] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [systemProperties] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [systemEnvironment] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [random] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [classpath:/application-pretest.yml]] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [file:/xxx/service/xxx/properties/xxx_pretest.properties]] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [classpath:/application.yml]] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.MutablePropertySources - Adding [localProperties] PropertySource with lowest search precedence
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [commandLineArgs]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [servletConfigInitParams]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [servletContextInitParams]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [systemProperties]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [systemEnvironment]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [random]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [applicationConfig: [classpath:/application-pretest.yml]]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [applicationConfig: [file:/xxx/service/xxx/properties/xxx_pretest.properties]]
2015-11-18 19:17:39,141 DEBUG core.env.PropertySourcesPropertyResolver - Found key 'xxx.entitlement.endpoint.url' in [applicationConfig: [file:/xxx/service/xxx/properties/xxx_pretest.properties]] with type [String] and value
'https://xxx:1111/services/EntitlementService'
1.3.0中的(最有趣的是此日志中的最后一行 - 意外结果):
2015-11-18 19:34:40,608 DEBUG core.env.MutablePropertySources - Adding [environmentProperties] PropertySource with lowest search precedence
2015-11-18 19:34:40,608 DEBUG core.env.MutablePropertySources - Adding [localProperties] PropertySource with lowest search precedence
2015-11-18 19:34:40,632 DEBUG core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
2015-11-18 19:34:40,632 DEBUG core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
2015-11-18 19:34:40,633 DEBUG core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2015-11-18 19:34:41,044 DEBUG core.env.MutablePropertySources - Adding [commandLineArgs] PropertySource with lowest search precedence
2015-11-18 19:34:41,044 DEBUG core.env.MutablePropertySources - Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-11-18 19:34:41,044 DEBUG core.env.MutablePropertySources - Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-11-18 19:34:41,045 DEBUG core.env.MutablePropertySources - Adding [systemProperties] PropertySource with lowest search precedence
2015-11-18 19:34:41,045 DEBUG core.env.MutablePropertySources - Adding [systemEnvironment] PropertySource with lowest search precedence
2015-11-18 19:34:41,045 DEBUG core.env.MutablePropertySources - Adding [random] PropertySource with lowest search precedence
2015-11-18 19:34:41,045 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [classpath:/application.yml]#default] PropertySource with lowest search precedence
2015-11-18 19:34:41,045 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [classpath:/application-pretest.yml]] PropertySource with lowest search precedence
2015-11-18 19:34:41,045 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [file:/xxx/service/xxx/properties/xxx_pretest.properties]] PropertySource with lowest search precedence
2015-11-18 19:34:41,045 DEBUG core.env.MutablePropertySources - Adding [localProperties] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [commandLineArgs] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [systemProperties] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [systemEnvironment] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [random] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [classpath:/application.yml]#default] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [classpath:/application-pretest.yml]] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [applicationConfig: [file:/xxx/service/xxx/properties/xxx_pretest.properties]] PropertySource with lowest search precedence
2015-11-18 19:34:41,046 DEBUG core.env.MutablePropertySources - Adding [localProperties] PropertySource with lowest search precedence
...
2015-11-18 19:34:42,811 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [environmentProperties]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [commandLineArgs]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [servletConfigInitParams]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [servletContextInitParams]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [systemProperties]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [systemEnvironment]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [random]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [applicationConfig: [classpath:/application.yml]#default]
2015-11-18 19:34:42,812 DEBUG core.env.PropertySourcesPropertyResolver - Found key 'xxx.entitlement.endpoint.url' in [applicationConfig: [classpath:/application.yml]#default] with type [String] and value 'https://localhost:9448/service
s/EntitlementService'
不幸的是,在启用弹簧'core.env'记录后,整个属性解析机制对我来说非常可疑。我看到大量的DEBUG core.env.MutablePropertySources - Adding [...] PropertySource with lowest search precedence
输出与DEBUG core.env.PropertySourcesPropertyResolver - Searching for key 'xxx.entitlement.endpoint.url' in [...]
混合在一起。我希望在Adding [...]
块之后只有一个Searching for key ...
和的块。但不要混在一起。但我想知道的是为什么我的外部配置文件获得最低优先级而不是最高优先级!?
答案 0 :(得分:0)
我在迁移到1.3.0后以下列方式对此进行了测试.RELEASE:
使用包含一个配置键的捆绑application.properties
创建可运行的jar:
server.context路径= /默认
使用覆盖的配置属性在application.properties
中创建另一个/tmp
:
server.context路径= /超驰
运行jar:java -jar -Dspring.config.location=/tmp/application.properties my-application.jar
现在可以在http://localhost:8080/override/
处找到应用程序。
在本实验中,结果与1.2.7.RELEASE一致。