【问题标题】:Spring ConditionalOnProperty cannot read property in custom.properties fileSpring ConditionalOnProperty 无法读取 custom.properties 文件中的属性
【发布时间】:2020-07-22 03:47:38
【问题描述】:

您好,我有一些自定义属性文件,例如 application.properties。我在这些文件中放置了一些属性。我的问题是 @ConditionalOnProperty 无法读取 customproperties 文件中的 myproperties。 如果我将相同的属性放在 application.properties ConditionalOnProperty 有效,但是当我将 myproperty 放在 customproperties 文件中时它不起作用。除了 ConditionalOnProperty 我从自定义属性文件中读取值没有任何问题。有没有办法从Spring Boot 中的 ConditionalOnProperty。

@Congiuration
@PropertySource("classpath:myproperties.properties")
@ConfigurationProperties(prefix="mycustomprop")
public class MyProperties{
private String myproperty;


@Configuration
@ConditionalOnProperty(name = "mycustomprop.myproperty", havingValue = "false") //not work when myproperty in customproperty file
public class MyConfiguration implements WebMvcConfigurer



@SpringBootApplication
@PropertySources({
        @PropertySource("classpath:myproperties.properties")
})

public class MyApplication

【问题讨论】:

    标签: spring spring-mvc properties-file


    【解决方案1】:

    您的 @PropertySource annotation 需要使用 @Configuration 类(如果需要,也可以使用 @SpringBootApplication),而不是您的自定义 MyProperties 对象。

    【讨论】:

    • 我试过启动组件或配置,我也把@PropertSource放到springbootapplication但没有改变......
    • 发布您尝试在配置中使用@propertysource 的完整代码。
    • 我不明白这些是我尝试过的代码。MyProperties 这是自定义属性文件。它有一个属性 myproperty。我想使用@ConditionalOnProperty 注释,但它无法读取此属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    相关资源
    最近更新 更多