设置
- 春季启动 2.6.0
- Spring Cloud 配置 3.1 RC1
- 阿帕奇 Maven 3.8.x
- 开放JDK 11
概述
我有一个使用以下模块设置的多模块 Apache Maven 项目:
- bootstrap:包含一个
PropertySourceLocator
forBootstrapConfiguration
,在spring.factories
文件中定义。 - starter:依赖于引导程序,它是一个(基于 servlet 的)Web 应用程序
- 参考:使用 Maven Cargo 插件部署启动应用程序,部署到 Apache Tomcat 9.0.55
运行
- starter 模块声明了一个配置类,用
@PropertySource("wa.properties")
. 这个wa.properties
在启动模块的类路径上有一个设置:cas.authn.syncope.name=Starter
- 启动模块有一个在构建 spring 应用程序时
ServletInitializer
将spring.config.name
属性设置为“wa”。 - 参考模块在类路径中只有一个
wa-embedded.properties
文件,其设置如下:cas.authn.syncope.name=Embedded
- 参考模块从弹簧激活的配置文件开始:
embedded,all
注意:cas.authn.syncope.name
绑定到 Java POJO,CasConfigurationProperties
,用 注释@ConfigurationProperties("cas")
。
观察
应用程序中存在以下 bean,为本文简化:
@Bean
@ConditionalOnMissingBean(name = "something")
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
public Something something(ApplicationContext ctx, CasConfigurationProperties cas) {
...
}
- 如果我查看 的内容
cas.getAuthn().getSyncope().getName())
,它会显示:“Starter” - 如果我看
ctx.getEnvironment().getProperty("cas.authn.syncope.name")
,它显示“嵌入”。
换句话说,在引导过程中使用的属性绑定与应用程序上下文的实际环境不匹配。
分析
似乎在创建引导应用程序上下文时,
wa-embedded.properties
不会读取特定于配置文件的属性。事实上,用于绑定的唯一属性源是wa.properties
“localProperties”的一部分,我相信它来自@PropertySource("wa.properties")
. 没有其他内容被读取或发现。然后,属性绑定发生绑定
CasConfigurationProperties
并cas.authn.syncope.name
从@PropertySource("wa.properties")
. 此属性的值设置为Starter
。然后,初始化应用程序 servlet 上下文,并使用配置文件对其环境进行后处理,并创建适当的侦听器和 Spring bean。特别是,这个bean:
@Bean
@ConditionalOnMissingBean(name = "something")
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
public Something something(ApplicationContext ctx, CasConfigurationProperties cas) {
...
}
...显示这ctx
是具有通过所有配置文件进行后处理并显示ctx.getEnvironment().getProperty("cas.authn.syncope.name")
为“嵌入式”的环境的实际应用程序上下文。
但是,CasConfigurationProperties
仅使用 Bootstrap 上下文进行处理,其等效属性显示“Starter”。
...这意味着 bean 将使用CasConfigurationProperties
.
研究
此设置使用 Spring Boot 2.5.6 和 Spring Cloud 3.0.5 可以正常工作。我认为 Spring Boot 中的任何更改都不会影响这一点,但我确实看到 Cloud 3.0 和 3.1 之间存在许多差异。
我不确定我是否可以创建一个复制器来充分展示这一点。我会尽力。同时,您能否对此进行评估,看看这是否可能被视为错误或某种错误配置?