Search in sources :

Example 86 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes 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 87 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project dubbo by alibaba.

the class ServiceClassPostProcessor method registerServiceBean.

/**
 * Registers {@link ServiceBean} from new annotated {@link DubboService} {@link BeanDefinition}
 *
 * @param beanDefinitionHolder
 * @param registry
 * @param scanner
 * @see ServiceBean
 * @see BeanDefinition
 */
private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, BeanDefinitionRegistry registry, DubboClassPathBeanDefinitionScanner scanner) {
    Class<?> beanClass = resolveClass(beanDefinitionHolder);
    Annotation service = findServiceAnnotation(beanClass);
    /**
     * The {@link AnnotationAttributes} of @DubboService annotation
     */
    AnnotationAttributes serviceAnnotationAttributes = getAnnotationAttributes(service, false, false);
    Class<?> interfaceClass = resolveServiceInterfaceClass(serviceAnnotationAttributes, beanClass);
    String annotatedServiceBeanName = beanDefinitionHolder.getBeanName();
    AbstractBeanDefinition serviceBeanDefinition = buildServiceBeanDefinition(service, serviceAnnotationAttributes, interfaceClass, annotatedServiceBeanName);
    // ServiceBean Bean name
    String beanName = generateServiceBeanName(serviceAnnotationAttributes, interfaceClass);
    if (scanner.checkCandidate(beanName, serviceBeanDefinition)) {
        // check duplicated candidate bean
        registry.registerBeanDefinition(beanName, serviceBeanDefinition);
        if (!serviceBeanDefinition.getPropertyValues().contains("id")) {
            serviceBeanDefinition.getPropertyValues().addPropertyValue("id", beanName);
        }
        if (logger.isInfoEnabled()) {
            logger.info("The BeanDefinition[" + serviceBeanDefinition + "] of ServiceBean has been registered with name : " + beanName);
        }
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("The Duplicated BeanDefinition[" + serviceBeanDefinition + "] of ServiceBean[ bean name : " + beanName + "] was be found , Did @DubboComponentScan scan to same package in many times?");
        }
    }
}
Also used : AnnotationUtils.getAnnotationAttributes(org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes) AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Annotation(java.lang.annotation.Annotation) AnnotatedElementUtils.findMergedAnnotation(org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation)

Example 88 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project dubbo by alibaba.

the class DubboComponentScanRegistrar method getPackagesToScan.

private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
    AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(DubboComponentScan.class.getName()));
    String[] basePackages = attributes.getStringArray("basePackages");
    Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
    String[] value = attributes.getStringArray("value");
    // Appends value array attributes
    Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value));
    packagesToScan.addAll(Arrays.asList(basePackages));
    for (Class<?> basePackageClass : basePackageClasses) {
        packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
    }
    if (packagesToScan.isEmpty()) {
        return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName()));
    }
    return packagesToScan;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes)

Example 89 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project dubbo by alibaba.

the class ReferenceBeanBuilderTest method testBuild.

@Test
public void testBuild() throws Exception {
    DubboReference reference = findAnnotation(findField(getClass(), "TEST_FIELD"), DubboReference.class);
    AnnotationAttributes attributes = AnnotationUtils.getAnnotationAttributes(reference, false, false);
    ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder.create(attributes, context);
    beanBuilder.interfaceClass(CharSequence.class);
    ReferenceBean referenceBean = beanBuilder.build();
    Assertions.assertEquals(CharSequence.class, referenceBean.getInterfaceClass());
    Assertions.assertEquals("1.0.0", referenceBean.getVersion());
    Assertions.assertEquals("TEST_GROUP", referenceBean.getGroup());
    Assertions.assertEquals("dubbo://localhost:12345", referenceBean.getUrl());
    Assertions.assertEquals("client", referenceBean.getClient());
    Assertions.assertEquals(true, referenceBean.isGeneric());
    Assertions.assertTrue(referenceBean.isInjvm());
    Assertions.assertEquals(false, referenceBean.isCheck());
    Assertions.assertFalse(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("java.lang.CharSequence", referenceBean.getStub());
    Assertions.assertEquals("failover", referenceBean.getCluster());
    Assertions.assertEquals(Integer.valueOf(3), 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(3), referenceBean.getActives());
    Assertions.assertEquals(true, referenceBean.getSent());
    Assertions.assertEquals("mock", referenceBean.getMock());
    Assertions.assertEquals("validation", referenceBean.getValidation());
    Assertions.assertEquals(Integer.valueOf(3), referenceBean.getTimeout());
    Assertions.assertEquals("cache", referenceBean.getCache());
    Assertions.assertEquals("echo,generic,accesslog", referenceBean.getFilter());
    Assertions.assertEquals("deprecated", referenceBean.getListener());
    Assertions.assertEquals("reference", referenceBean.getId());
    Assertions.assertEquals(ofSet("service1", "service2", "service3"), referenceBean.getSubscribedServices());
    Assertions.assertEquals("service1,service2,service3", referenceBean.getProvidedBy());
    Assertions.assertEquals("fastjson", referenceBean.getSerialization());
    // parameters
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("n1", "v1");
    parameters.put("n2", "v2");
    parameters.put("n3", "v3");
    Assertions.assertEquals(parameters, referenceBean.getParameters());
    // Asserts Null fields
    Assertions.assertThrows(IllegalStateException.class, () -> referenceBean.getApplication());
    Assertions.assertNull(referenceBean.getModule());
    Assertions.assertNull(referenceBean.getConsumer());
    Assertions.assertNull(referenceBean.getMonitor());
    Assertions.assertEquals(Collections.emptyList(), referenceBean.getRegistries());
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) DubboReference(org.apache.dubbo.config.annotation.DubboReference) HashMap(java.util.HashMap) ReferenceBean(org.apache.dubbo.config.spring.ReferenceBean) Test(org.junit.jupiter.api.Test)

Aggregations

AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)89 Test (org.junit.jupiter.api.Test)17 ArrayList (java.util.ArrayList)8 LinkedHashSet (java.util.LinkedHashSet)7 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)7 Annotation (java.lang.annotation.Annotation)6 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)3 ConditionOutcome (org.springframework.boot.autoconfigure.condition.ConditionOutcome)3 StandardAnnotationMetadata (org.springframework.core.type.StandardAnnotationMetadata)3 DubboReference (org.apache.dubbo.config.annotation.DubboReference)2 ReferenceBean (org.apache.dubbo.config.spring.ReferenceBean)2 Test (org.junit.Test)2 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)2 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)2 DiscoveredOperationMethod (org.springframework.boot.actuate.endpoint.annotation.DiscoveredOperationMethod)2 ContextConfiguration (org.springframework.test.context.ContextConfiguration)2