-
Notifications
You must be signed in to change notification settings - Fork 41.1k
@ComponentScan in application class overrides default excludeFilters specified in @SpringBootApplication #10933
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
Comments
Your finding is not fully complete, actually. It's not about the exclude not being added, it's about you having an opinion as which packages should be scanned on the source of the context for the test. This section of the 2.0 documentation provides more details. With that in mind, I don't think that we should change anything. |
Well, in fact the documentation that you pointed also does not cover the complete picture as well when it says: "This effectively overrides the default component scan directive with the side effect of scanning those two packages regardless of the slice that you chose.". It gives the impression that scanning both packages is the only side effect. In the case of Stack Overflow question, the package scan is the same for both cases and the reason for adding ComponentScan was to effectively add a custom excludeFilter. Without the default excludeFilters specified in the SpringBootApplication annotation the filter TestTypeExcludeFilter will not be used in the tests and this is what caused the problem and not the fact that it was scanning other things. |
In the context of that section, this is the only side effect. If you think there is a phrasing that would be more complete/accurate, I am happy to review a PR.
It doesn't matter what packages you scan. If you put it in your If you can't, please share more details, perhaps we can improve that then. |
If you specify a If TypeExcludeFilter is missing it will break the tests. I am not sure about what may go wrong when AutoConfigurationExcludeFilter is missing. |
Generally speaking, nothing. This filter just prevents accidentally component scanning on auto-configuration classes that should, instead, be found via entries in
Boot deliberately backs off when we see the user expressing their own opinion. In this case you've expressed an opinion about the exclude filters so the defaults no longer apply. If we always kept the defaults, there'd be no way to switch them off. The approach we've taken means that you can either take complete control and remove the defaults, or you can keep them by explicitly declaring them in your I think we should update the documentation to make it clearer that using |
On the other hand, if you don't want to touch the filters and think the |
Initially in our project we did not have excludeFilters and we were already using |
Another option would be:
|
5 is not an option because |
Option #4 is invalid for the same reason then. |
I don't think there's much we should do here other than add a note to the documentation. |
This enhancement idea is based on the findings after this question in Stack Overflow:
https://stackoverflow.com/questions/47054716/componentscan-in-application-class-breaks-webmvctest-and-springboottest
For me it was not obvious that when adding
@ComponentScan
in the application class it would override the defaults set by@SpringBootApplication
(mainly the defaults set for excludeFilters).I think there are multiple possibilities to make this clearer:
When Spring Boot finds a
@ComponentScan
in the application class and it does not have the two default exclude filters (TypeExcludeFilter and AutoConfigurationExcludeFilter) it should throw an exception. An attribute could also be added to@SpringBootApplication
to let the developer bypass this check if desired.Would it make sense to simply deny the use of
@ComponentScan
in the application class?Spring Boot could automatically consider the two default filters even if they are not specified in the
@ComponentScan
annotation when it is in the application class.Would it make sense to move the default exclude filters from the
@SpringBootApplication
annotation to the@ComponentScann
annotation?The text was updated successfully, but these errors were encountered: