ConditionalOnExpression评估期间PropertySource不可用 [英] PropertySource not available during ConditionalOnExpression evaluation

查看:110
本文介绍了ConditionalOnExpression评估期间PropertySource不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要根据属性实例化的以下组件类;

@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的值).

为了设置这些变量的值.

  1. 在类路径的application.properties中指定属性.始终从此文件中读取在早期阶段加载的变量.
  2. 使用配置文件并将application.properties定义为application-profile.properties并激活相应的配置文件.
  3. 将属性值作为JVM参数传递,例如-Dproperty.key=value.

否则,如果您想使用属性文件本身覆盖属性,我建议您通过此答案./p>

所有这些都可以组合在一起.

components.properties file has the following property;

components.enabled=componentA,componentB,componentD

The problem is that @PropertySource("classpath:components.properties") seems to be not available during the evaluation of @ConditionalOnExpression("'${components.enabled}'.contains('componentA')").

On the other hand, if I put the 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.

However, I'd like to use components.properties in order to keep the all component specific properties in the same place.

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 @ConditionalOnExpression).

In order to set the values of these variables.

  1. Specify the property in application.properties in your classpath. The variables loaded at the earlier stages are always read from this file.
  2. Use profiles and define application.properties as application-profile.properties and activate the respective profile.
  3. Pass the property value as a JVM parameter like -Dproperty.key=value.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆