Skip to content

ClassPathScanningCandidateComponentProvider doesn't respect AnnotationTypeFilter [SPR-12802] #17399

Closed

Description

@spring-projects-issues

Diego Plentz opened SPR-12802 and commented

ClassPathScanningCandidateComponentProvider doesn't find interfaces when used with AnnotationTypeFilter. AnnotationTypeFilter has a constructor that explicitly says that it can be used to find annotated interfaces:

public AnnotationTypeFilter(Class<? extends Annotation> annotationType,
boolean considerMetaAnnotations,
boolean considerInterfaces)
Create a new AnnotationTypeFilter for the given annotation type.
Parameters:
annotationType - the annotation type to match
considerMetaAnnotations - whether to also match on meta-annotations
considerInterfaces - whether to also match interfaces

But, since ClassPathScanningCandidateComponentProvider implementation filter interfaces out in the isCandidateComponent(AnnotatedBeanDefinition beanDefinition) method, the type filter doesn't work as expected.

ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider();
provider.addIncludeFilter(new AnnotationTypeFilter(MyCustomAnnotation.class, false, true));
Set<BeanDefinition> findCandidateComponents = provider.findCandidateComponents("com.foo");

Affects: 4.1.5

Issue Links:

Activity

  1. spring-projects-issues commented on Mar 11, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    At this point, that mechanism works as designed: Our component scanning can only register concrete component classes with an application context, since the bean factory would not know what to do with them otherwise. What would you expect to happen to registered interfaces in your scenario?

    Currently, specified type filters are allowed to select based on simple criteria, with non-concrete classes automatically filtered out still. So we can't simply refine that check in existing branches of the framework, otherwise some existing type filter configurations might break.

    That said, we are considering a revision of the component scanning contract to allow for registering abstract component types, including interfaces, in combination with a mechanism which allows for transforming such components to usable bean classes: see #16286.

    Juergen

  2. spring-projects-issues commented on Mar 11, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Diego Plentz commented

    Juergen,

    I understand that this behaviour is the one expected when registering interfaces for Spring context, but this class is being used by users(me included - and you can quickly find a lot of others users in a quick google search) to scan the classpath, since it's a better dependency to have then https://code.google.com/p/reflections/, if you're already using spring in your project.

    The onde thing that isn't really clear to me is why that AnnotationTypeFilter's constructor exists since ClassPathScanningCandidateComponentProvider always remove the non-concrete interfaces anyway.

  3. added
    type: bug
    status: declined
    in: core
    on Jan 11, 2019
  4. joeltoby commented on May 2, 2019

    @joeltoby

    I agree that the current behaviour of ignoring AnnotationTypeFilters is not intuitive or helpful since developers are using ClassPathScanningCandidateComponentProvider exactly as Diego Plentz outlined.

    Is there now (several years later) any better way to filter ClassPathScanning by annotations? If not, would you consider reopening this please @jhoeller ?

  5. sinogermany commented on Nov 16, 2019

    @sinogermany

    @joeltoby and whoever is looking for a temp solution before #16286 gets merged:

    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false) {
        @Override
        protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
            return super.isCandidateComponent(beanDefinition) || beanDefinition.getMetadata().isAbstract();
        }
    };
  6. joeltoby commented on Dec 3, 2019

    @joeltoby

    Thank you @sinogermany. I'll give that a try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: declined

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions