In my Spring Boot project (SB v2.4.4) I'm trying to conditionally enable a configuration class using @ConditionalOnProperty:
@ConditionalOnProperty( value = "feature1.enabled", havingValue = "true", matchIfMissing = false ) @Configuration public class NewConfig { ... } Property feature1.enabled is declared in a features.properties file that is loaded using a second configuration class:
@Configuration @PropertySource("classpath:features.properties") public class FeaturesConfig { } Properties declared in features.properties are correctly loaded, but @ConditionalOnProperty doesn't seem to resolve feature1.enabled.
I also tried to annotate NewConfig class with a @DependsOn("featuresConfig"), but nothing changes.
The only thing that seems to work is to move feature1.enabled in application.properties.
So why this is not working? What's wrong with my configuration?
@PropertySourceare loaded after all@Conditionals are checked. So putting that in a properties file that isn't loaded as part of Spring Boot won't work. You will need to add it as additional properties file to spring boot using the proper commandline arguments.