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

excludeFilters of @ComponentScan ignored since Spring Framework 5.2 #24263

Closed
rob-valor opened this issue Dec 24, 2019 · 7 comments
Closed

excludeFilters of @ComponentScan ignored since Spring Framework 5.2 #24263

rob-valor opened this issue Dec 24, 2019 · 7 comments
Assignees
Labels
status: duplicate A duplicate of another issue type: regression A bug that is also a regression

Comments

@rob-valor
Copy link

Since Spring Boot 2.2.0 the excludeFilters of @ComponentScan is ignored when loading the Spring Boot application context.

When using a configuration class, loaded with spring.factories which does a @ComponentScan for the "framework" it provides, the application context is loading configuration classes which should be excluded (added to the excludeFilters). In version 2.1.11 all works fine. Starting from 2.2.0 the excluded class is loaded causing the context to fail.

I added a small maven project to reproduce the issue. Switch parent pom version to 2.2.0 (or later) and maven build will fail. It looks like a Spring Boot (test context?) issue. Tests using a standard Spring context are not affected (common-framework-boot-supportmodule), the @SpringBootTest tests are (application module).
spring-componentscan-issue.zip

@snicoll
Copy link
Member

snicoll commented Dec 25, 2019

Thanks for the sample. Debugging shows me that the exclude filter is read and added to the scanner but scan includes the class anyway. Moving to spring-framework for further inspection as I don't see how it could be related to Spring Boot.

@snicoll snicoll transferred this issue from spring-projects/spring-boot Dec 25, 2019
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 25, 2019
@rob-valor
Copy link
Author

rob-valor commented Dec 25, 2019

Indeed, sometimes the most basic things tend to be skipped when facing/solving a software issue 😊 . Yes, also@SpringBootApplication also does a classic @ComponentScan. I modified the example to (1) not fail the context having a cleaner test failure instead of blowing up and (2) build a classic spring context by also adding a @ComponentScan to the common-framework-boot-support module which, without having Spring Boot on the classpath, also fails to properly apply the excludeFilters.
spring-componentscan-issue_update.zip

It's just the exclude filter that is not applied with nothing special involved affecting the behavior of the component scanning. simple-example.zip has a simple context with 1 class to scan and 1 class to exclude. Failing test starting from Spring 5.2

@rob-valor
Copy link
Author

The issue seems to be in AnnotationTypeFilter#matchSelf. The AnnotationMetadata implematation is completely different (5.2: SimpleAnnotationMetadata, 5.1: AnnotationMetadataReadingVisitor) and apparently missing valuable class level annotation information. AnnotationMetadata#hasAnnotation returns false when looking for my excludeFilters marker annotation which it does not in Spring 5.1.

Hope this helps.

@snicoll
Copy link
Member

snicoll commented Dec 26, 2019

@rob-valor great detective work! I suspected indeed the new annotation API retrieval to have a regression in this area. Thanks for the updated sample.

@snicoll snicoll added type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 26, 2019
@snicoll snicoll added this to the 5.2.3 milestone Dec 26, 2019
@sbrannen sbrannen self-assigned this Dec 26, 2019
@sbrannen
Copy link
Member

The solution is to ensure that your annotation is properly retained at runtime.

If you declare @DoNotScan as follows, your test passes as expected.

@Retention(RetentionPolicy.RUNTIME)
public @interface DoNotScan {
}

This is effectively a duplicate of #23901.

See #23901 (comment) for details, specifically:

As of Spring Framework 5.2, all annotations are now required to be annotated with @Retention(RetentionPolicy.RUNTIME) in order for Spring to see them.

I am therefore closing this issue.

@sbrannen sbrannen added the status: duplicate A duplicate of another issue label Dec 26, 2019
@sbrannen sbrannen removed this from the 5.2.3 milestone Dec 26, 2019
@sbrannen sbrannen changed the title excludeFilters of @ComponentScan ignored since version 2.2.0 excludeFilters of @ComponentScan ignored since Spring Framework 5.2 Dec 26, 2019
@rob-valor
Copy link
Author

rob-valor commented Dec 26, 2019

We'll update our code then. Thanks. Apparently I didn't read the complete part of core container
in Upgrading to Version 5.2. Sorry guys 😉

@plum117
Copy link

plum117 commented Jan 10, 2020

Thanks I had the same issues, adding @Retention(RetentionPolicy.RUNTIME) solved it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

5 participants