1

Prior to the update from Spring Boot 2.0.3 to 2.2.4 we had some annotations like the following example:

import org.springframework.stereotype.Component;

@Component
public @interface Adapter {
}

During ComponentScan classes annotated with this Adapter annotation got picked up and added to the Spring context. After the update there are now org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'XY' available.

According to a passage in the Terminology of the Spring Annotation Programming Model regarding Stereotype Annotations our approach should work:

Any component annotated with @Component is a candidate for component scanning. Similarly, any component annotated with an annotation that is itself meta-annotated with @Component is also a candidate for component scanning.

I read through the release notes of Spring Boot 2.1 and 2.2 and the release notes of the Spring Framework 5.1 and 5.2, but found nothing that the behaviour of stereotype annotations changed in this point.

When I annotate the classes with a Spring provided stereotype annotation (in addition to our own annotation) they get picked up by the ComponentScan.

I also tried to set a custom filter in a @ComponentScan like this

@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
    classes = Adapter.class))

but still got the NoSuchBeanDefinitionException

At last I stumbled upon this "Enhancement" in the Spring Framework and wonder if this could be the root of my problem.

So nothing worked so far and I would be glad if someone could point me in a direction that restores the original behaviour that our own stereotype annotations are working again. Thanks in advance.

C. Weber
  • 907
  • 5
  • 18

1 Answers1

1

After taking a closer look at the Spring Stereotype annotations adding @Retention(RetentionPolicy.RUNTIME) to our annotations solved the issue. The classes with our own annotations get picked up by ComponentScan again.

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.springframework.stereotype.Component;

@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface Adapter {
}
C. Weber
  • 907
  • 5
  • 18
  • I struggled a lot with exactly this issue (upgrading from and to the exact versions) and that was a valuable reminder to always look at the implementations. On your `Adapter` though, I believe `Component` is not needed. – Manuel Pap Oct 13 '21 at 13:30