首页 > 解决方案 > Spring ConditionalOnProperty 无法读取 custom.properties 文件中的属性

问题描述

嗨,我有一些自定义属性文件,例如 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

标签: springspring-mvcproperties-file

解决方案


@PropertySource annotation需要继续@Configuration上课(或@SpringBootApplication,如果您愿意),而不是您的自定义 MyProperties 对象。


推荐阅读