This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 6c6fe16  Add Unit Test for AOP support(#7790)
6c6fe16 is described below

commit 6c6fe167a23af4286792ba516a3fb683487f286f
Author: ph3636 <[email protected]>
AuthorDate: Wed May 19 22:10:01 2021 +0800

    Add Unit Test for AOP support(#7790)
    
    Co-authored-by: ph3636 <>
---
 .../AnnotatedInterfaceConfigBeanBuilder.java       |  7 ++
 .../ReferenceAnnotationBeanPostProcessor.java      | 10 +--
 .../factory/annotation/ReferenceBeanBuilder.java   |  2 +
 ...eferencePostProcessAfterInitializationTest.java | 96 ++++++++++++++++++++++
 4 files changed, 109 insertions(+), 6 deletions(-)

diff --git 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java
 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java
index 15055f7..9869bbb 100644
--- 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java
+++ 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java
@@ -56,6 +56,8 @@ public abstract class AnnotatedInterfaceConfigBeanBuilder<C 
extends AbstractInte
 
     protected Class<?> interfaceClass;
 
+    protected String beanName;
+
     protected AnnotatedInterfaceConfigBeanBuilder(AnnotationAttributes 
attributes, ApplicationContext applicationContext) {
         Assert.notNull(attributes, "The Annotation attributes must not be 
null!");
         Assert.notNull(applicationContext, "The ApplicationContext must not be 
null!");
@@ -211,4 +213,9 @@ public abstract class AnnotatedInterfaceConfigBeanBuilder<C 
extends AbstractInte
         this.interfaceClass = interfaceClass;
         return (T) this;
     }
+
+    public <T extends AnnotatedInterfaceConfigBeanBuilder<C>> T 
beanName(String beanName) {
+        this.beanName = beanName;
+        return (T) this;
+    }
 }
diff --git 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
index aa683f4..0457118 100644
--- 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
+++ 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
@@ -156,7 +156,7 @@ public class ReferenceAnnotationBeanPostProcessor extends 
AbstractAnnotationBean
 
         prepareReferenceBean(referencedBeanName, referenceBean, 
localServiceBean);
 
-        registerReferenceBean(referencedBeanName, referenceBean, attributes, 
localServiceBean, injectedType);
+        registerReferenceBean(referencedBeanName, referenceBean, 
localServiceBean, referenceBeanName);
 
         cacheInjectedReferenceBean(referenceBean, injectedElement);
 
@@ -174,13 +174,10 @@ public class ReferenceAnnotationBeanPostProcessor extends 
AbstractAnnotationBean
      * @since 2.7.3
      */
     private void registerReferenceBean(String referencedBeanName, 
ReferenceBean referenceBean,
-                                       AnnotationAttributes attributes,
-                                       boolean localServiceBean, Class<?> 
interfaceClass) {
+                                       boolean localServiceBean, String 
beanName) {
 
         ConfigurableListableBeanFactory beanFactory = getBeanFactory();
 
-        String beanName = getReferenceBeanName(attributes, interfaceClass);
-
         if (localServiceBean) {  // If @Service bean is local one
             /**
              * Get  the @Service's BeanDefinition from {@link BeanFactory}
@@ -371,7 +368,8 @@ public class ReferenceAnnotationBeanPostProcessor extends 
AbstractAnnotationBean
         if (referenceBean == null) {
             ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder
                     .create(attributes, applicationContext)
-                    .interfaceClass(referencedType);
+                    .interfaceClass(referencedType)
+                    .beanName(referenceBeanName);
             referenceBean = beanBuilder.build();
             referenceBeanCache.put(referenceBeanName, referenceBean);
         } else if 
(!referencedType.isAssignableFrom(referenceBean.getInterfaceClass())) {
diff --git 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java
 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java
index 0bf6521..7d61ec9 100644
--- 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java
+++ 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java
@@ -179,6 +179,8 @@ class ReferenceBeanBuilder extends 
AnnotatedInterfaceConfigBeanBuilder<Reference
 
         bean.afterPropertiesSet();
 
+        
applicationContext.getAutowireCapableBeanFactory().applyBeanPostProcessorsAfterInitialization(bean,
 beanName);
+
     }
 
     @Deprecated
diff --git 
a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboReferencePostProcessAfterInitializationTest.java
 
b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboReferencePostProcessAfterInitializationTest.java
new file mode 100644
index 0000000..a32669f
--- /dev/null
+++ 
b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboReferencePostProcessAfterInitializationTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.config.spring.beans.factory.annotation;
+
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.apache.dubbo.config.spring.ReferenceBean;
+import org.apache.dubbo.config.spring.api.HelloService;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import static 
org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.BEAN_NAME;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * {@link DubboReference @DubboReference} of Generic injection test
+ *
+ * @see DubboReference
+ * @since 2.7.9
+ */
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration(
+        classes = {
+                ServiceAnnotationTestConfiguration.class,
+                DubboReferencePostProcessAfterInitializationTest.class
+        })
+@TestPropertySource(properties = {
+        "packagesToScan = 
org.apache.dubbo.config.spring.context.annotation.provider",
+        "consumer.version = ${demo.service.version}",
+        "consumer.url = dubbo://127.0.0.1:12345?version=2.5.7",
+})
+@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
+public class DubboReferencePostProcessAfterInitializationTest {
+
+    private static final Map<String, ReferenceBean<?>> REFERENCE_BEAN_MAP = 
new HashMap<>(1, 1);
+
+    @Bean(BEAN_NAME)
+    public ReferenceAnnotationBeanPostProcessor 
referenceAnnotationBeanPostProcessor() {
+        return new ReferenceAnnotationBeanPostProcessor();
+    }
+
+    @DubboReference
+    private HelloService helloService;
+
+    @Test
+    public void test() {
+        assertEquals(1, REFERENCE_BEAN_MAP.size());
+        assertEquals("Greeting, Mercy.", helloService.sayHello("Mercy"));
+    }
+
+    @Bean("afterBeanPostProcessor")
+    public AfterBeanPostProcessor afterBeanPostProcessor() {
+        return new AfterBeanPostProcessor();
+    }
+
+    public static class AfterBeanPostProcessor implements BeanPostProcessor {
+
+        public Object postProcessAfterInitialization(Object bean, String 
beanName) throws BeansException {
+            if (bean.getClass() == ReferenceBean.class && ((ReferenceBean<?>) 
bean).getInterfaceClass() == HelloService.class) {
+                REFERENCE_BEAN_MAP.put(beanName, (ReferenceBean<?>) bean);
+            } else if (REFERENCE_BEAN_MAP.containsKey(beanName)) {
+                return Proxy.newProxyInstance(getClass().getClassLoader(), new 
Class<?>[]{HelloService.class}, (proxy, method, args) -> {
+                    if ("sayHello".equals(method.getName())) {
+                        return method.invoke(bean, args) + ".";
+                    }
+                    return method.invoke(bean, args);
+                });
+            }
+            return bean;
+        }
+    }
+}

Reply via email to