`@ConditionalOnProperty` 与多个 `@PropertySource` 一起使用 [英] `@ConditionalOnProperty` used with multiple `@PropertySource`
问题描述
我有一些带有 @ConditionalOnProperty
的 bean,其中的属性取自某些 @PropertySource
.但是我有多个 @PropertySources
.只有在其中之一中,才会定义条件的给定属性.但令我惊讶的是,@ConditionalOnProperty
注释会查询每个属性源.由于属性并非在每个属性源中,PropertyResolver
会将 bean 标记为非数学.
I have some beans with @ConditionalOnProperty
, where the property is taken from some @PropertySource
. But I have multiple @PropertySources
. Only in one of them, given property for condition will be defined. But for my surprise, the @ConditionalOnProperty
annotation consults every property source. And since property isn't in every property source, PropertyResolver
will flag bean as non-mathing.
我想要的是接口,具有实际实现和无操作实现,并控制将使用哪个实现.我不想使用配置文件来控制它,但是配置文件是基于条件的,所以应该有一种方法.所以对我来说发生的事情是,我不管设置被遗漏了没有实施.当然,我可以添加matchIfMissing
,然后无论设置如何,我都会被排除在外.
What I'd like to have, is interface, with actual implementation and no-op implementation, and control which implementation will be used. I don't want to control it using profile, but profile is based on conditionals so there should be a way. So what is happening for me is, that I'm regardless of setting left out with no implementation. Sure, I can add matchIfMissing
, and then I will be left out with both, regardless of setting.
注释是:
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
并且在属性文件中是
feature.enabled=true
这里有什么问题?但它真的不可能,如果我使用@Conditional...我必须只使用一个属性源,对吗?
What is wrong here? But it really cannot be, that if I'm using @Conditional... I have to use just one property source, right?
更新:具有以下 bean 定义,我具有所描述的行为:未注册 bean,因为 feature.enabled
未在 application.properties
中定义.添加 matchIfMissing=true
没有帮助,因为带有此参数的 bean 将始终添加,因为它(始终)不存在于 application.properties
中.添加 @ConditionalOnMissingBean 也没有帮助我.我在 another.properties
中将 feature.enabled
设置为 true,但是 FeatureImpl 被否决了,因为有属性源,其中 feature.enabled 不是 true.令人惊讶的是,当时用@ConditionalOnMissingBean 注释的 FeatureNoOp 也没有注册.不知道为什么.
UPDATE:
having following beans definitions, I have behavior as described: no bean is registered, because feature.enabled
is not defined in application.properties
. Adding matchIfMissing=true
does not help, because bean with this parameter will be added always, since it's (always) not present in application.properties
. Adding @ConditionalOnMissingBean did not help me also. I set feature.enabled
to true in another.properties
, but that FeatureImpl got vetoed, because there is property source, where feature.enabled isn't true. And surprisingly FeatureNoOp, at that time annotated with @ConditionalOnMissingBean, wasn't registered either. No idea why.
@Service
@Slf4j
@ConditionalOnProperty(name = "feature.enabled", havingValue = "false")
public class FeatureNoOp implements Feature {
对比
@Service
@Slf4j
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public class FeatureImpl implements Feature {
配置如下:
@Configuration
@PropertySource("classpath:application.properties")
public class Config {
//...
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@PropertySource("classpath:another.properties")
@Profile("war-deployment")
public static class WarDeploymentConfig {
//...
}
}
推荐答案
真正的原因是,正如 M.Deinum 正确建议的那样(谢谢!),在@PropertySource 注释中.
The true cause was, as M.Deinum correctly suggested (thanks!), in @PropertySource annotations.
我有 @PropertySource 用于 application.properties,另外我还有一些配置类,如下所示,只是为了带来基于活动配置文件的另一个属性源:
I had @PropertySource for application.properties, plus I have some configuration classes like one below, just to bring another property source based on active profile:
@Configuration
@PropertySource("classpath:profile-related-properties.properties")
@Profile("some-profile")
public static class SomeProfileConfig {}
即使这提供了一些明确的说明正在使用的内容,它显然也说明了一些问题.我更改了代码,这样我就不会在 application.properties 上重复"声明用法并重命名文件/配置文件,以便可以使用另一个自动发现的属性模式:application-{profile_name}.properties.进行此更改后,一切正常.
even if this provide some explicit saying what's being used, it apparently spells some issues. I changed the code, so that I do not 'repeatedly' declare usage on application.properties and renamed files/profiles so that another automatically discovered properties pattern can be used: application-{profile_name}.properties. After this change, everything works as expected.
这篇关于`@ConditionalOnProperty` 与多个 `@PropertySource` 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!