You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by me...@apache.org on 2019/06/25 03:29:55 UTC

[dubbo] branch 2.6.x updated: Polish /apache/dubbo#3695 : @Reference field can't refer its' @Service Bean in same JVM from v2.6.6

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

mercyblitz pushed a commit to branch 2.6.x
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/2.6.x by this push:
     new f82b092  Polish /apache/dubbo#3695 : @Reference field can't refer its' @Service Bean in same JVM from v2.6.6
f82b092 is described below

commit f82b09289dc5aeae946b3ae88be1d7e91eec1a54
Author: mercyblitz <me...@gmail.com>
AuthorDate: Tue Jun 25 11:29:27 2019 +0800

    Polish /apache/dubbo#3695 : @Reference field can't refer its' @Service Bean in same JVM from v2.6.6
---
 .../annotation/AnnotationBeanNameBuilder.java      | 142 ---------------------
 .../ReferenceAnnotationBeanPostProcessor.java      |   7 +-
 .../ServiceAnnotationBeanPostProcessor.java        |   7 +-
 .../factory/annotation/ServiceBeanNameBuilder.java |   3 -
 .../annotation/AnnotationBeanNameBuilderTest.java  |  83 ------------
 5 files changed, 4 insertions(+), 238 deletions(-)

diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilder.java
deleted file mode 100644
index cf72a4a..0000000
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilder.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * 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 com.alibaba.dubbo.config.spring.beans.factory.annotation;
-
-import com.alibaba.dubbo.common.Constants;
-import com.alibaba.dubbo.config.annotation.Reference;
-import com.alibaba.dubbo.config.annotation.Service;
-import com.alibaba.dubbo.registry.Registry;
-
-import org.springframework.core.env.Environment;
-
-import static com.alibaba.dubbo.common.Constants.CONSUMERS_CATEGORY;
-import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL;
-import static com.alibaba.dubbo.common.Constants.PROVIDERS_CATEGORY;
-import static com.alibaba.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName;
-import static org.springframework.util.StringUtils.arrayToCommaDelimitedString;
-import static org.springframework.util.StringUtils.hasText;
-
-/**
- * The Bean Name Builder for the annotations {@link Service} and {@link Reference}
- * <p>
- * The naming rule is consistent with the the implementation {@link Registry} that is based on the service-name aware
- * infrastructure, e.g Spring Cloud, Cloud Native and so on.
- * <p>
- * The pattern of bean name : ${category}:${protocol}:${serviceInterface}:${version}:${group}.
- * <p>
- * ${version} and ${group} are optional.
- *
- * @since 2.6.6
- */
-class AnnotationBeanNameBuilder {
-
-    private static final String SEPARATOR = ":";
-
-    // Required properties
-
-    private final String category;
-
-    private final String protocol;
-
-    private final String interfaceClassName;
-
-    // Optional properties
-
-    private String version;
-
-    private String group;
-
-    private Environment environment;
-
-    private AnnotationBeanNameBuilder(String category, String protocol, String interfaceClassName) {
-        this.category = category;
-        this.protocol = protocol;
-        this.interfaceClassName = interfaceClassName;
-    }
-
-    private AnnotationBeanNameBuilder(Service service, Class<?> interfaceClass) {
-        this(PROVIDERS_CATEGORY, resolveProtocol(service.protocol()), resolveInterfaceName(service, interfaceClass));
-        this.group(service.group());
-        this.version(service.version());
-    }
-
-    private AnnotationBeanNameBuilder(Reference reference, Class<?> interfaceClass) {
-        this(CONSUMERS_CATEGORY, resolveProtocol(reference.protocol()), resolveInterfaceName(reference, interfaceClass));
-        this.group(reference.group());
-        this.version(reference.version());
-    }
-
-    public static AnnotationBeanNameBuilder create(Service service, Class<?> interfaceClass) {
-        return new AnnotationBeanNameBuilder(service, interfaceClass);
-    }
-
-    public static AnnotationBeanNameBuilder create(Reference reference, Class<?> interfaceClass) {
-        return new AnnotationBeanNameBuilder(reference, interfaceClass);
-    }
-
-    private static void append(StringBuilder builder, String value) {
-        if (hasText(value)) {
-            builder.append(SEPARATOR).append(value);
-        }
-    }
-
-    public AnnotationBeanNameBuilder group(String group) {
-        this.group = group;
-        return this;
-    }
-
-    public AnnotationBeanNameBuilder version(String version) {
-        this.version = version;
-        return this;
-    }
-
-    public AnnotationBeanNameBuilder environment(Environment environment) {
-        this.environment = environment;
-        return this;
-    }
-
-    /**
-     * Resolve the protocol
-     *
-     * @param protocols one or more protocols
-     * @return if <code>protocols</code> == <code>null</code>, it will return
-     * {@link Constants#DEFAULT_PROTOCOL "dubbo"} as the default protocol
-     * @see Constants#DEFAULT_PROTOCOL
-     */
-    private static String resolveProtocol(String... protocols) {
-        String protocol = arrayToCommaDelimitedString(protocols);
-        return hasText(protocol) ? protocol : DEFAULT_PROTOCOL;
-    }
-
-    /**
-     * Build bean name while resolve the placeholders if possible.
-     *
-     * @return pattern : ${category}:${protocol}:${serviceInterface}:${version}:${group}
-     */
-    public String build() {
-        // Append the required properties
-        StringBuilder beanNameBuilder = new StringBuilder(category);
-        append(beanNameBuilder, protocol);
-        append(beanNameBuilder, interfaceClassName);
-        // Append the optional properties
-        append(beanNameBuilder, version);
-        append(beanNameBuilder, group);
-        String beanName = beanNameBuilder.toString();
-        // Resolve placeholders
-        return environment != null ? environment.resolvePlaceholders(beanName) : beanName;
-    }
-}
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
index c7908cd7..2033e63 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
@@ -21,7 +21,6 @@ import com.alibaba.dubbo.config.spring.ReferenceBean;
 import com.alibaba.dubbo.config.spring.ServiceBean;
 import com.alibaba.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
 import com.alibaba.dubbo.config.spring.util.AnnotationUtils;
-
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.annotation.InjectionMetadata;
 import org.springframework.context.ApplicationContext;
@@ -105,7 +104,7 @@ public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBean
     }
 
     @Override
-                            protected Object doGetInjectedBean(Reference reference, Object bean, String beanName, Class<?> injectedType,
+    protected Object doGetInjectedBean(Reference reference, Object bean, String beanName, Class<?> injectedType,
                                        InjectionMetadata.InjectedElement injectedElement) throws Exception {
 
         String referencedBeanName = buildReferencedBeanName(reference, injectedType);
@@ -188,9 +187,7 @@ public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBean
 
     private String buildReferencedBeanName(Reference reference, Class<?> injectedType) {
 
-        AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, injectedType);
-
-        builder.environment(getEnvironment());
+        ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, injectedType, getEnvironment());
 
         return getEnvironment().resolvePlaceholders(builder.build());
     }
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java
index 457f355..5e96116 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java
@@ -23,7 +23,6 @@ import com.alibaba.dubbo.config.annotation.Method;
 import com.alibaba.dubbo.config.annotation.Service;
 import com.alibaba.dubbo.config.spring.ServiceBean;
 import com.alibaba.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner;
-
 import org.springframework.beans.BeansException;
 import org.springframework.beans.MutablePropertyValues;
 import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -291,9 +290,7 @@ public class ServiceAnnotationBeanPostProcessor implements BeanDefinitionRegistr
      */
     private String generateServiceBeanName(Service service, Class<?> interfaceClass, String annotatedServiceBeanName) {
 
-        AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(service, interfaceClass);
-
-        builder.environment(environment);
+        ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(service, interfaceClass, environment);
 
         return builder.build();
     }
@@ -439,7 +436,7 @@ public class ServiceAnnotationBeanPostProcessor implements BeanDefinitionRegistr
 
         Method[] methods = service.methods();
         List<MethodConfig> methodConfigs = MethodConfig.constructMethodConfig(methods);
-        if(!methodConfigs.isEmpty()){
+        if (!methodConfigs.isEmpty()) {
             builder.addPropertyValue("methods", methodConfigs);
         }
 
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java
index 4a97a24..8121c04 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java
@@ -20,7 +20,6 @@ import com.alibaba.dubbo.config.annotation.Reference;
 import com.alibaba.dubbo.config.annotation.Service;
 import com.alibaba.dubbo.config.spring.ReferenceBean;
 import com.alibaba.dubbo.config.spring.ServiceBean;
-
 import org.springframework.core.env.Environment;
 import org.springframework.util.StringUtils;
 
@@ -35,9 +34,7 @@ import static com.alibaba.dubbo.config.spring.util.AnnotationUtils.resolveInterf
  * @see ServiceBean
  * @see ReferenceBean
  * @since 2.6.5
- * @deprecated {@link AnnotationBeanNameBuilder} as the replacement
  */
-@Deprecated
 class ServiceBeanNameBuilder {
 
     private static final String SEPARATOR = ":";
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java
deleted file mode 100644
index 28c53cc..0000000
--- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * 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 com.alibaba.dubbo.config.spring.beans.factory.annotation;
-
-import com.alibaba.dubbo.config.annotation.Reference;
-import com.alibaba.dubbo.config.annotation.Service;
-import com.alibaba.dubbo.config.spring.api.DemoService;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.springframework.core.annotation.AnnotationUtils;
-import org.springframework.mock.env.MockEnvironment;
-import org.springframework.util.ReflectionUtils;
-
-import static com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationBeanNameBuilderTest.GROUP;
-import static com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationBeanNameBuilderTest.VERSION;
-
-/**
- * {@link AnnotationBeanNameBuilder} Test
- *
- * @see AnnotationBeanNameBuilder
- * @since 2.6.6
- */
-@Service(interfaceClass = DemoService.class, group = GROUP, version = VERSION,
-        application = "application", module = "module", registry = {"1", "2", "3"})
-public class AnnotationBeanNameBuilderTest {
-
-    @Reference(interfaceClass = DemoService.class, group = "DUBBO", version = "${dubbo.version}",
-            application = "application", module = "module", registry = {"1", "2", "3"})
-    static final Class<?> INTERFACE_CLASS = DemoService.class;
-
-    static final String GROUP = "DUBBO";
-
-    static final String VERSION = "1.0.0";
-
-    private MockEnvironment environment;
-
-    @Before
-    public void prepare() {
-        environment = new MockEnvironment();
-        environment.setProperty("dubbo.version", "1.0.0");
-    }
-
-    @Test
-    public void testServiceAnnotation() {
-        Service service = AnnotationUtils.getAnnotation(AnnotationBeanNameBuilderTest.class, Service.class);
-        AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(service, INTERFACE_CLASS);
-        Assert.assertEquals("providers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:1.0.0:DUBBO",
-                builder.build());
-
-        builder.environment(environment);
-        Assert.assertEquals("providers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:1.0.0:DUBBO",
-                builder.build());
-    }
-
-    @Test
-    public void testReferenceAnnotation() {
-        Reference reference = AnnotationUtils.getAnnotation(ReflectionUtils.findField(AnnotationBeanNameBuilderTest.class, "INTERFACE_CLASS"), Reference.class);
-        AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, INTERFACE_CLASS);
-        Assert.assertEquals("consumers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:${dubbo.version}:DUBBO",
-                builder.build());
-
-        builder.environment(environment);
-        Assert.assertEquals("consumers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:1.0.0:DUBBO",
-                builder.build());
-    }
-
-}