Skip to content

Annotation-based filters for component scanning find meta-annotations as well #22551

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

Closed
kingrockw opened this issue Mar 9, 2019 · 4 comments
Closed
Assignees
Labels
for: stackoverflow A question that's better suited to stackoverflow.com in: web Issues in web modules (web, webmvc, webflux, websocket) type: documentation A documentation task
Milestone

Comments

@kingrockw
Copy link

kingrockw commented Mar 9, 2019

<context:component-scan base-package="cn.rock">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"></context:exclude-filter>
</context:component-scan>

Why can't the DispatcherServlet load the @Controller class in Spring MVC config according to the above configuration?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 9, 2019
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Mar 9, 2019
@sbrannen sbrannen self-assigned this Mar 9, 2019
@sbrannen sbrannen added the for: stackoverflow A question that's better suited to stackoverflow.com label Mar 9, 2019
@sbrannen
Copy link
Member

sbrannen commented Mar 9, 2019

Although this kind of question is better suited for Stack Overflow, I'll go ahead and answer your question here.

  1. @Controller is meta-annotated with @Component.
  2. You have configured an exclude filter for classes annotated with @Component.
  3. When specifying type="annotation" with <context:exclude-filter ... />, Spring creates an org.springframework.core.type.filter.AnnotationTypeFilter that by default considers annotations that are directly present or meta-present.

The net result is that you have instructed Spring to exclude from component scanning any classes that are directly annotated with @Component or meta-annotated with @Component (e.g., @Controller, @Service, @Repository, etc.). Thus, the DispatcherServlet does not see your @Controller class, because your @Controller class has been excluded from component scanning.

If you would like to exclude exactly classes annotated directly with @Component, you will need to implement a custom org.springframework.core.type.filter.TypeFilter. The easiest way would be to extend AnnotationTypeFilter and have the default constructor of your subclass delegate to the AnnotationTypeFilter(Class<? extends Annotation>, boolean) -- for example super(Component.class, false).

You can then specify type="custom" and expression="org.example.MyCustomComponentAnnotationTypeFilter" in <context:exclude-filter ... />.

@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 9, 2019
@sbrannen sbrannen closed this as completed Mar 9, 2019
sbrannen added a commit that referenced this issue Mar 9, 2019
Prior to this commit the documentation for annotation-based include and
exclude filters used with component scanning did not explicitly mention
the fact that annotations are considered a match if they are either
present or meta-present on candidate classes.

This commit improves the documentation in this regard.

See gh-22551
@sbrannen
Copy link
Member

sbrannen commented Mar 9, 2019

@kingrockw, thanks to your question I noticed that the documentation was not explicit with regard to the default support for meta-annotations. I have therefore updated the documentation in b109f14.

Cheers!

@sbrannen sbrannen added the type: documentation A documentation task label Mar 9, 2019
@sbrannen sbrannen added this to the 5.2 M1 milestone Mar 9, 2019
@sbrannen sbrannen changed the title component-scan exclude-filter org.springframework.stereotype.Component Annotation-based filters for component scanning find meta-annotations as well Mar 9, 2019
@sbrannen
Copy link
Member

sbrannen commented Mar 9, 2019

As an alternative to my previous proposal to implement a custom TypeFilter, if you are activating the component scanning in an XML configuration file that is solely dedicated to loading web-related beans, you may wish to simply include only classes annotated with @Controller, which you should be able to achieve as follows.

<context:component-scan base-package="cn.rock" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

@kingrockw
Copy link
Author

Thank you very much for your answer. @sbrannen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com in: web Issues in web modules (web, webmvc, webflux, websocket) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants