spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Allow more configuration for ConditionalOnProperty

Open goughy000 opened this issue 1 year ago • 0 comments

I was wanting to make use of @ConditionalOnProperty in a project recently and expected it to evaluate false if the value of the property was null, however it seems that the condition only evaluates false if the property is completely missing, or it's set to the string value false

What I want to propose is that we add another option to the annotation like follows

String[] notHavingValues() default { "false" };

and then in OnPropertyCondition::isMatch we can check against this rather than the hardcoded false value.

For my use case I could then annotate my bean definition as so, whilst also not breaking backwards compatibility

@Bean
@ConditionalOnProperty(name = "my.property", notHavingValues = { "", null })
public Widget widget() {
   return new Widget();
}

Happy to raise a PR, but wanted to get feedback first - thanks for your consideration!

Alternative workarounds are to use a ConditionalOnExpression and write spel

goughy000 avatar Feb 16 '24 18:02 goughy000