Spring Boot annotation attribute overriding doesn't work on beans

Viewed 119

I'm trying to create a custom annotation for enabling functionality based on feature flags. I'm basing my approach on the ConditionalOnProperty annotation, but to enable code reuse I want to define a common prefix that can be used wherever I need this functionality.

I have defined my annotation as below.

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@ConditionalOnProperty(prefix = "foo.bar")
public @interface ConditionalOnFeature {
    @AliasFor(annotation = ConditionalOnProperty.class, attribute = "value")
    String[] value();
}

I'm using the AliasFor annotation to set the value attribute in the ConditionalOnProperty annotation, and hardcoding my prefix as foo.bar (all my features will sit under this property). As far as I understand Spring annotation overriding, this should work.

However when I use this on a class (for example a controller), the code fails to compile, stating that The name or value attribute of @ConditionalOnProperty must be specified.

@RestController
@ConditionalOnFeature("enable-fancy-controller")
public class FancyController {

    @RequestMapping(value = "/fancy-method", method = RequestMethod.GET)
    public String fancyMethod() {
        return "Fancy";
    }
}

However it works perfectly fine when I instead use my annotation on a method. Compiles successfully and behaves as intended.

@RestController
public class FancierController {

    @ConditionalOnFeature("enable-fancier-method")
    @RequestMapping(value = "/fancier-method", method = RequestMethod.GET)
    public String fancierMethod() {
        return "Fancier";
    }
}

Have I configured my annotation wrong? Or is this some bug with Spring?

0 Answers
Related