Skip to content

Commit 1a9ddd8

Browse files
dhchao11mercyblitz
authored andcommitted
allow @service and @reference to merge attributes form annotations in lower levels of the annotation hierachy. (apache#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
1 parent 18773aa commit 1a9ddd8

File tree

7 files changed

+166
-12
lines changed

7 files changed

+166
-12
lines changed

dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleReferenceAnnotationBeanPostProcessor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555

5656
import static org.springframework.core.BridgeMethodResolver.findBridgedMethod;
5757
import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair;
58-
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
59-
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
58+
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;
59+
import static org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation;
6060

6161
/**
6262
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
@@ -116,7 +116,7 @@ private List<ReferenceFieldElement> findFieldReferenceMetadata(final Class<?> be
116116
@Override
117117
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
118118

119-
Reference reference = getAnnotation(field, Reference.class);
119+
Reference reference = getMergedAnnotation(field, Reference.class);
120120

121121
if (reference != null) {
122122

@@ -157,7 +157,7 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
157157
return;
158158
}
159159

160-
Reference reference = findAnnotation(bridgedMethod, Reference.class);
160+
Reference reference = findMergedAnnotation(bridgedMethod, Reference.class);
161161

162162
if (reference != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) {
163163
if (Modifier.isStatic(method.getModifiers())) {

dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/CompatibleServiceAnnotationBeanPostProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import static org.apache.dubbo.config.spring.util.ObjectUtils.of;
6464
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
6565
import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR;
66-
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
66+
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;
6767
import static org.springframework.util.ClassUtils.resolveClassName;
6868

6969
/**
@@ -251,7 +251,7 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean
251251

252252
Class<?> beanClass = resolveClass(beanDefinitionHolder);
253253

254-
Service service = findAnnotation(beanClass, Service.class);
254+
Service service = findMergedAnnotation(beanClass, Service.class);
255255

256256
Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);
257257

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
import static org.apache.dubbo.config.spring.util.ClassUtils.resolveGenericType;
6060
import static org.springframework.core.BridgeMethodResolver.findBridgedMethod;
6161
import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair;
62-
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
63-
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
62+
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;
63+
import static org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation;
6464

6565
/**
6666
* Abstract generic {@link BeanPostProcessor} implementation for customized annotation that annotated injected-object.
@@ -150,7 +150,7 @@ private List<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> findFiel
150150

151151
ReflectionUtils.doWithFields(beanClass, field -> {
152152

153-
A annotation = getAnnotation(field, getAnnotationType());
153+
A annotation = getMergedAnnotation(field, getAnnotationType());
154154

155155
if (annotation != null) {
156156

@@ -188,7 +188,7 @@ private List<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> findAnn
188188
return;
189189
}
190190

191-
A annotation = findAnnotation(bridgedMethod, getAnnotationType());
191+
A annotation = findMergedAnnotation(bridgedMethod, getAnnotationType());
192192

193193
if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) {
194194
if (Modifier.isStatic(method.getModifiers())) {

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
import static org.apache.dubbo.config.spring.util.ObjectUtils.of;
6565
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
6666
import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR;
67-
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
67+
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;
6868
import static org.springframework.util.ClassUtils.resolveClassName;
6969

7070
/**
@@ -250,7 +250,7 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean
250250

251251
Class<?> beanClass = resolveClass(beanDefinitionHolder);
252252

253-
Service service = findAnnotation(beanClass, Service.class);
253+
Service service = findMergedAnnotation(beanClass, Service.class);
254254

255255
Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);
256256

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.config.spring.annotation.merged;
18+
19+
20+
import org.apache.dubbo.config.annotation.Reference;
21+
import org.springframework.core.annotation.AliasFor;
22+
23+
import java.lang.annotation.Documented;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
import java.lang.annotation.ElementType;
28+
29+
@Documented
30+
@Retention(RetentionPolicy.RUNTIME)
31+
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
32+
@Reference
33+
public @interface MergedReference {
34+
35+
@AliasFor(annotation = Reference.class, attribute = "group")
36+
String group() default "dubbo";
37+
38+
@AliasFor(annotation = Reference.class, attribute = "version")
39+
String version() default "1.0.0";
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.config.spring.annotation.merged;
18+
19+
20+
import org.apache.dubbo.config.annotation.Service;
21+
import org.springframework.core.annotation.AliasFor;
22+
23+
import java.lang.annotation.Documented;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
import java.lang.annotation.ElementType;
28+
29+
@Documented
30+
@Retention(RetentionPolicy.RUNTIME)
31+
@Target({ElementType.TYPE})
32+
@Service
33+
public @interface MergedService {
34+
35+
@AliasFor(annotation = Service.class, attribute = "group")
36+
String group() default "dubbo";
37+
38+
@AliasFor(annotation = Service.class, attribute = "version")
39+
String version() default "1.0.0";
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.config.spring.beans.factory.annotation;
18+
19+
import org.apache.dubbo.config.annotation.Reference;
20+
import org.apache.dubbo.config.annotation.Service;
21+
import org.apache.dubbo.config.spring.annotation.merged.MergedReference;
22+
import org.apache.dubbo.config.spring.annotation.merged.MergedService;
23+
import org.apache.dubbo.config.spring.api.DemoService;
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
import org.springframework.core.annotation.AnnotatedElementUtils;
27+
import org.springframework.util.ReflectionUtils;
28+
import java.lang.reflect.Field;
29+
30+
public class MergedAnnotationTest {
31+
32+
@Test
33+
public void testMergedReference() {
34+
Field field = ReflectionUtils.findField(MergedAnnotationTest.TestBean1.class, "demoService");
35+
Reference reference = AnnotatedElementUtils.getMergedAnnotation(field, Reference.class);
36+
Assert.assertEquals("dubbo", reference.group());
37+
Assert.assertEquals("1.0.0", reference.version());
38+
39+
Field field2 = ReflectionUtils.findField(MergedAnnotationTest.TestBean2.class, "demoService");
40+
Reference reference2 = AnnotatedElementUtils.getMergedAnnotation(field2, Reference.class);
41+
Assert.assertEquals("group", reference2.group());
42+
Assert.assertEquals("2.0", reference2.version());
43+
}
44+
45+
@Test
46+
public void testMergedService() {
47+
Service service1 = AnnotatedElementUtils.getMergedAnnotation(MergedAnnotationTest.DemoServiceImpl1.class, Service.class);
48+
Assert.assertEquals("dubbo", service1.group());
49+
Assert.assertEquals("1.0.0", service1.version());
50+
51+
Service service2 = AnnotatedElementUtils.getMergedAnnotation(MergedAnnotationTest.DemoServiceImpl2.class, Service.class);
52+
Assert.assertEquals("group", service2.group());
53+
Assert.assertEquals("2.0", service2.version());
54+
}
55+
56+
@MergedService
57+
public static class DemoServiceImpl1 {
58+
}
59+
60+
@MergedService(group = "group", version = "2.0")
61+
public static class DemoServiceImpl2 {
62+
}
63+
64+
private static class TestBean1 {
65+
@MergedReference
66+
private DemoService demoService;
67+
}
68+
69+
private static class TestBean2 {
70+
@MergedReference(group = "group", version = "2.0")
71+
private DemoService demoService;
72+
}
73+
74+
}

0 commit comments

Comments
 (0)