Search in sources :

Example 1 with Reference

use of org.apache.dubbo.config.annotation.Reference in project dubbo by alibaba.

the class ServiceBeanNameBuilderTest method testReferenceAnnotation.

@Test
public void testReferenceAnnotation() {
    Reference reference = AnnotationUtils.getAnnotation(ReflectionUtils.findField(ServiceBeanNameBuilderTest.class, "INTERFACE_CLASS"), Reference.class);
    ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, INTERFACE_CLASS, environment);
    Assertions.assertEquals("ServiceBean:org.apache.dubbo.config.spring.api.DemoService:1.0.0:DUBBO", builder.build());
}
Also used : Reference(org.apache.dubbo.config.annotation.Reference) Test(org.junit.jupiter.api.Test)

Example 2 with Reference

use of org.apache.dubbo.config.annotation.Reference in project dubbo by alibaba.

the class MergedAnnotationTest method testMergedReference.

@Test
public void testMergedReference() {
    Field field = ReflectionUtils.findField(TestBean1.class, "demoService");
    Reference reference = AnnotatedElementUtils.getMergedAnnotation(field, Reference.class);
    Assertions.assertEquals("dubbo", reference.group());
    Assertions.assertEquals("1.0.0", reference.version());
    Field field2 = ReflectionUtils.findField(TestBean2.class, "demoService");
    Reference reference2 = AnnotatedElementUtils.getMergedAnnotation(field2, Reference.class);
    Assertions.assertEquals("group", reference2.group());
    Assertions.assertEquals("2.0", reference2.version());
}
Also used : Field(java.lang.reflect.Field) MergedReference(org.apache.dubbo.config.spring.annotation.merged.MergedReference) Reference(org.apache.dubbo.config.annotation.Reference) Test(org.junit.jupiter.api.Test)

Example 3 with Reference

use of org.apache.dubbo.config.annotation.Reference in project dubbo by alibaba.

the class ReferenceConfigTest method testConstructWithReferenceAnnotation.

@Test
public void testConstructWithReferenceAnnotation() throws NoSuchFieldException {
    Reference reference = getClass().getDeclaredField("innerTest").getAnnotation(Reference.class);
    ReferenceConfig referenceConfig = new ReferenceConfig(reference);
    Assertions.assertEquals(1, referenceConfig.getMethods().size());
    Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getName(), "sayHello");
    Assertions.assertEquals(1300, (int) ((MethodConfig) referenceConfig.getMethods().get(0)).getTimeout());
    Assertions.assertEquals(4, (int) ((MethodConfig) referenceConfig.getMethods().get(0)).getRetries());
    Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getLoadbalance(), "random");
    Assertions.assertEquals(3, (int) ((MethodConfig) referenceConfig.getMethods().get(0)).getActives());
    Assertions.assertEquals(5, (int) ((MethodConfig) referenceConfig.getMethods().get(0)).getExecutes());
    Assertions.assertTrue(((MethodConfig) referenceConfig.getMethods().get(0)).isAsync());
    Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOninvokeMethod(), "i");
    Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnreturnMethod(), "r");
    Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnthrowMethod(), "t");
    Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getCache(), "c");
}
Also used : Reference(org.apache.dubbo.config.annotation.Reference) Test(org.junit.jupiter.api.Test)

Example 4 with Reference

use of org.apache.dubbo.config.annotation.Reference in project dubbo by alibaba.

the class ReferenceAnnotationBeanPostProcessor method doGetInjectedBean.

@Override
protected Object doGetInjectedBean(AnnotationAttributes attributes, Object bean, String beanName, Class<?> injectedType, InjectionMetadata.InjectedElement injectedElement) throws Exception {
    /**
     * The name of bean that annotated Dubbo's {@link Service @Service} in local Spring {@link ApplicationContext}
     */
    String referencedBeanName = buildReferencedBeanName(attributes, injectedType);
    /**
     * The name of bean that is declared by {@link Reference @Reference} annotation injection
     */
    String referenceBeanName = getReferenceBeanName(attributes, injectedType);
    referencedBeanNameIdx.computeIfAbsent(referencedBeanName, k -> new TreeSet<String>()).add(referenceBeanName);
    ReferenceBean referenceBean = buildReferenceBeanIfAbsent(referenceBeanName, attributes, injectedType);
    boolean localServiceBean = isLocalServiceBean(referencedBeanName, referenceBean, attributes);
    prepareReferenceBean(referencedBeanName, referenceBean, localServiceBean);
    registerReferenceBean(referencedBeanName, referenceBean, localServiceBean, referenceBeanName);
    cacheInjectedReferenceBean(referenceBean, injectedElement);
    return getBeanFactory().applyBeanPostProcessorsAfterInitialization(referenceBean.get(), referenceBeanName);
}
Also used : LoggerFactory(org.apache.dubbo.common.logger.LoggerFactory) Arrays(java.util.Arrays) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) HashMap(java.util.HashMap) ReferenceBean(org.apache.dubbo.config.spring.ReferenceBean) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) AbstractAnnotationBeanPostProcessor(com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor) Map(java.util.Map) Service(org.apache.dubbo.config.annotation.Service) AnnotationUtils.getAttribute(com.alibaba.spring.util.AnnotationUtils.getAttribute) Method(java.lang.reflect.Method) Logger(org.apache.dubbo.common.logger.Logger) InjectionMetadata(org.springframework.beans.factory.annotation.InjectionMetadata) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ObjectUtils(org.springframework.util.ObjectUtils) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Reference(org.apache.dubbo.config.annotation.Reference) BeansException(org.springframework.beans.BeansException) ApplicationListener(org.springframework.context.ApplicationListener) Field(java.lang.reflect.Field) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) AnnotationUtils(com.alibaba.spring.util.AnnotationUtils) ApplicationEvent(org.springframework.context.ApplicationEvent) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) StringUtils.hasText(org.springframework.util.StringUtils.hasText) ServiceBean(org.apache.dubbo.config.spring.ServiceBean) ServiceBeanNameBuilder.create(org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.create) DubboService(org.apache.dubbo.config.annotation.DubboService) Annotation(java.lang.annotation.Annotation) DubboReference(org.apache.dubbo.config.annotation.DubboReference) AnnotationUtils.getAttributes(com.alibaba.spring.util.AnnotationUtils.getAttributes) AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) Collections(java.util.Collections) ApplicationContextAware(org.springframework.context.ApplicationContextAware) TreeSet(java.util.TreeSet) ReferenceBean(org.apache.dubbo.config.spring.ReferenceBean)

Example 5 with Reference

use of org.apache.dubbo.config.annotation.Reference in project dubbo by alibaba.

the class AnnotationPropertyValuesAdapterTest method test.

@Test
public void test() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("version", "1.0.0");
    mockEnvironment.setProperty("url", "   dubbo://localhost:12345");
    Field field = ReflectionUtils.findField(TestBean.class, "demoService");
    Reference reference = AnnotationUtils.getAnnotation(field, Reference.class);
    AnnotationPropertyValuesAdapter propertyValues = new AnnotationPropertyValuesAdapter(reference, mockEnvironment);
    ReferenceBean referenceBean = new ReferenceBean();
    DataBinder dataBinder = new DataBinder(referenceBean);
    dataBinder.setDisallowedFields("application", "module", "consumer", "monitor", "registry");
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<String[], String>() {

        @Override
        public String convert(String[] source) {
            return arrayToCommaDelimitedString(source);
        }
    });
    conversionService.addConverter(new Converter<String[], Map<String, String>>() {

        @Override
        public Map<String, String> convert(String[] source) {
            return CollectionUtils.toStringMap(source);
        }
    });
    dataBinder.setConversionService(conversionService);
    dataBinder.bind(propertyValues);
    // System.out.println(referenceBean);
    Assertions.assertEquals(DemoService.class, referenceBean.getInterfaceClass());
    Assertions.assertEquals("org.apache.dubbo.config.spring.api.DemoService", referenceBean.getInterface());
    Assertions.assertEquals("1.0.0", referenceBean.getVersion());
    Assertions.assertEquals("group", referenceBean.getGroup());
    Assertions.assertEquals("dubbo://localhost:12345", referenceBean.getUrl());
    Assertions.assertEquals("client", referenceBean.getClient());
    Assertions.assertEquals(true, referenceBean.isGeneric());
    Assertions.assertNull(referenceBean.isInjvm());
    Assertions.assertEquals(false, referenceBean.isCheck());
    Assertions.assertEquals(true, referenceBean.isInit());
    Assertions.assertEquals(true, referenceBean.getLazy());
    Assertions.assertEquals(true, referenceBean.getStubevent());
    Assertions.assertEquals("reconnect", referenceBean.getReconnect());
    Assertions.assertEquals(true, referenceBean.getSticky());
    Assertions.assertEquals("javassist", referenceBean.getProxy());
    Assertions.assertEquals("stub", referenceBean.getStub());
    Assertions.assertEquals("failover", referenceBean.getCluster());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getConnections());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getCallbacks());
    Assertions.assertEquals("onconnect", referenceBean.getOnconnect());
    Assertions.assertEquals("ondisconnect", referenceBean.getOndisconnect());
    Assertions.assertEquals("owner", referenceBean.getOwner());
    Assertions.assertEquals("layer", referenceBean.getLayer());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getRetries());
    Assertions.assertEquals("random", referenceBean.getLoadbalance());
    Assertions.assertEquals(true, referenceBean.isAsync());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getActives());
    Assertions.assertEquals(true, referenceBean.getSent());
    Assertions.assertEquals("mock", referenceBean.getMock());
    Assertions.assertEquals("validation", referenceBean.getValidation());
    Assertions.assertEquals(Integer.valueOf(2), referenceBean.getTimeout());
    Assertions.assertEquals("cache", referenceBean.getCache());
    Assertions.assertEquals("default,default", referenceBean.getFilter());
    Assertions.assertEquals("default,default", referenceBean.getListener());
    Map<String, String> data = new LinkedHashMap<String, String>();
    data.put("key1", "value1");
    Assertions.assertEquals(data, referenceBean.getParameters());
    // Bean compare
    Assertions.assertNull(referenceBean.getRegistry());
}
Also used : Reference(org.apache.dubbo.config.annotation.Reference) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) StringUtils.arrayToCommaDelimitedString(org.springframework.util.StringUtils.arrayToCommaDelimitedString) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) MockEnvironment(org.springframework.mock.env.MockEnvironment) ReferenceBean(org.apache.dubbo.config.spring.ReferenceBean) DataBinder(org.springframework.validation.DataBinder) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

Reference (org.apache.dubbo.config.annotation.Reference)5 Test (org.junit.jupiter.api.Test)4 Field (java.lang.reflect.Field)3 Map (java.util.Map)2 ReferenceBean (org.apache.dubbo.config.spring.ReferenceBean)2 AbstractAnnotationBeanPostProcessor (com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor)1 AnnotationUtils (com.alibaba.spring.util.AnnotationUtils)1 AnnotationUtils.getAttribute (com.alibaba.spring.util.AnnotationUtils.getAttribute)1 AnnotationUtils.getAttributes (com.alibaba.spring.util.AnnotationUtils.getAttributes)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeSet (java.util.TreeSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1