-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce dedicated annotation to deal with boolean property conditions #43704
Comments
I'd like the rest of the team to review https://github.com/philwebb/spring-boot/tree/gh-43704 before we merge. |
Nice work, one small suggestion, maybe the name of annotations should reflect that
|
Honestly, when I saw those annotations for the first time, I had no clue which one I should use and when until I read Javadoc. My suggestion is to have two annotations: @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@ConditionalOnProperty(name = "enabled", havingValue = "true")
public @interface ConditionalOnEnabledProperty {
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "prefix")
String value();
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "prefix")
String prefix();
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "matchIfMissing")
boolean matchIfMissing () default false;
} @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@ConditionalOnProperty(name = "enabled", havingValue = "false")
public @interface ConditionalOnNotEnabledProperty {
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "prefix")
String value();
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "prefix")
String prefix();
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "matchIfMissing")
boolean matchIfMissing () default false;
} In that case, it would be possible to cover all possible outcomes: //spring.jmx.enabled property must be present and has 'true' value
@ConditionalOnEnabledProperty("spring.jmx")
static class JmxConfiguration{}
//spring.jmx.enabled property may be empty but if present must have 'true' value
@ConditionalOnEnabledProperty(prefix = "spring.jmx", matchIfMissing = true)
static class JmxConfiguration{}
//-------------------------------
//spring.jmx.enabled property must be present and has 'false' value
@ConditionalOnNotEnabledProperty("spring.jmx")
static class JmxConfiguration{}
//spring.jmx.enabled property may be empty but if present must have 'false' value
@ConditionalOnNotEnabledProperty(prefix = "spring.jmx", matchIfMissing = true)
static class JmxConfiguration{} |
+1 for a pair of annotations, although I'd maybe use
Not 100% sure on this. Does I wonder if the I also wonder about renaming |
I was thinking about
Exactly
For me, both |
Thanks for all the feedback!
I didn't think we could alias a
I'm not so keen on I'm starting to wonder if we should have a single dedicated annotation and always be explicit about what we want. It might be more verbose, but it will at least be clear. The annotations in my branch map to one of three options: @ConditionalOnProperty(name = "enabled", matchIfMissing = true, havingValue = "true")
@ConditionalOnProperty(name = "enabled", matchIfMissing = false, havingValue = "true")
@ConditionalOnProperty(name = "enabled", matchIfMissing = false, havingValue = "false") I'm thinking perhaps an enum attribute value on a single annotation might work well: @ConditionalOnEnabledProperty(prefix="spring.jmx", havingValue=Enabled.TRUE)
@ConditionalOnEnabledProperty(prefix="spring.jmx", havingValue=Enabled.TRUE_OR_MISSING)
@ConditionalOnEnabledProperty(prefix="spring.jmx", havingValue=Enabled.FALSE) |
I am wondering if the following syntax could be used: @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
//custom condition (alias for `@ConditionalOnProperty.havingValue` does not work (type mismatch) )
@Conditional(OnEnabledPropertyCondition.class)
public @interface ConditionalOnEnabledProperty {
String prefix();
boolean matchIfMissing() default false;
Enabled havingValue() default Enabled.TRUE;
enum Enabled {
FALSE, TRUE
}
}
@ConditionalOnEnabledProperty(prefix = "management.tracing.baggage")
@ConditionalOnEnabledProperty(prefix = "server.servlet.encoding", matchIfMissing = true)
@ConditionalOnEnabledProperty(prefix = "management.tracing.baggage", havingValue = Enabled.FALSE) |
Here's an updated proposal that uses a single |
I like With a @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@ConditionalOnBooleanProperty(name = "enabled")
public @interface ConditionalOnEnabledProperty {
@AliasFor(annotation = ConditionalOnBooleanProperty.class)
String prefix();
@AliasFor(annotation = ConditionalOnBooleanProperty.class)
boolean matchIfMissing() default false;
@AliasFor(annotation = ConditionalOnBooleanProperty.class)
boolean havingValue() default true;
} |
I didn't add it because we've been moving away from using prefixes, but it's easy enough to put back. I'm not convinced we should have a |
As shown in #43641 we have inconsistencies with the way we use
@ConditionalOnProperty
for enabled annotations. Introducing a dedicated set of annotations will help remove the inconsistencies and allow us to identify more easily configurations which depend on.enabled
properties.The text was updated successfully, but these errors were encountered: