Mer*_* Z. 5 java spring spring-boot spring-java-config
我有以下组件类,我想根据属性实例化它;
@Component("componentA")
@PropertySource("classpath:components.properties")
@ConditionalOnExpression("'${components.enabled}'.contains('componentA')")
public class ComponentA implements ComponentIF {
...
Run Code Online (Sandbox Code Playgroud)
components.properties 文件具有以下属性;
components.enabled=componentA,componentB,componentD
Run Code Online (Sandbox Code Playgroud)
问题是@PropertySource("classpath:components.properties")在评估@ConditionalOnExpression("'${components.enabled}'.contains('componentA')").
另一方面,如果我将components.enabled=componentA,componentB,componentD属性放在 spring-boot 的application.properties文件中,则该属性在 ConditionalOnExpression 评估期间可用,并且按预期工作。
但是,我想使用components.properties以将所有组件特定的属性保持在同一位置。
任何想法是否 PropertySource 在 ConditionalOnExpression 期间无效?
我也有同样的问题。就我而言,我想根据条件启用某些方面。看来 spring 在初始化的早期阶段读取了一些值,并且很难覆盖这些值。(比如值@ConditionalOnExpression)。
以便设置这些变量的值。
application.properties在类路径中指定属性。早期阶段加载的变量始终从此文件中读取。application.properties为application-profile.properties并激活相应的配置文件。-Dproperty.key=value.否则,如果您想使用属性文件本身覆盖该属性,我建议您仔细阅读这个答案。
所有这些都可以组合在一起。要了解它们的优先级,请参阅此。
| 归档时间: |
|
| 查看次数: |
755 次 |
| 最近记录: |