Skip to content
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

ClassPathScanningCandidateComponentProvider behaviour not scanning annotated classes anymore (spring-context 5.1.17 -> 5.2.8) #25726

Closed
jplatasv opened this issue Sep 7, 2020 · 2 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue

Comments

@jplatasv
Copy link

jplatasv commented Sep 7, 2020

demo-annotationtypefilter.zip
Hi, first time here. Please bear with me if this report has to be fixed somehow.

We have recently upgraded Spring Boot 2.1.16 to 2.3.3 and we have found differences in the behaviour of ClassPathScanningCandidateComponentProvider which are breaking something we had working.

Essentially, we have a @Configuration class implementing ResourceLoaderAware, EnvironmentAware, ImportBeanDefinitionRegistrar which includes an snippet like this one:

	ClassPathScanningCandidateComponentProvider scanner = getScanner();
	AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(HappyAnnotation.class);
	scanner.addIncludeFilter(annotationTypeFilter);
	scanner.setResourceLoader(this.resourceLoader);
	Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents("com");

With this, we would be able to find interfaces in the classpath annotated with @HappyAnnotation, to which we would do some operation later to have them registered in the Spring context. Now the candidateComponents is empty, so nothing will be registered.

We have noticed several things:

  • ClassPathScanningCandidateComponentProvider makes use of a SimpleMetadataReader at some point. The implementation for it has changed.
  • The former implementation for SimpleMetadataReader made use of a visitor called AnnotationMetadataReadingVisitor. It has been deprecated and SimpleAnnotationMetadataReadingVisitor is being used instead.
  • The new implementation of SimpleMetadataReader ends up with an attribute annotationMetadata of type SimpleAnnotationMetadata, which at the same time has annotations (TypeMappedAnnotations) with an annotationFilter being shown in debug as "All annotations filtered", which suggests all annotations are ignored.
  • I have seen that AnnotationMetadataReadingVisitor is mentioned to be deprecated and replaced for the other one but no public replacement for it is being provided, as per documentation. I am not really sure this pattern we use is valid anymore.

I attach an small project that reproduces the issue. Just change the commented spring boot parent version in the pom for the other one to reproduce.

If not a bug and if time allows, please forward me to some documentation which explains how to get our Set<BeanDefinition> correctly in this version.

Thanks in advance,

JP

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 7, 2020
@snicoll
Copy link
Member

snicoll commented Sep 7, 2020

Thanks for the sample, that is very useful.

@HappyAnnotation does not have any retention. Adding @Retention(RetentionPolicy.RUNTIME) makes it work with Spring Boot 2.3.3. Arguably, such setup has always been invalid considering that the annotation should be discoverable at runtime.

@snicoll snicoll closed this as completed Sep 7, 2020
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 7, 2020
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Sep 14, 2020
@sbrannen
Copy link
Member

This is indeed a duplicate of #23901, and the change is documented in the Core Container section of the Upgrading to Version 5.2 notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants