-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Reject conditional @ComponentScan declarations that rely on the REGISTER_BEAN phase #23206
Comments
Thanks for the report. The behaviour that you have observed is part of Spring Framework. In the first example the unexpected behaviour is occurring because Framework does not consistently consider the conditions on The second example avoids the problem of the first example because In short, we need to move this to spring-projects/spring-framework so that the Framework team can take a look. |
version:5.2.1.RELEASE @ConditionalOnBean(EnableSvcConfiguration.Marker.class)
@ComponentScan("com.github.bzi.service")
@Configuration
public class SvcAutoConfiguration {
} In my case, when i use |
@echooymxq |
Juergen and I brainstormed based on the same above and we've decided to reject the scenario altogether, that is when we detect that Consider the following example: @AutoConfiguration
@ConditionalOnBean(String.class)
@ComponentScan("com.acme")
public class TestAutoConfiguration { ... } When a
When a
In both cases, component scan does not happen. We can see that the condition is evaluated twice there, one at the If the scan is moved to a nested config class that has an outer class with a @AutoConfiguration
@ConditionalOnBean(String.class)
public class TestAutoConfiguration {
@Configuration
@ComponentScan("com.acme")
static class NestedConfiguration {
}
} Then it keeps scanning as reported here. In general, conditional component scan is not a great idea as it has to happen early in the lifecycle of the bean factory setup, so that its effect can apply to other conditions. Mixing a late condition, such as |
The change made in #27077 is "harmonizing" things a little bit. In both cases now the scan is always applied as it runs in the |
This commit introduce a change of behaviour when component scan is used with conditions. Previously, any condition in the REGISTER_BEAN phase were ignored and the scan was applied regardless of the outcome of those conditions. This is because REGISTER_BEAN condition evaluation happens later in the bean factory preparation. Rather than ignoring those conditions, this commit fails fast when it detects such use case. Code will have to be adapted accordingly. Closes spring-projectsgh-23206
spring boot version
2.1.5.RELEASE
TestConfig.java
The package
org.xyattic.boot.calkin.demo.oauth2.test
by default is not scanned.When I activated it under certain conditions, I found that
@ComponentScan
was also activated when the condition was not met.The log shows as not matching.
And the following will not accidentally activate.
The text was updated successfully, but these errors were encountered: