3

I have a qualifier annotation that I use to autowire a specific bean like this:

@Autowired
@MyAnnotation
private MyClass myClass;

@Bean
@MyAnnotation
public MyClass myClass()
{
    return new MyClassImpl();
}

@Retention( RetentionPolicy.RUNTIME )
@Qualifier
public @interface MyAnnotation
{
}

The above works, but I want to make sure that the annotation can only be used on classes that implement the MyClass interface. How would I do this?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • 1
    Not trivially. Write an annotation processor for compile time validation or subclass `AutowiredAnnotationBeanPostProcessor` (you'll have to touch more than that iirc) and implement it to fail on fields of the wrong type when that field is annotated with the qualifying `@MyAnnotation`. – Sotirios Delimanolis Jan 20 '18 at 01:34

0 Answers0