Skip to content

Commit a4ff87c

Browse files
authored
Merge branch '3.1' into bugfix/compatible_2.6_lb
2 parents 6bd6bb9 + 16c61c5 commit a4ff87c

3 files changed

Lines changed: 41 additions & 9 deletions

File tree

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
2121
import org.apache.dubbo.common.logger.LoggerFactory;
22+
import org.apache.dubbo.common.utils.ArrayUtils;
2223
import org.apache.dubbo.common.utils.Assert;
2324
import org.apache.dubbo.common.utils.ClassUtils;
2425
import org.apache.dubbo.common.utils.StringUtils;
@@ -399,8 +400,22 @@ public String registerReferenceBean(String propertyName, Class<?> injectedType,
399400
}
400401

401402
//check bean definition
402-
if (beanDefinitionRegistry.containsBeanDefinition(referenceBeanName)) {
403-
BeanDefinition prevBeanDefinition = beanDefinitionRegistry.getBeanDefinition(referenceBeanName);
403+
boolean isContains;
404+
if ((isContains = beanDefinitionRegistry.containsBeanDefinition(referenceBeanName)) || beanDefinitionRegistry.isAlias(referenceBeanName)) {
405+
String preReferenceBeanName = referenceBeanName;
406+
if (!isContains){
407+
// Look in the alias for the origin bean name
408+
String[] aliases = beanDefinitionRegistry.getAliases(referenceBeanName);
409+
if (ArrayUtils.isNotEmpty(aliases)) {
410+
for (String alias : aliases) {
411+
if (beanDefinitionRegistry.containsBeanDefinition(alias)) {
412+
preReferenceBeanName = alias;
413+
break;
414+
}
415+
}
416+
}
417+
}
418+
BeanDefinition prevBeanDefinition = beanDefinitionRegistry.getBeanDefinition(preReferenceBeanName);
404419
String prevBeanType = prevBeanDefinition.getBeanClassName();
405420
String prevBeanDesc = referenceBeanName + "[" + prevBeanType + "]";
406421
String newBeanDesc = referenceBeanName + "[" + referenceKey + "]";

dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,30 @@ public Object aroundHelloService(ProceedingJoinPoint pjp) throws Throwable {
128128
@DubboReference(version = "2", url = "dubbo://127.0.0.1:12345?version=2", tag = "demo_tag")
129129
private HelloService helloService2;
130130

131-
// #7 ReferenceBean (Method Injection #3)
131+
// #7 ReferenceBean (Field Injection #5)
132+
// The HelloService is the same as above service(#6 ReferenceBean (Field Injection #4)), helloService3 will be registered as an alias of helloService2
133+
@DubboReference(version = "2", url = "dubbo://127.0.0.1:12345?version=2", tag = "demo_tag")
134+
private HelloService helloService3;
135+
136+
// #8 ReferenceBean (Method Injection #3)
132137
@DubboReference(version = "3", url = "dubbo://127.0.0.1:12345?version=2", tag = "demo_tag")
133138
public void setHelloService2(HelloService helloService2) {
134139
// The helloService2 beanName is the same as above(#6 ReferenceBean (Field Injection #4)), and this will rename to helloService2#2
135140
renamedHelloService2 = helloService2;
136141
}
137142

143+
// #9 ReferenceBean (Method Injection #4)
144+
@DubboReference(version = "4", url = "dubbo://127.0.0.1:12345?version=2")
145+
public void setHelloService3(DemoService helloService3){
146+
// The helloService3 beanName is the same as above(#7 ReferenceBean (Field Injection #5) is an alias),
147+
// The current beanName(helloService3) is not registered in the beanDefinitionMap, but it is already an alias. so this will rename to helloService3#2
148+
this.renamedHelloService3 = helloService3;
149+
}
150+
138151
private HelloService renamedHelloService2;
139152

153+
private DemoService renamedHelloService3;
154+
140155
@Test
141156
void testAop() throws Exception {
142157

@@ -150,7 +165,7 @@ void testAop() throws Exception {
150165
Assertions.assertNotNull(testBean.getDemoServiceFromParent());
151166
Assertions.assertNotNull(testBean.getDemoService());
152167
Assertions.assertNotNull(testBean.myDemoService);
153-
Assertions.assertEquals(2, demoServicesMap.size());
168+
Assertions.assertEquals(3, demoServicesMap.size());
154169

155170
Assertions.assertNotNull(context.getBean("demoServiceImpl"));
156171
Assertions.assertNotNull(context.getBean("myDemoService"));
@@ -189,12 +204,13 @@ void testGetInjectedFieldReferenceBeanMap() {
189204
Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
190205
beanPostProcessor.getInjectedFieldReferenceBeanMap();
191206

192-
Assertions.assertEquals(4, referenceBeanMap.size());
207+
Assertions.assertEquals(5, referenceBeanMap.size());
193208

194209
Map<String, Integer> checkingFieldNames = new HashMap<>();
195210
checkingFieldNames.put("private org.apache.dubbo.config.spring.api.HelloService org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessorTest$MyConfiguration.helloService", 0);
196211
checkingFieldNames.put("private org.apache.dubbo.config.spring.api.HelloService org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessorTest.helloService", 0);
197212
checkingFieldNames.put("private org.apache.dubbo.config.spring.api.HelloService org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessorTest.helloService2", 0);
213+
checkingFieldNames.put("private org.apache.dubbo.config.spring.api.HelloService org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessorTest.helloService3", 0);
198214
checkingFieldNames.put("private org.apache.dubbo.config.spring.api.DemoService org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessorTest$ParentBean.demoServiceFromParent", 0);
199215

200216
for (Map.Entry<InjectionMetadata.InjectedElement, ReferenceBean<?>> entry : referenceBeanMap.entrySet()) {
@@ -222,12 +238,13 @@ void testGetInjectedMethodReferenceBeanMap() {
222238
Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
223239
beanPostProcessor.getInjectedMethodReferenceBeanMap();
224240

225-
Assertions.assertEquals(3, referenceBeanMap.size());
241+
Assertions.assertEquals(4, referenceBeanMap.size());
226242

227243
Map<String, Integer> checkingMethodNames = new HashMap<>();
228244
checkingMethodNames.put("setDemoServiceFromAncestor", 0);
229245
checkingMethodNames.put("setDemoService", 0);
230246
checkingMethodNames.put("setHelloService2", 0);
247+
checkingMethodNames.put("setHelloService3", 0);
231248

232249
for (Map.Entry<InjectionMetadata.InjectedElement, ReferenceBean<?>> entry : referenceBeanMap.entrySet()) {
233250

@@ -252,7 +269,7 @@ void testReferenceBeansMethodAnnotation() {
252269

253270
Collection<ReferenceBean> referenceBeans = referenceBeanManager.getReferences();
254271

255-
Assertions.assertEquals(4, referenceBeans.size());
272+
Assertions.assertEquals(5, referenceBeans.size());
256273

257274
for (ReferenceBean referenceBean : referenceBeans) {
258275
ReferenceConfig referenceConfig = referenceBean.getReferenceConfig();
@@ -350,4 +367,4 @@ public TestBean testBean() {
350367
}
351368

352369
}
353-
}
370+
}

dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static ProxyHolder referProxy(ServiceInstance instance) {
137137
ModuleModel internalModel = applicationModel.getInternalModule();
138138
ConsumerModel consumerModel = applicationModel.getInternalModule().registerInternalConsumer(MetadataService.class, url);
139139

140-
Protocol protocol = applicationModel.getExtensionLoader(Protocol.class).getAdaptiveExtension();
140+
Protocol protocol = applicationModel.getExtensionLoader(Protocol.class).getExtension(url.getProtocol(), false);
141141

142142
url = url.setServiceModel(consumerModel);
143143

0 commit comments

Comments
 (0)