ConditionalOnExpression评估期间PropertySource不可用 [英] PropertySource not available during ConditionalOnExpression evaluation
问题描述
我有以下要根据属性实例化的以下组件类;
@Component("componentA")
@PropertySource("classpath:components.properties")
@ConditionalOnExpression("'${components.enabled}'.contains('componentA')")
public class ComponentA implements ComponentIF {
...
components.properties文件具有以下属性;
components.enabled=componentA,componentB,componentD
问题是在评估@ConditionalOnExpression("'${components.enabled}'.contains('componentA')")
期间@PropertySource("classpath:components.properties")
似乎不可用.
另一方面,如果将components.enabled=componentA,componentB,componentD
属性放在spring-boot的application.properties
文件中,则该属性在ConditionalOnExpression评估期间可用,并且可以按预期工作.
但是,我想使用components.properties
以便将所有组件特定的属性都保留在同一位置.
有什么想法,PropertySource是否在ConditionalOnExpression期间无效?
我也遇到了同样的问题.就我而言,我想根据条件启用某些方面.似乎spring在初始化的早期阶段读取了一些值,并且很难覆盖这些值. (如@ConditionalOnExpression
的值).
为了设置这些变量的值.
- 在类路径的
application.properties
中指定属性.始终从此文件中读取在早期阶段加载的变量. - 使用配置文件并将
application.properties
定义为application-profile.properties
并激活相应的配置文件. - 将属性值作为JVM参数传递,例如
-Dproperty.key=value
.
否则,如果您想使用属性文件本身覆盖属性,我建议您通过此答案./p>
所有这些都可以组合在一起. components.properties file has the following property; The problem is that On the other hand, if I put the However, I'd like to use Any ideas whether PropertySource is not effective during ConditionalOnExpression? I also had the same problem. In my case, I wanted to enable certain Aspects based on condition. It seems that spring reads some values during the earlier stages of initialization and it's difficult to override those values. (like values of In order to set the values of these variables. Else If you wanted to override the property using property files itself I would advise you to go through this answer. All these can be combined all together. To know about their precedence refer this. 这篇关于ConditionalOnExpression评估期间PropertySource不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!components.enabled=componentA,componentB,componentD
@PropertySource("classpath:components.properties")
seems to be not available during the evaluation of @ConditionalOnExpression("'${components.enabled}'.contains('componentA')")
. components.enabled=componentA,componentB,componentD
property inside of the spring-boot's application.properties
file, the property becomes available during ConditionalOnExpression evaluation and it's working as expected.components.properties
in order to keep the all component specific properties in the same place.@ConditionalOnExpression
).
application.properties
in your classpath. The variables loaded at the earlier stages are always read from this file.application.properties
as application-profile.properties
and activate the respective profile.-Dproperty.key=value
.