-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Strange behavior when trying to override @Bean
method from parent class and rewrite the bean name
#32522
Comments
The case 1 isn't hard to understand since I found that the bean definitions is registered into But the case 2 is more interesting since it prefer to throw exception and it actually says here is no proper 'getBeanOverridden()'. to construct it. After some code-digging I found it's has something to do with the code below at line 460 Lines 459 to 461 in c04290e
In the first case it gets a In the second case it gets a Lines 466 to 474 in c04290e
At below line, it gets all candidates to generate that bean but ignore the methods in super class if there already found a method with the same signature, so it'll only keeps Line 468 in c04290e
But the Line 470 in c04290e
Then after various checks it decides to throw an Line 611 in c04290e
That Lines 232 to 234 in c04290e
here can have I personally think it is a problem and that only keeping bean in sub class is optimal. but on the opposite side I also think this can rarely become a realistic situation, so if it worthy to change the code takes me into dilemma. |
Duplicate of #28286 |
Affects: 6.1.5
I was trying to override a
@Bean
method from the parent class but with a different bean name, I expected the here would only be one bean generated by method in child, but the result was weird and has something to do whether you are using@Import
or@ComponentScan
.I know this is not a practical usage, but it's still an interesting and intriguing problem.
Example
Case 1: only using
@Import(BeanOverridingChild.class)
It produced two beans; the console output are like:
Case 2: only using
@ComponentScan(basePackageClasses = {TestMain.class})
It thrown an
BeanCreationException
in following output:The text was updated successfully, but these errors were encountered: