From e957a8fb8051a355052a0e1fcda61cbe067596df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E8=B6=85?= Date: Thu, 16 May 2019 17:27:54 +0800 Subject: [PATCH 1/2] allow @Service and @Reference to merge attributes form annotations in lower levels of the annotation hierachy. --- ...eReferenceAnnotationBeanPostProcessor.java | 8 +- ...bleServiceAnnotationBeanPostProcessor.java | 4 +- .../AnnotationInjectedBeanPostProcessor.java | 8 +- .../ServiceAnnotationBeanPostProcessor.java | 4 +- .../annotation/merged/MergedReference.java | 39 ++++++++++ .../annotation/merged/MergedService.java | 39 ++++++++++ .../annotation/MergedAnnotationTest.java | 74 +++++++++++++++++++ 7 files changed, 164 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 305e3724132..8d55ab1fb6d 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 @@ -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; /** @@ -249,7 +249,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..608e8bdce88 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedReference.java @@ -0,0 +1,39 @@ +/* + * 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.*; + +/** + * @author dhchao11 + */ +@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..e214a80c834 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/merged/MergedService.java @@ -0,0 +1,39 @@ +/* + * 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.*; + +/** + * @author dhchao11 + */ +@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; + } + +} From 2ce451c04f9cd7c2ad76b9ae028dc96d571f7fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E8=B6=85?= Date: Tue, 25 Jun 2019 15:24:51 +0800 Subject: [PATCH 2/2] remove author information & not introduce all dependencies for test --- .../config/spring/annotation/merged/MergedReference.java | 9 +++++---- .../config/spring/annotation/merged/MergedService.java | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) 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 index 608e8bdce88..ddf3ac77c46 100644 --- 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 @@ -20,11 +20,12 @@ import org.apache.dubbo.config.annotation.Reference; import org.springframework.core.annotation.AliasFor; -import java.lang.annotation.*; +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; -/** - * @author dhchao11 - */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) 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 index e214a80c834..1ecfbe08e4c 100644 --- 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 @@ -20,11 +20,12 @@ import org.apache.dubbo.config.annotation.Service; import org.springframework.core.annotation.AliasFor; -import java.lang.annotation.*; +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; -/** - * @author dhchao11 - */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE})