spring-framework
spring-framework copied to clipboard
Fix: type matching for request-scope generic beans
This PR fixes an issue where @MockBean fails to work with request-scoped Supplier<T> without explicit bean name.
Issue
When using @MockBean with a request-scoped generic bean (especially Supplier<T>), Spring fails to match the bean by type unless the bean name is explicitly specified. This happens because the type matching algorithm doesn't properly handle the generic type information for scoped proxy beans.
Solution
The solution enhances AbstractBeanFactory.isTypeMatch method to check for beans with a scope and look up their corresponding target bean definitions. When a scoped proxy bean is found, the method tries to match the type against the target bean's resolvable type.
Testing
Added a test case that verifies type matching works correctly for scoped proxy beans with generic types.
Fixes gh-30043
Is it better to include the Apache library to obtain this syntactic sugar? This utility would reduce a lot of boilerplate code. Or do you prefer the 10 lines of boilerplate code instead of the 1 line that's actually needed?
There is obviously a need for this, so when we can't copy it, can we rely on other open-source frameworks providing the feature already?
Thanks for the advisory. @bclozel
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ObjectUtils.html#firstNonNull(T...)
Hi, no updates? @rstoyanchev