Skip to content

Commit

Permalink
Polish apache#4498 : @reference can't inject the Dubbo service proxy …
Browse files Browse the repository at this point in the history
…when its attribute is empty
  • Loading branch information
mercyblitz committed Jul 9, 2019
1 parent 5285ab2 commit 99b9cd6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import static org.apache.dubbo.config.spring.util.AnnotationUtils.getMergedAttributes;
import static org.springframework.core.BridgeMethodResolver.findBridgedMethod;
import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
* Abstract generic {@link BeanPostProcessor} implementation for customized annotation that annotated injected-object.
Expand Down Expand Up @@ -172,7 +171,7 @@ private List<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> findFiel

AnnotationAttributes attributes = getMergedAttributes(field, annotationType, getEnvironment(), true);

if (!isEmpty(attributes)) {
if (attributes != null) {

if (Modifier.isStatic(field.getModifiers())) {
if (logger.isWarnEnabled()) {
Expand Down Expand Up @@ -213,7 +212,7 @@ private List<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> findAnn

AnnotationAttributes attributes = getMergedAttributes(bridgedMethod, annotationType, getEnvironment(), true);

if (!isEmpty(attributes) && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) {
if (attributes != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) {
if (Modifier.isStatic(method.getModifiers())) {
if (logger.isWarnEnabled()) {
logger.warn("@" + annotationType.getName() + " annotation is not supported on static methods: " + method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public static Map<String, Object> resolvePlaceholders(Map<String, Object> source
* @param propertyResolver {@link PropertyResolver} instance, e.g {@link Environment}
* @param ignoreDefaultValue whether ignore default value or not
* @param ignoreAttributeNames the attribute names of annotation should be ignored
* @return non-null
* @return If the specified annotation type is not found, return <code>null</code>
* @since 2.7.3
*/
public static AnnotationAttributes getMergedAttributes(AnnotatedElement annotatedElement,
Expand All @@ -424,7 +424,7 @@ public static AnnotationAttributes getMergedAttributes(AnnotatedElement annotate
boolean ignoreDefaultValue,
String... ignoreAttributeNames) {
Annotation annotation = getMergedAnnotation(annotatedElement, annotationType);
return fromMap(getAttributes(annotation, propertyResolver, ignoreDefaultValue, ignoreAttributeNames));
return annotation == null ? null : fromMap(getAttributes(annotation, propertyResolver, ignoreDefaultValue, ignoreAttributeNames));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.ReferenceBean;
import org.apache.dubbo.config.spring.api.DemoService;
import org.apache.dubbo.config.spring.api.HelloService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
Expand All @@ -36,6 +38,7 @@
import java.util.Map;

import static org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.BEAN_NAME;
import static org.junit.Assert.assertTrue;

/**
* {@link ReferenceAnnotationBeanPostProcessor} Test
Expand Down Expand Up @@ -68,14 +71,31 @@ public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor
@Autowired
private ConfigurableApplicationContext context;

@Autowired
@Qualifier("defaultHelloService")
private HelloService defaultHelloService;

@Autowired
@Qualifier("helloServiceImpl")
private HelloService helloServiceImpl;

@Reference(id = "helloService")
private HelloService helloService;

@Test
public void test() throws Exception {

assertTrue(context.containsBean("helloService"));

TestBean testBean = context.getBean(TestBean.class);

DemoService demoService = testBean.getDemoService();

Assert.assertEquals("Hello,Mercy", demoService.sayName("Mercy"));
Assert.assertEquals("Greeting, Mercy", defaultHelloService.sayHello("Mercy"));
Assert.assertEquals("Hello, Mercy", helloServiceImpl.sayHello("Mercy"));

Assert.assertEquals("Greeting, Mercy", helloService.sayHello("Mercy"));

Assert.assertNotNull(testBean.getDemoServiceFromAncestor());
Assert.assertNotNull(testBean.getDemoServiceFromParent());
Expand Down Expand Up @@ -115,7 +135,7 @@ public void testGetReferenceBeans() {

Collection<ReferenceBean<?>> referenceBeans = beanPostProcessor.getReferenceBeans();

Assert.assertEquals(1, referenceBeans.size());
Assert.assertEquals(2, referenceBeans.size());

ReferenceBean<?> referenceBean = referenceBeans.iterator().next();

Expand All @@ -135,7 +155,7 @@ public void testGetInjectedFieldReferenceBeanMap() {
Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
beanPostProcessor.getInjectedFieldReferenceBeanMap();

Assert.assertEquals(1, referenceBeanMap.size());
Assert.assertEquals(2, referenceBeanMap.size());

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

Expand All @@ -144,11 +164,6 @@ public void testGetInjectedFieldReferenceBeanMap() {
Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement",
injectedElement.getClass().getName());

ReferenceBean<?> referenceBean = entry.getValue();

Assert.assertEquals("2.5.7", referenceBean.getVersion());
Assert.assertEquals("dubbo://127.0.0.1:12345", referenceBean.getUrl());

}

}
Expand All @@ -172,11 +187,6 @@ public void testGetInjectedMethodReferenceBeanMap() {
Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedMethodElement",
injectedElement.getClass().getName());

ReferenceBean<?> referenceBean = entry.getValue();

Assert.assertEquals("2.5.7", referenceBean.getVersion());
Assert.assertEquals("dubbo://127.0.0.1:12345", referenceBean.getUrl());

}

}
Expand Down

0 comments on commit 99b9cd6

Please sign in to comment.