Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot @ConditionalOnProperty not resolving property

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?

like image 252
davioooh Avatar asked Mar 12 '26 02:03

davioooh


1 Answers

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.

like image 152
davioooh Avatar answered Mar 14 '26 16:03

davioooh