3

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?

1
  • 1
    Because the properties with @PropertySource are 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. Commented Apr 19, 2021 at 8:52

1 Answer 1

2

As suggested by M.Denium in his comment, I solved the issue adding this command line argument on application launch:

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

adding features.properties file as an additional configuration location for my application, fixes the property resolution issue in @ConditionalOnProperty.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.