You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/06/28 02:19:51 UTC

[GitHub] [dubbo] ph3636 commented on a change in pull request #7790: feature: As a FactoryBean type, ReferenceBean supports calling applyBeanPostProcessorsAfterInitialization. Add Unit Test

ph3636 commented on a change in pull request #7790:
URL: https://github.com/apache/dubbo/pull/7790#discussion_r659421600



##########
File path: 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) -> {

Review comment:
       Test scenarios such as the reference bean generated by dubbroeference support calling applybean postprocessor after initialization, and the instance generated by this reference bean supports calling applybean postprocessor after initialization




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org