spring boot@conditionalonproperty未解析属性

aor9mmx1  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(466)

在我的springboot项目(sbv2.4.4)中,我尝试使用 @ConditionalOnProperty :

@ConditionalOnProperty(
        value = "feature1.enabled",
        havingValue = "true",
        matchIfMissing = false
)
@Configuration
public class NewConfig {
    ...
}

财产 feature1.enabled 是在 features.properties 使用第二个配置类加载的文件:

@Configuration
@PropertySource("classpath:features.properties")
public class FeaturesConfig {
}

中声明的属性 features.properties 正确加载,但是 @ConditionalOnProperty 似乎无法解决 feature1.enabled .
我也试着注解 NewConfig 带a的班级 @DependsOn("featuresConfig") ,但没有任何变化。
唯一有效的办法就是搬家 feature1.enabledapplication.properties .
为什么这不起作用?我的配置有什么问题?

oxalkeyp

oxalkeyp1#

正如m.denium在评论中所建议的,我在应用程序启动时添加了以下命令行参数,解决了这个问题:

-Dspring.config.additional-location=classpath:/features.properties

添加 features.properties 文件作为应用程序的附加配置位置,修复了中的属性解析问题 @ConditionalOnProperty .

相关问题