[ISSUE #12637] Add reference bean naming strategy to avoid @Resource by-name conflicts - #16438
Open
zengbohan1 wants to merge 5 commits into
Open
[ISSUE #12637] Add reference bean naming strategy to avoid @Resource by-name conflicts#16438zengbohan1 wants to merge 5 commits into
zengbohan1 wants to merge 5 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16438 +/- ##
============================================
- Coverage 60.94% 60.90% -0.04%
- Complexity 30 11767 +11737
============================================
Files 1953 1953
Lines 89271 89283 +12
Branches 13473 13476 +3
============================================
- Hits 54405 54377 -28
- Misses 29297 29321 +24
- Partials 5569 5585 +16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
@DubboReferenceis used on a field,ReferenceAnnotationBeanPostProcessorregisters aReferenceBeanwhose bean name defaults to the field name (when no explicitidattribute is set). If another bean in the same application context declares a field with the same name but a different type and injects it by name (JSR-250@Resource), the injection fails.Reproduction
Startup fails with:
Root cause
This is a cross-annotation conflict with a timing gap:
@DubboReferencevs@DubboReferencecollisions on the same bean name are already handled by a rename fallback (xxx#2) insideregisterReferenceBean.@Resourcenever registers a bean definition; it resolves by name at injection time, which happens after@DubboReferencehas already registered itsReferenceBeanunder the field name. So no rename/protection logic can kick in at that point.Proposal
Add an opt-in naming strategy for reference beans that do not declare an explicit
id, selected via a Spring environment property:field-name(default, current behavior): bean name = annotated field/setter property name.interface-name(new): bean name = simple name of the referenced service interface, so it no longer depends on the declaring field name.The default keeps full backward compatibility (non-breaking). An explicit
idattribute always takes priority regardless of the strategy.Verification
Reproduced and verified with a Spring Boot 2.7.13 + Dubbo 3.3.6 + JDK 17 consumer application (ZooKeeper registry) containing exactly the conflicting controllers above, with the patched class applied.
Scenario 1 - default strategy (
field-name, property not set): behavior unchanged, still failsScenario 2 -
-Ddubbo.application.reference-bean-naming-strategy=interface-name: application startsThe
Demo2Controllerreference bean is now registered asDemo2Service;Demo1Controller's@Resourcelookup by name finds nothing nameddemo1Service, falls back to by-type matching and successfully injects the localDemo1ServiceImplprovider bean.A new test
ReferenceBeanNamingStrategyTestcovers both strategies (default strategy derives names from the field name including the existing rename-to-#2fallback;interface-namestrategy derives them from the referenced interface simple names).Note
If desired, maintainers could consider flipping the default value to
interface-namein a future major/minor release (e.g. 3.4) after collecting feedback, since it removes a whole class of by-name conflicts.