From 81e61cc5c3d83b716ca2e712ccdc444c0fff208a Mon Sep 17 00:00:00 2001 From: jimin Date: Thu, 20 Jun 2019 07:01:24 +0800 Subject: [PATCH 01/11] remove unnecessary null check before instance of (#4321) Signed-off-by: jimin.jm --- .../dubbo/common/beanutil/JavaBeanSerializeUtil.java | 4 ++-- .../java/org/apache/dubbo/common/json/JSONArray.java | 10 +++++----- .../java/org/apache/dubbo/common/json/JSONObject.java | 10 +++++----- .../dubbo/registry/support/ProviderInvokerWrapper.java | 2 +- .../dubbo/remoting/transport/netty/NettyHelper.java | 2 +- .../protocol/dubbo/decode/DubboTelnetDecodeTest.java | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java b/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java index 995965ac74a..1ce5c455cf5 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java @@ -237,12 +237,12 @@ private static void deserializeInternal(Object result, JavaBeanDescriptor beanDe for (Map.Entry entry : beanDescriptor) { Object key = entry.getKey(); Object value = entry.getValue(); - if (key != null && key instanceof JavaBeanDescriptor) { + if (key instanceof JavaBeanDescriptor) { JavaBeanDescriptor keyDescriptor = (JavaBeanDescriptor) entry.getKey(); key = instantiateForDeserialize(keyDescriptor, loader, cache); deserializeInternal(key, keyDescriptor, loader, cache); } - if (value != null && value instanceof JavaBeanDescriptor) { + if (value instanceof JavaBeanDescriptor) { JavaBeanDescriptor valueDescriptor = (JavaBeanDescriptor) entry.getValue(); value = instantiateForDeserialize(valueDescriptor, loader, cache); deserializeInternal(value, valueDescriptor, loader, cache); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONArray.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONArray.java index f5e039c3be8..a75bedbdd40 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONArray.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONArray.java @@ -47,7 +47,7 @@ public Object get(int index) { */ public boolean getBoolean(int index, boolean def) { Object tmp = mArray.get(index); - return tmp != null && tmp instanceof Boolean ? ((Boolean) tmp).booleanValue() : def; + return tmp instanceof Boolean ? ((Boolean) tmp).booleanValue() : def; } /** @@ -59,7 +59,7 @@ public boolean getBoolean(int index, boolean def) { */ public int getInt(int index, int def) { Object tmp = mArray.get(index); - return tmp != null && tmp instanceof Number ? ((Number) tmp).intValue() : def; + return tmp instanceof Number ? ((Number) tmp).intValue() : def; } /** @@ -71,7 +71,7 @@ public int getInt(int index, int def) { */ public long getLong(int index, long def) { Object tmp = mArray.get(index); - return tmp != null && tmp instanceof Number ? ((Number) tmp).longValue() : def; + return tmp instanceof Number ? ((Number) tmp).longValue() : def; } /** @@ -83,7 +83,7 @@ public long getLong(int index, long def) { */ public float getFloat(int index, float def) { Object tmp = mArray.get(index); - return tmp != null && tmp instanceof Number ? ((Number) tmp).floatValue() : def; + return tmp instanceof Number ? ((Number) tmp).floatValue() : def; } /** @@ -95,7 +95,7 @@ public float getFloat(int index, float def) { */ public double getDouble(int index, double def) { Object tmp = mArray.get(index); - return tmp != null && tmp instanceof Number ? ((Number) tmp).doubleValue() : def; + return tmp instanceof Number ? ((Number) tmp).doubleValue() : def; } /** diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONObject.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONObject.java index 052f35269dd..c04d4e14050 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONObject.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONObject.java @@ -47,7 +47,7 @@ public Object get(String key) { */ public boolean getBoolean(String key, boolean def) { Object tmp = mMap.get(key); - return tmp != null && tmp instanceof Boolean ? (Boolean) tmp : def; + return tmp instanceof Boolean ? (Boolean) tmp : def; } /** @@ -59,7 +59,7 @@ public boolean getBoolean(String key, boolean def) { */ public int getInt(String key, int def) { Object tmp = mMap.get(key); - return tmp != null && tmp instanceof Number ? ((Number) tmp).intValue() : def; + return tmp instanceof Number ? ((Number) tmp).intValue() : def; } /** @@ -71,7 +71,7 @@ public int getInt(String key, int def) { */ public long getLong(String key, long def) { Object tmp = mMap.get(key); - return tmp != null && tmp instanceof Number ? ((Number) tmp).longValue() : def; + return tmp instanceof Number ? ((Number) tmp).longValue() : def; } /** @@ -83,7 +83,7 @@ public long getLong(String key, long def) { */ public float getFloat(String key, float def) { Object tmp = mMap.get(key); - return tmp != null && tmp instanceof Number ? ((Number) tmp).floatValue() : def; + return tmp instanceof Number ? ((Number) tmp).floatValue() : def; } /** @@ -95,7 +95,7 @@ public float getFloat(String key, float def) { */ public double getDouble(String key, double def) { Object tmp = mMap.get(key); - return tmp != null && tmp instanceof Number ? ((Number) tmp).doubleValue() : def; + return tmp instanceof Number ? ((Number) tmp).doubleValue() : def; } /** diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderInvokerWrapper.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderInvokerWrapper.java index ce079b743e4..a1eaad3410d 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderInvokerWrapper.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderInvokerWrapper.java @@ -90,7 +90,7 @@ public void setReg(boolean reg) { @Override public boolean equals(Object o) { - if (o == null || !(o instanceof ProviderInvokerWrapper)) { + if (!(o instanceof ProviderInvokerWrapper)) { return false; } ProviderInvokerWrapper other = (ProviderInvokerWrapper) o; diff --git a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyHelper.java b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyHelper.java index ec73094ce95..64f41b2daef 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyHelper.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyHelper.java @@ -27,7 +27,7 @@ final class NettyHelper { public static void setNettyLoggerFactory() { InternalLoggerFactory factory = InternalLoggerFactory.getDefaultFactory(); - if (factory == null || !(factory instanceof DubboLoggerFactory)) { + if (!(factory instanceof DubboLoggerFactory)) { InternalLoggerFactory.setDefaultFactory(new DubboLoggerFactory()); } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java index e54d2e6508a..28c49cce497 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java @@ -452,7 +452,7 @@ private ByteBuf createDubboByteBuf() throws IOException { } private static boolean checkTelnetDecoded(Object msg) { - if (msg != null && msg instanceof String && !msg.toString().contains("Unsupported command:")) { + if (msg instanceof String && !msg.toString().contains("Unsupported command:")) { return true; } return false; From 60c9d9dbbc8db2dccc019df82d7a9194d4ccbdfa Mon Sep 17 00:00:00 2001 From: zhenxianyimeng <1920405993@qq.com> Date: Thu, 20 Jun 2019 07:02:59 +0800 Subject: [PATCH 02/11] add zookeeper maven dependency so that on change registery can run the demo (#4352) --- .../dubbo-demo-annotation-consumer/pom.xml | 8 ++++++++ .../dubbo-demo-annotation-provider/pom.xml | 8 ++++++++ dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml | 8 ++++++++ dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml index c13bc265bc2..c8f9d9d50e1 100644 --- a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml +++ b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml @@ -42,6 +42,14 @@ org.apache.dubbo dubbo-registry-multicast + + org.apache.dubbo + dubbo-registry-zookeeper + + + org.apache.dubbo + dubbo-configcenter-zookeeper + org.apache.dubbo dubbo-rpc-dubbo diff --git a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml index 4f164bce61e..02d0248e431 100644 --- a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml +++ b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml @@ -43,6 +43,14 @@ org.apache.dubbo dubbo-registry-multicast + + org.apache.dubbo + dubbo-registry-zookeeper + + + org.apache.dubbo + dubbo-configcenter-zookeeper + org.apache.dubbo dubbo-rpc-dubbo diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml index 4dfde790cfa..5720551e7db 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml @@ -38,6 +38,14 @@ org.apache.dubbo dubbo-registry-multicast + + org.apache.dubbo + dubbo-registry-zookeeper + + + org.apache.dubbo + dubbo-configcenter-zookeeper + org.apache.dubbo dubbo-config-spring diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml index a93c41ccf00..c683235834a 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml @@ -40,6 +40,14 @@ org.apache.dubbo dubbo-registry-multicast + + org.apache.dubbo + dubbo-registry-zookeeper + + + org.apache.dubbo + dubbo-configcenter-zookeeper + org.apache.dubbo dubbo-rpc-dubbo From 21a0d1c13ca86ae9afb421794349d740ff88d0bd Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Thu, 20 Jun 2019 14:20:57 +0800 Subject: [PATCH 03/11] Polish apache/incubator-dubbo#4347 : Dubbo+nacos throw classNotFound --- dubbo-dependencies-bom/pom.xml | 2 +- .../org/apache/dubbo/registry/nacos/NacosRegistryFactory.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index 20c17b66317..cdbe2bcd5d9 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -128,7 +128,7 @@ 3.0.19.Final 8.5.31 0.3.0 - 1.0.0 + 1.0.1 1.7.25 1.2 diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistryFactory.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistryFactory.java index 2b66fb1e95b..d702acf7e25 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistryFactory.java @@ -17,6 +17,7 @@ package org.apache.dubbo.registry.nacos; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.RegistryFactory; import org.apache.dubbo.registry.support.AbstractRegistryFactory; @@ -24,7 +25,6 @@ import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; -import com.alibaba.nacos.client.naming.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From d58405e30dd562a2141ce60374fe1bd616381912 Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Thu, 20 Jun 2019 16:36:16 +0800 Subject: [PATCH 04/11] Polish apache/incubator-dubbo#4330 : Add @com.alibaba.dubbo.config.annotation.Service support --- dubbo-config/dubbo-config-spring/pom.xml | 6 ++ .../ServiceAnnotationBeanPostProcessor.java | 95 ++++++++++++++----- .../annotation/provider/HelloServiceImpl.java | 3 +- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/dubbo-config/dubbo-config-spring/pom.xml b/dubbo-config/dubbo-config-spring/pom.xml index 04a8223bcb7..4e579382df9 100644 --- a/dubbo-config/dubbo-config-spring/pom.xml +++ b/dubbo-config/dubbo-config-spring/pom.xml @@ -29,6 +29,11 @@ false + + org.apache.dubbo + dubbo-compatible + ${project.parent.version} + org.apache.dubbo dubbo-config-api @@ -128,5 +133,6 @@ tomcat-embed-core test + diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java index 2dec2d32b0a..3952ebebe83 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java @@ -43,6 +43,7 @@ import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.context.annotation.ConfigurationClassPostProcessor; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.filter.AnnotationTypeFilter; @@ -52,6 +53,7 @@ import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -65,6 +67,7 @@ import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR; import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; +import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes; import static org.springframework.util.ClassUtils.resolveClassName; /** @@ -132,6 +135,14 @@ private void registerServiceBeans(Set packagesToScan, BeanDefinitionRegi scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class)); + /** + * Add the compatibility for legacy Dubbo's @Service + * + * The issue : https://github.com/apache/dubbo/issues/4330 + * @since 2.7.3 + */ + scanner.addIncludeFilter(new AnnotationTypeFilter(com.alibaba.dubbo.config.annotation.Service.class)); + for (String packageToScan : packagesToScan) { // Registers @Service Bean first @@ -250,17 +261,22 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean Class beanClass = resolveClass(beanDefinitionHolder); - Service service = findAnnotation(beanClass, Service.class); + Annotation service = findServiceAnnotation(beanClass); + + /** + * The {@link AnnotationAttributes} of @Service annotation + */ + AnnotationAttributes serviceAnnotationAttributes = getAnnotationAttributes(service, false, false); - Class interfaceClass = resolveServiceInterfaceClass(beanClass, service); + Class interfaceClass = resolveServiceInterfaceClass(beanClass, serviceAnnotationAttributes); String annotatedServiceBeanName = beanDefinitionHolder.getBeanName(); AbstractBeanDefinition serviceBeanDefinition = - buildServiceBeanDefinition(service, interfaceClass, annotatedServiceBeanName); + buildServiceBeanDefinition(service, serviceAnnotationAttributes, interfaceClass, annotatedServiceBeanName); // ServiceBean Bean name - String beanName = generateServiceBeanName(service, interfaceClass); + String beanName = generateServiceBeanName(serviceAnnotationAttributes, interfaceClass); if (scanner.checkCandidate(beanName, serviceBeanDefinition)) { // check duplicated candidate bean registry.registerBeanDefinition(beanName, serviceBeanDefinition); @@ -282,29 +298,52 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean } + + /** + * Find the {@link Annotation annotation} of @Service + * + * @param beanClass the {@link Class class} of Bean + * @return null if not found + * @since 2.7.3 + */ + private Annotation findServiceAnnotation(Class beanClass) { + Annotation service = findAnnotation(beanClass, Service.class); + if (service == null) { + service = findAnnotation(beanClass, com.alibaba.dubbo.config.annotation.Service.class); + } + return service; + } + /** * Generates the bean name of {@link ServiceBean} * - * @param service - * @param interfaceClass the class of interface annotated {@link Service} + * @param serviceAnnotationAttributes + * @param interfaceClass the class of interface annotated {@link Service} * @return ServiceBean@interfaceClassName#annotatedServiceBeanName - * @since 2.5.9 + * @since 2.7.3 */ - private String generateServiceBeanName(Service service, Class interfaceClass) { - ServiceBeanNameBuilder builder = create(service, interfaceClass, environment); - + private String generateServiceBeanName(AnnotationAttributes serviceAnnotationAttributes, Class interfaceClass) { + ServiceBeanNameBuilder builder = create(interfaceClass, environment); + builder.group(serviceAnnotationAttributes.getString("group")); + builder.version(serviceAnnotationAttributes.getString("version")); return builder.build(); } - private Class resolveServiceInterfaceClass(Class annotatedServiceBeanClass, Service service) { + /** + * @param annotatedServiceBeanClass + * @param serviceAnnotationAttributes + * @return + * @since 2.7.3 + */ + private Class resolveServiceInterfaceClass(Class annotatedServiceBeanClass, AnnotationAttributes serviceAnnotationAttributes) { - Class interfaceClass = service.interfaceClass(); + Class interfaceClass = serviceAnnotationAttributes.getClass("interfaceClass"); if (void.class.equals(interfaceClass)) { interfaceClass = null; - String interfaceClassName = service.interfaceName(); + String interfaceClassName = serviceAnnotationAttributes.getString("interfaceName"); if (StringUtils.hasText(interfaceClassName)) { if (ClassUtils.isPresent(interfaceClassName, classLoader)) { @@ -361,7 +400,19 @@ private Set resolvePackagesToScan(Set packagesToScan) { return resolvedPackagesToScan; } - private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class interfaceClass, + /** + * Build the {@link AbstractBeanDefinition Bean Definition} + * + * @param serviceAnnotation + * @param serviceAnnotationAttributes + * @param interfaceClass + * @param annotatedServiceBeanName + * @return + * @since 2.7.3 + */ + private AbstractBeanDefinition buildServiceBeanDefinition(Annotation serviceAnnotation, + AnnotationAttributes serviceAnnotationAttributes, + Class interfaceClass, String annotatedServiceBeanName) { BeanDefinitionBuilder builder = rootBeanDefinition(ServiceBean.class); @@ -373,19 +424,19 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol", "interface", "interfaceName", "parameters"); - propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(service, environment, ignoreAttributeNames)); + propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(serviceAnnotation, environment, ignoreAttributeNames)); // References "ref" property to annotated-@Service Bean addPropertyReference(builder, "ref", annotatedServiceBeanName); // Set interface builder.addPropertyValue("interface", interfaceClass.getName()); // Convert parameters into map - builder.addPropertyValue("parameters", convertParameters(service.parameters())); + builder.addPropertyValue("parameters", convertParameters(serviceAnnotationAttributes.getStringArray("parameters"))); /** * Add {@link org.apache.dubbo.config.ProviderConfig} Bean reference */ - String providerConfigBeanName = service.provider(); + String providerConfigBeanName = serviceAnnotationAttributes.getString("provider"); if (StringUtils.hasText(providerConfigBeanName)) { addPropertyReference(builder, "provider", providerConfigBeanName); } @@ -393,7 +444,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class /** * Add {@link org.apache.dubbo.config.MonitorConfig} Bean reference */ - String monitorConfigBeanName = service.monitor(); + String monitorConfigBeanName = serviceAnnotationAttributes.getString("monitor"); if (StringUtils.hasText(monitorConfigBeanName)) { addPropertyReference(builder, "monitor", monitorConfigBeanName); } @@ -401,7 +452,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class /** * Add {@link org.apache.dubbo.config.ApplicationConfig} Bean reference */ - String applicationConfigBeanName = service.application(); + String applicationConfigBeanName = serviceAnnotationAttributes.getString("application"); if (StringUtils.hasText(applicationConfigBeanName)) { addPropertyReference(builder, "application", applicationConfigBeanName); } @@ -409,7 +460,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class /** * Add {@link org.apache.dubbo.config.ModuleConfig} Bean reference */ - String moduleConfigBeanName = service.module(); + String moduleConfigBeanName = serviceAnnotationAttributes.getString("module"); if (StringUtils.hasText(moduleConfigBeanName)) { addPropertyReference(builder, "module", moduleConfigBeanName); } @@ -418,7 +469,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class /** * Add {@link org.apache.dubbo.config.RegistryConfig} Bean reference */ - String[] registryConfigBeanNames = service.registry(); + String[] registryConfigBeanNames = serviceAnnotationAttributes.getStringArray("registry"); List registryRuntimeBeanReferences = toRuntimeBeanReferences(registryConfigBeanNames); @@ -429,7 +480,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class /** * Add {@link org.apache.dubbo.config.ProtocolConfig} Bean reference */ - String[] protocolConfigBeanNames = service.protocol(); + String[] protocolConfigBeanNames = serviceAnnotationAttributes.getStringArray("protocol"); List protocolRuntimeBeanReferences = toRuntimeBeanReferences(protocolConfigBeanNames); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/HelloServiceImpl.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/HelloServiceImpl.java index 982e52a3f73..0044ea8c350 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/HelloServiceImpl.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/HelloServiceImpl.java @@ -16,9 +16,10 @@ */ package org.apache.dubbo.config.spring.context.annotation.provider; -import org.apache.dubbo.config.annotation.Service; import org.apache.dubbo.config.spring.api.HelloService; +import com.alibaba.dubbo.config.annotation.Service; + /** * {@link HelloService} Implementation just annotating Dubbo's {@link Service} * From fa619c9842113ca320e0f72050987f77a6c7f92f Mon Sep 17 00:00:00 2001 From: Huang YunKun Date: Fri, 21 Jun 2019 08:43:27 +0800 Subject: [PATCH 05/11] [Dubbo-4355] Fix dubbo.jar do not contain "serialization-protobuf-json" module issue (#4356) (#4364) * include protobuf-json jar to dubbo --- dubbo-all/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-all/pom.xml b/dubbo-all/pom.xml index d8c8949c13e..eaa2dbc92ea 100644 --- a/dubbo-all/pom.xml +++ b/dubbo-all/pom.xml @@ -622,7 +622,7 @@ org.apache.dubbo:dubbo-serialization-jdk org.apache.dubbo:dubbo-serialization-protostuff org.apache.dubbo:dubbo-serialization-gson - org.apache.dubbo:dubbo-serialization-googlePb + org.apache.dubbo:dubbo-serialization-protobuf-json org.apache.dubbo:dubbo-configcenter-api org.apache.dubbo:dubbo-configcenter-definition org.apache.dubbo:dubbo-configcenter-apollo From 28b8fba8ef2a7453544e174965f1ce0748123537 Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Mon, 24 Jun 2019 14:49:13 +0800 Subject: [PATCH 06/11] Polish apache/incubator-dubbo#4330 : Optimization --- .../ServiceAnnotationBeanPostProcessor.java | 55 +---------- .../annotation/ServiceBeanNameBuilder.java | 42 +++++---- .../config/spring/util/AnnotationUtils.java | 94 ++++++++++++++++++- .../provider/ProviderConfiguration.java | 4 +- 4 files changed, 123 insertions(+), 72 deletions(-) diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java index 3952ebebe83..0edc5f333ba 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java @@ -47,8 +47,6 @@ import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.filter.AnnotationTypeFilter; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -63,6 +61,7 @@ import java.util.Set; import static org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.create; +import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveServiceInterfaceClass; import static org.apache.dubbo.config.spring.util.ObjectUtils.of; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR; @@ -268,7 +267,7 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean */ AnnotationAttributes serviceAnnotationAttributes = getAnnotationAttributes(service, false, false); - Class interfaceClass = resolveServiceInterfaceClass(beanClass, serviceAnnotationAttributes); + Class interfaceClass = resolveServiceInterfaceClass(serviceAnnotationAttributes, beanClass); String annotatedServiceBeanName = beanDefinitionHolder.getBeanName(); @@ -323,56 +322,12 @@ private Annotation findServiceAnnotation(Class beanClass) { * @since 2.7.3 */ private String generateServiceBeanName(AnnotationAttributes serviceAnnotationAttributes, Class interfaceClass) { - ServiceBeanNameBuilder builder = create(interfaceClass, environment); - builder.group(serviceAnnotationAttributes.getString("group")); - builder.version(serviceAnnotationAttributes.getString("version")); + ServiceBeanNameBuilder builder = create(interfaceClass, environment) + .group(serviceAnnotationAttributes.getString("group")) + .version(serviceAnnotationAttributes.getString("version")); return builder.build(); } - /** - * @param annotatedServiceBeanClass - * @param serviceAnnotationAttributes - * @return - * @since 2.7.3 - */ - private Class resolveServiceInterfaceClass(Class annotatedServiceBeanClass, AnnotationAttributes serviceAnnotationAttributes) { - - Class interfaceClass = serviceAnnotationAttributes.getClass("interfaceClass"); - - if (void.class.equals(interfaceClass)) { - - interfaceClass = null; - - String interfaceClassName = serviceAnnotationAttributes.getString("interfaceName"); - - if (StringUtils.hasText(interfaceClassName)) { - if (ClassUtils.isPresent(interfaceClassName, classLoader)) { - interfaceClass = resolveClassName(interfaceClassName, classLoader); - } - } - - } - - if (interfaceClass == null) { - // Find all interfaces from the annotated class - // To resolve an issue : https://github.com/apache/dubbo/issues/3251 - Class[] allInterfaces = ClassUtils.getAllInterfacesForClass(annotatedServiceBeanClass); - - if (allInterfaces.length > 0) { - interfaceClass = allInterfaces[0]; - } - - } - - Assert.notNull(interfaceClass, - "@Service interfaceClass() or interfaceName() or interface class must be present!"); - - Assert.isTrue(interfaceClass.isInterface(), - "The type that was annotated @Service is not an interface!"); - - return interfaceClass; - } - private Class resolveClass(BeanDefinitionHolder beanDefinitionHolder) { BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition(); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java index 5d272515ab3..390e0b6cf1f 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java @@ -21,10 +21,12 @@ import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.ServiceBean; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName; +import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes; /** * Dubbo {@link Service @Service} Bean Builder @@ -39,6 +41,7 @@ public class ServiceBeanNameBuilder { private static final String SEPARATOR = ":"; + // Required private final String interfaceClassName; private final Environment environment; @@ -48,25 +51,30 @@ public class ServiceBeanNameBuilder { private String group; + private ServiceBeanNameBuilder(Class interfaceClass, Environment environment) { + this(interfaceClass.getName(), environment); + } + private ServiceBeanNameBuilder(String interfaceClassName, Environment environment) { this.interfaceClassName = interfaceClassName; this.environment = environment; } - private ServiceBeanNameBuilder(Class interfaceClass, Environment environment) { - this(interfaceClass.getName(), environment); - } - - private ServiceBeanNameBuilder(Service service, Class interfaceClass, Environment environment) { - this(resolveInterfaceName(service, interfaceClass), environment); - this.group(service.group()); - this.version(service.version()); + private ServiceBeanNameBuilder(AnnotationAttributes attributes, Class defaultInterfaceClass, Environment environment) { + this(resolveInterfaceName(attributes, defaultInterfaceClass), environment); + this.group(attributes.getString("group")); + this.version(attributes.getString("version")); } - private ServiceBeanNameBuilder(Reference reference, Class interfaceClass, Environment environment) { - this(resolveInterfaceName(reference, interfaceClass), environment); - this.group(reference.group()); - this.version(reference.version()); + /** + * @param attributes + * @param defaultInterfaceClass + * @param environment + * @return + * @since 2.7.3 + */ + public static ServiceBeanNameBuilder create(AnnotationAttributes attributes, Class defaultInterfaceClass, Environment environment) { + return new ServiceBeanNameBuilder(attributes, defaultInterfaceClass, environment); } public static ServiceBeanNameBuilder create(Class interfaceClass, Environment environment) { @@ -74,16 +82,16 @@ public static ServiceBeanNameBuilder create(Class interfaceClass, Environment } public static ServiceBeanNameBuilder create(Service service, Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(service, interfaceClass, environment); + return create(getAnnotationAttributes(service, false, false), interfaceClass, environment); } public static ServiceBeanNameBuilder create(Reference reference, Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(reference, interfaceClass, environment); + return create(getAnnotationAttributes(reference, false, false), interfaceClass, environment); } private static void append(StringBuilder builder, String value) { if (StringUtils.hasText(value)) { - builder.append(value).append(SEPARATOR); + builder.append(SEPARATOR).append(value); } } @@ -98,14 +106,14 @@ public ServiceBeanNameBuilder version(String version) { } public String build() { - StringBuilder beanNameBuilder = new StringBuilder("ServiceBean").append(SEPARATOR); + StringBuilder beanNameBuilder = new StringBuilder("ServiceBean"); // Required append(beanNameBuilder, interfaceClassName); // Optional append(beanNameBuilder, version); append(beanNameBuilder, group); // Build and remove last ":" - String rawBeanName = beanNameBuilder.substring(0, beanNameBuilder.length() - 1); + String rawBeanName = beanNameBuilder.toString(); // Resolve placeholders return environment.resolvePlaceholders(rawBeanName); } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java index 9832e105624..600c1b72280 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java @@ -19,9 +19,11 @@ import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.annotation.Service; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertyResolver; -import org.springframework.util.StringUtils; +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; @@ -41,8 +43,11 @@ import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes; import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue; +import static org.springframework.util.ClassUtils.getAllInterfacesForClass; +import static org.springframework.util.ClassUtils.resolveClassName; import static org.springframework.util.CollectionUtils.arrayToList; import static org.springframework.util.ObjectUtils.nullSafeEquals; +import static org.springframework.util.StringUtils.hasText; import static org.springframework.util.StringUtils.trimWhitespace; /** @@ -53,11 +58,13 @@ */ public class AnnotationUtils { + + @Deprecated public static String resolveInterfaceName(Service service, Class defaultInterfaceClass) throws IllegalStateException { String interfaceName; - if (StringUtils.hasText(service.interfaceName())) { + if (hasText(service.interfaceName())) { interfaceName = service.interfaceName(); } else if (!void.class.equals(service.interfaceClass())) { interfaceName = service.interfaceClass().getName(); @@ -73,6 +80,85 @@ public static String resolveInterfaceName(Service service, Class defaultInter } + /** + * Resolve the interface name from {@link AnnotationAttributes} + * + * @param attributes {@link AnnotationAttributes} instance, may be {@link Service @Service} or {@link Reference @Reference} + * @param defaultInterfaceClass the default {@link Class class} of interface + * @return the interface name if found + * @throws IllegalStateException if interface name was not found + */ + public static String resolveInterfaceName(AnnotationAttributes attributes, Class defaultInterfaceClass) { + + String interfaceName = null; + + Class interfaceClass = attributes.getClass("interfaceClass"); + if (interfaceClass != null && !void.class.equals(interfaceClass)) { + interfaceName = interfaceClass.getName(); + } else if ((!hasText(interfaceName = attributes.getString("interfaceName")))) { + interfaceName = defaultInterfaceClass.isInterface() ? defaultInterfaceClass.getName() : null; + } + + if (!hasText(interfaceName)) { + throw new IllegalStateException( + "The @Service undefined interfaceClass or interfaceName, and the type " + + defaultInterfaceClass.getName() + " is not a interface."); + } + + return interfaceName; + } + + /** + * Resolve the {@link Class class} of Dubbo Service interface from the specified + * {@link AnnotationAttributes annotation attributes} and annotated {@link Class class}. + * + * @param attributes {@link AnnotationAttributes annotation attributes} + * @param annotatedClass the annotated {@link Class class}. + * @return the {@link Class class} of Dubbo Service interface + * @throws IllegalArgumentException if can't resolved + */ + public static Class resolveServiceInterfaceClass(AnnotationAttributes attributes, Class annotatedClass) + throws IllegalArgumentException { + + ClassLoader classLoader = annotatedClass.getClassLoader(); + + Class interfaceClass = attributes.getClass("interfaceClass"); + + if (void.class.equals(interfaceClass)) { // default or set void.class for purpose. + + interfaceClass = null; + + String interfaceClassName = attributes.getString("interfaceName"); + + if (hasText(interfaceClassName)) { + if (ClassUtils.isPresent(interfaceClassName, classLoader)) { + interfaceClass = resolveClassName(interfaceClassName, classLoader); + } + } + + } + + if (interfaceClass == null) { + // Find all interfaces from the annotated class + // To resolve an issue : https://github.com/apache/dubbo/issues/3251 + Class[] allInterfaces = getAllInterfacesForClass(annotatedClass); + + if (allInterfaces.length > 0) { + interfaceClass = allInterfaces[0]; + } + + } + + Assert.notNull(interfaceClass, + "@Service interfaceClass() or interfaceName() or interface class must be present!"); + + Assert.isTrue(interfaceClass.isInterface(), + "The type that was annotated @Service is not an interface!"); + + return interfaceClass; + } + + @Deprecated public static String resolveInterfaceName(Reference reference, Class defaultInterfaceClass) throws IllegalStateException { @@ -262,10 +348,10 @@ public static Map getAttributes(Annotation annotation, PropertyR * @since 2.7.1 * ignore annotation member */ - if (attributeValue.getClass().isAnnotation()){ + if (attributeValue.getClass().isAnnotation()) { continue; } - if (attributeValue.getClass().isArray() && attributeValue.getClass().getComponentType().isAnnotation()){ + if (attributeValue.getClass().isArray() && attributeValue.getClass().getComponentType().isAnnotation()) { continue; } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java index c77322848d6..9c45d7134a5 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java @@ -22,6 +22,7 @@ import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.PropertySource; import org.springframework.transaction.PlatformTransactionManager; @@ -30,7 +31,8 @@ import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.EnableTransactionManagement; -@DubboComponentScan(basePackages = "org.apache.dubbo.config.spring.context.annotation.provider") +@DubboComponentScan(basePackages = "org.apache.dubbo") +@ComponentScan(basePackages = "org.apache.dubbo") @PropertySource("META-INF/default.properties") @EnableTransactionManagement public class ProviderConfiguration { From 7285ce9bf2ed8ec56d06fb0b3c7b0f1d7aa81192 Mon Sep 17 00:00:00 2001 From: HzjNeverStop <441627022@qq.com> Date: Mon, 24 Jun 2019 21:21:10 +0800 Subject: [PATCH 07/11] [Dubbo-4299]Fix NPE when PojoUtils realize Null element in collection(#4299) (#4300) * fix NPE when PojoUtils realize Null element in collection(#4299) * add unit tests for bugfix of PojoUtils NPE(#4299) * revert import (#4299) --- .../apache/dubbo/common/utils/PojoUtils.java | 2 +- .../dubbo/common/utils/PojoUtilsTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index 3bab26134e3..52204acb4dd 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -366,7 +366,7 @@ private static Object realize0(Object pojo, Class type, Type genericType, fin history.put(pojo, dest); for (Object obj : src) { Type keyType = getGenericClassByIndex(genericType, 0); - Class keyClazz = obj.getClass(); + Class keyClazz = obj == null ? null : obj.getClass(); if (keyType instanceof Class) { keyClazz = (Class) keyType; } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java index 3595efb99f6..f0ab173b4f8 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java @@ -37,6 +37,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.HashSet; import java.util.Map; import java.util.UUID; @@ -682,6 +683,24 @@ public void testDateTimeTimestamp() throws Exception { assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(timestamp)); } + @Test + public void testRealizeCollectionWithNullElement() { + LinkedList listStr = new LinkedList<>(); + listStr.add("arrayValue"); + listStr.add(null); + HashSet setStr = new HashSet<>(); + setStr.add("setValue"); + setStr.add(null); + + Object listResult = PojoUtils.realize(listStr, LinkedList.class); + assertEquals(LinkedList.class, listResult.getClass()); + assertEquals(listResult, listStr); + + Object setResult = PojoUtils.realize(setStr, HashSet.class); + assertEquals(HashSet.class, setResult.getClass()); + assertEquals(setResult, setStr); + } + public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } From 18773aa4d0925693839be7f53c0eee27697ebc1d Mon Sep 17 00:00:00 2001 From: cvictory Date: Tue, 25 Jun 2019 15:52:54 +0800 Subject: [PATCH 08/11] fix qos configuration cannot work after added 'qos-enable' style support (#4378) fixes #4377 --- .../dubbo/common/constants/QosConstants.java | 12 +++++-- .../dubbo/config/ApplicationConfig.java | 31 ++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/QosConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/QosConstants.java index 6815daedb48..cabac0e4e65 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/QosConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/QosConstants.java @@ -22,9 +22,15 @@ */ public interface QosConstants { - String QOS_ENABLE = "qos-enable"; + String QOS_ENABLE = "qos.enable"; - String QOS_PORT = "qos-port"; + String QOS_PORT = "qos.port"; - String ACCEPT_FOREIGN_IP = "qos-accept-foreign-ip"; + String ACCEPT_FOREIGN_IP = "qos.accept.foreign.ip"; + + String QOS_ENABLE_COMPATIBLE = "qos-enable"; + + String QOS_PORT_COMPATIBLE = "qos-port"; + + String ACCEPT_FOREIGN_IP_COMPATIBLE = "qos-accept-foreign-ip"; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java index 83fc6db2e9a..824a0dde423 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java @@ -30,8 +30,11 @@ import static org.apache.dubbo.common.constants.CommonConstants.DUMP_DIRECTORY; import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY; import static org.apache.dubbo.common.constants.QosConstants.ACCEPT_FOREIGN_IP; +import static org.apache.dubbo.common.constants.QosConstants.ACCEPT_FOREIGN_IP_COMPATIBLE; import static org.apache.dubbo.common.constants.QosConstants.QOS_ENABLE; +import static org.apache.dubbo.common.constants.QosConstants.QOS_ENABLE_COMPATIBLE; import static org.apache.dubbo.common.constants.QosConstants.QOS_PORT; +import static org.apache.dubbo.common.constants.QosConstants.QOS_PORT_COMPATIBLE; import static org.apache.dubbo.config.Constants.ARCHITECTURE; import static org.apache.dubbo.config.Constants.DEVELOPMENT_ENVIRONMENT; import static org.apache.dubbo.config.Constants.ENVIRONMENT; @@ -317,36 +320,34 @@ public void setQosAcceptForeignIp(Boolean qosAcceptForeignIp) { this.qosAcceptForeignIp = qosAcceptForeignIp; } - @Deprecated - @Parameter(key = "qos.enable", excluded = true) - public Boolean getQosEnableDeprecated() { + /** + * The format is the same as the springboot, including: getQosEnableCompatible(), getQosPortCompatible(), getQosAcceptForeignIpCompatible(). + * @return + */ + @Parameter(key = QOS_ENABLE_COMPATIBLE, excluded = true) + public Boolean getQosEnableCompatible() { return getQosEnable(); } - @Deprecated - public void setQosEnableDeprecated(Boolean qosEnable) { + public void setQosEnableCompatible(Boolean qosEnable) { setQosEnable(qosEnable); } - @Deprecated - @Parameter(key = "qos.port", excluded = true) - public Integer getQosPortDeprecated() { + @Parameter(key = QOS_PORT_COMPATIBLE, excluded = true) + public Integer getQosPortCompatible() { return getQosPort(); } - @Deprecated - public void setQosPortDeprecated(Integer qosPort) { + public void setQosPortCompatible(Integer qosPort) { this.setQosPort(qosPort); } - @Deprecated - @Parameter(key = "qos.accept.foreign.ip", excluded = true) - public Boolean getQosAcceptForeignIpDeprecated() { + @Parameter(key = ACCEPT_FOREIGN_IP_COMPATIBLE, excluded = true) + public Boolean getQosAcceptForeignIpCompatible() { return this.getQosAcceptForeignIp(); } - @Deprecated - public void setQosAcceptForeignIpDeprecated(Boolean qosAcceptForeignIp) { + public void setQosAcceptForeignIpCompatible(Boolean qosAcceptForeignIp) { this.setQosAcceptForeignIp(qosAcceptForeignIp); } From 151ed22d853c2b017f7374b7ede70eece9393681 Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Wed, 26 Jun 2019 13:13:33 +0800 Subject: [PATCH 09/11] Polish apache/incubator-dubbo#4330 : Refactor code --- .../java/com/alibaba/dubbo/config/annotation/Reference.java | 0 .../java/com/alibaba/dubbo/config/annotation/Service.java | 0 dubbo-config/dubbo-config-spring/pom.xml | 5 ----- 3 files changed, 5 deletions(-) rename {dubbo-compatible => dubbo-config/dubbo-config-api}/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java (100%) rename {dubbo-compatible => dubbo-config/dubbo-config-api}/src/main/java/com/alibaba/dubbo/config/annotation/Service.java (100%) diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java similarity index 100% rename from dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java rename to dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Service.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Service.java similarity index 100% rename from dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Service.java rename to dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Service.java diff --git a/dubbo-config/dubbo-config-spring/pom.xml b/dubbo-config/dubbo-config-spring/pom.xml index 4e579382df9..405b29e3230 100644 --- a/dubbo-config/dubbo-config-spring/pom.xml +++ b/dubbo-config/dubbo-config-spring/pom.xml @@ -29,11 +29,6 @@ false - - org.apache.dubbo - dubbo-compatible - ${project.parent.version} - org.apache.dubbo dubbo-config-api From dae67c5eac5f111ef9ebbfd441f7ebf39892e0f8 Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Thu, 27 Jun 2019 13:11:57 +0800 Subject: [PATCH 10/11] Polish apache/incubator-dubbo#4409 : [Enhancement] @EnableDubboConfig reduces the duplicated DubboConfigConfiguration registration --- .../AnnotatedBeanDefinitionRegistryUtils.java | 62 +++++++++++++++- ...otatedBeanDefinitionRegistryUtilsTest.java | 74 +++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java index bf065f8b085..c46ca8adea7 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java @@ -18,18 +18,27 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; +import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.ObjectUtils; import java.lang.annotation.Annotation; -import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Objects; + +import static java.lang.String.format; +import static java.util.Arrays.asList; +import static org.springframework.util.ClassUtils.resolveClassName; /** * Annotated {@link BeanDefinition} Utilities *

* The source code is cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/AnnotatedBeanDefinitionRegistryUtils.java + * * @since 2.6.6 */ public abstract class AnnotatedBeanDefinitionRegistryUtils { @@ -37,10 +46,47 @@ public abstract class AnnotatedBeanDefinitionRegistryUtils { private static final Log logger = LogFactory.getLog(AnnotatedBeanDefinitionRegistryUtils.class); /** - * Register Beans + * Is present bean that was registered by the specified {@link Annotation annotated} {@link Class class} + * + * @param registry {@link BeanDefinitionRegistry} + * @param annotatedClass the {@link Annotation annotated} {@link Class class} + * @return if present, return true, or false + * @since 2.7.3 + */ + public static boolean isPresentBean(BeanDefinitionRegistry registry, Class annotatedClass) { + + boolean present = false; + + String[] beanNames = registry.getBeanDefinitionNames(); + + ClassLoader classLoader = annotatedClass.getClassLoader(); + + for (String beanName : beanNames) { + BeanDefinition beanDefinition = registry.getBeanDefinition(beanName); + if (beanDefinition instanceof AnnotatedBeanDefinition) { + AnnotationMetadata annotationMetadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata(); + String className = annotationMetadata.getClassName(); + Class targetClass = resolveClassName(className, classLoader); + present = Objects.equals(targetClass, annotatedClass); + if (present) { + if (logger.isDebugEnabled()) { + logger.debug(format("The annotatedClass[class : %s , bean name : %s] was present in registry[%s]", + className, beanName, registry)); + } + break; + } + } + } + + return present; + } + + /** + * Register Beans if not present in {@link BeanDefinitionRegistry registry} * * @param registry {@link BeanDefinitionRegistry} * @param annotatedClasses {@link Annotation annotation} class + * @revision 2.7.3 {@link #isPresentBean(BeanDefinitionRegistry, Class)} */ public static void registerBeans(BeanDefinitionRegistry registry, Class... annotatedClasses) { @@ -50,10 +96,20 @@ public static void registerBeans(BeanDefinitionRegistry registry, Class... an boolean debugEnabled = logger.isDebugEnabled(); + // Remove all annotated-classes that have been registered + Iterator> iterator = new ArrayList<>(asList(annotatedClasses)).iterator(); + + while (iterator.hasNext()) { + Class annotatedClass = iterator.next(); + if (isPresentBean(registry, annotatedClass)) { + iterator.remove(); + } + } + AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry); if (debugEnabled) { - logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + Arrays.asList(annotatedClasses) + " ."); + logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + asList(annotatedClasses) + " ."); } reader.register(annotatedClasses); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java new file mode 100644 index 00000000000..05c4a78a7bc --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java @@ -0,0 +1,74 @@ +/* + * 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.util; + +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Service; + +import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.isPresentBean; +import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * {@link AnnotatedBeanDefinitionRegistryUtils} Test + * + * @since 2.7.3 + */ +public class AnnotatedBeanDefinitionRegistryUtilsTest { + + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + + @AfterEach + public void destroy() { + context.close(); + } + + @Test + public void testIsPresentBean() { + + assertFalse(isPresentBean(context, A.class)); + + context.register(A.class); + + for (int i = 0; i < 9; i++) { + assertTrue(isPresentBean(context, A.class)); + } + + } + + @Test + public void testRegisterBeans() { + + registerBeans(context, A.class, A.class); + + context.refresh(); + + A a = context.getBean(A.class); + + Assert.assertNotNull(a); + } + + + @Service + static class A { + + } +} From 1a9ddd866658f6ccae056e14face4152551de17e Mon Sep 17 00:00:00 2001 From: wenchao wang Date: Thu, 27 Jun 2019 01:53:02 -0500 Subject: [PATCH 11/11] allow @Service and @Reference to merge attributes form annotations in lower levels of the annotation hierachy. (#4078) * allow @Service and @Reference to merge attributes form annotations in lower levels of the annotation hierachy. * remove author information & not introduce all dependencies for test --- ...eReferenceAnnotationBeanPostProcessor.java | 8 +- ...bleServiceAnnotationBeanPostProcessor.java | 4 +- .../AnnotationInjectedBeanPostProcessor.java | 8 +- .../ServiceAnnotationBeanPostProcessor.java | 4 +- .../annotation/merged/MergedReference.java | 40 ++++++++++ .../annotation/merged/MergedService.java | 40 ++++++++++ .../annotation/MergedAnnotationTest.java | 74 +++++++++++++++++++ 7 files changed, 166 insertions(+), 12 deletions(-) create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedReference.java create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedService.java create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MergedAnnotationTest.java diff --git a/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleReferenceAnnotationBeanPostProcessor.java b/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleReferenceAnnotationBeanPostProcessor.java index d6826a5f819..56844be84b5 100644 --- a/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleReferenceAnnotationBeanPostProcessor.java +++ b/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleReferenceAnnotationBeanPostProcessor.java @@ -55,8 +55,8 @@ import static org.springframework.core.BridgeMethodResolver.findBridgedMethod; import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; -import static org.springframework.core.annotation.AnnotationUtils.getAnnotation; +import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; +import static org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation; /** * {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation @@ -116,7 +116,7 @@ private List findFieldReferenceMetadata(final Class be @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { - Reference reference = getAnnotation(field, Reference.class); + Reference reference = getMergedAnnotation(field, Reference.class); if (reference != null) { @@ -157,7 +157,7 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess return; } - Reference reference = findAnnotation(bridgedMethod, Reference.class); + Reference reference = findMergedAnnotation(bridgedMethod, Reference.class); if (reference != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) { if (Modifier.isStatic(method.getModifiers())) { diff --git a/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleServiceAnnotationBeanPostProcessor.java b/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleServiceAnnotationBeanPostProcessor.java index 3d0370c7cc2..470352fb5a3 100644 --- a/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleServiceAnnotationBeanPostProcessor.java +++ b/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleServiceAnnotationBeanPostProcessor.java @@ -63,7 +63,7 @@ import static org.apache.dubbo.config.spring.util.ObjectUtils.of; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; +import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; import static org.springframework.util.ClassUtils.resolveClassName; /** @@ -251,7 +251,7 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean Class beanClass = resolveClass(beanDefinitionHolder); - Service service = findAnnotation(beanClass, Service.class); + Service service = findMergedAnnotation(beanClass, Service.class); Class interfaceClass = resolveServiceInterfaceClass(beanClass, service); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java index 53ff695da07..23f85584e6a 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java @@ -59,8 +59,8 @@ import static org.apache.dubbo.config.spring.util.ClassUtils.resolveGenericType; import static org.springframework.core.BridgeMethodResolver.findBridgedMethod; import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; -import static org.springframework.core.annotation.AnnotationUtils.getAnnotation; +import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; +import static org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation; /** * Abstract generic {@link BeanPostProcessor} implementation for customized annotation that annotated injected-object. @@ -150,7 +150,7 @@ private List findFiel ReflectionUtils.doWithFields(beanClass, field -> { - A annotation = getAnnotation(field, getAnnotationType()); + A annotation = getMergedAnnotation(field, getAnnotationType()); if (annotation != null) { @@ -188,7 +188,7 @@ private List findAnn return; } - A annotation = findAnnotation(bridgedMethod, getAnnotationType()); + A annotation = findMergedAnnotation(bridgedMethod, getAnnotationType()); if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) { if (Modifier.isStatic(method.getModifiers())) { diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java index 2dec2d32b0a..8c33d8619cc 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java @@ -64,7 +64,7 @@ import static org.apache.dubbo.config.spring.util.ObjectUtils.of; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; +import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; import static org.springframework.util.ClassUtils.resolveClassName; /** @@ -250,7 +250,7 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean Class beanClass = resolveClass(beanDefinitionHolder); - Service service = findAnnotation(beanClass, Service.class); + Service service = findMergedAnnotation(beanClass, Service.class); Class interfaceClass = resolveServiceInterfaceClass(beanClass, service); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedReference.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedReference.java new file mode 100644 index 00000000000..ddf3ac77c46 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedReference.java @@ -0,0 +1,40 @@ +/* + * 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.annotation.merged; + + +import org.apache.dubbo.config.annotation.Reference; +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Reference +public @interface MergedReference { + + @AliasFor(annotation = Reference.class, attribute = "group") + String group() default "dubbo"; + + @AliasFor(annotation = Reference.class, attribute = "version") + String version() default "1.0.0"; +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedService.java new file mode 100644 index 00000000000..1ecfbe08e4c --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedService.java @@ -0,0 +1,40 @@ +/* + * 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.annotation.merged; + + +import org.apache.dubbo.config.annotation.Service; +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +@Service +public @interface MergedService { + + @AliasFor(annotation = Service.class, attribute = "group") + String group() default "dubbo"; + + @AliasFor(annotation = Service.class, attribute = "version") + String version() default "1.0.0"; +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MergedAnnotationTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MergedAnnotationTest.java new file mode 100644 index 00000000000..353cc3a0d78 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MergedAnnotationTest.java @@ -0,0 +1,74 @@ +/* + * 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 org.apache.dubbo.config.annotation.Reference; +import org.apache.dubbo.config.annotation.Service; +import org.apache.dubbo.config.spring.annotation.merged.MergedReference; +import org.apache.dubbo.config.spring.annotation.merged.MergedService; +import org.apache.dubbo.config.spring.api.DemoService; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.util.ReflectionUtils; +import java.lang.reflect.Field; + +public class MergedAnnotationTest { + + @Test + public void testMergedReference() { + Field field = ReflectionUtils.findField(MergedAnnotationTest.TestBean1.class, "demoService"); + Reference reference = AnnotatedElementUtils.getMergedAnnotation(field, Reference.class); + Assert.assertEquals("dubbo", reference.group()); + Assert.assertEquals("1.0.0", reference.version()); + + Field field2 = ReflectionUtils.findField(MergedAnnotationTest.TestBean2.class, "demoService"); + Reference reference2 = AnnotatedElementUtils.getMergedAnnotation(field2, Reference.class); + Assert.assertEquals("group", reference2.group()); + Assert.assertEquals("2.0", reference2.version()); + } + + @Test + public void testMergedService() { + Service service1 = AnnotatedElementUtils.getMergedAnnotation(MergedAnnotationTest.DemoServiceImpl1.class, Service.class); + Assert.assertEquals("dubbo", service1.group()); + Assert.assertEquals("1.0.0", service1.version()); + + Service service2 = AnnotatedElementUtils.getMergedAnnotation(MergedAnnotationTest.DemoServiceImpl2.class, Service.class); + Assert.assertEquals("group", service2.group()); + Assert.assertEquals("2.0", service2.version()); + } + + @MergedService + public static class DemoServiceImpl1 { + } + + @MergedService(group = "group", version = "2.0") + public static class DemoServiceImpl2 { + } + + private static class TestBean1 { + @MergedReference + private DemoService demoService; + } + + private static class TestBean2 { + @MergedReference(group = "group", version = "2.0") + private DemoService demoService; + } + +}