Search in sources :

Example 1 with Service

use of com.alibaba.dubbo.config.annotation.Service in project dubbo by alibaba.

the class AnnotationBean method postProcessAfterInitialization.

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (!isMatchPackage(bean)) {
        return bean;
    }
    Service service = bean.getClass().getAnnotation(Service.class);
    if (service != null) {
        ServiceBean<Object> serviceConfig = new ServiceBean<Object>(service);
        serviceConfig.setRef(bean);
        if (void.class.equals(service.interfaceClass()) && "".equals(service.interfaceName())) {
            if (bean.getClass().getInterfaces().length > 0) {
                serviceConfig.setInterface(bean.getClass().getInterfaces()[0]);
            } else {
                throw new IllegalStateException("Failed to export remote service class " + bean.getClass().getName() + ", cause: The @Service undefined interfaceClass or interfaceName, and the service class unimplemented any interfaces.");
            }
        }
        if (applicationContext != null) {
            serviceConfig.setApplicationContext(applicationContext);
            if (service.registry() != null && service.registry().length > 0) {
                List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
                for (String registryId : service.registry()) {
                    if (registryId != null && registryId.length() > 0) {
                        registryConfigs.add((RegistryConfig) applicationContext.getBean(registryId, RegistryConfig.class));
                    }
                }
                serviceConfig.setRegistries(registryConfigs);
            }
            if (service.provider() != null && service.provider().length() > 0) {
                serviceConfig.setProvider((ProviderConfig) applicationContext.getBean(service.provider(), ProviderConfig.class));
            }
            if (service.monitor() != null && service.monitor().length() > 0) {
                serviceConfig.setMonitor((MonitorConfig) applicationContext.getBean(service.monitor(), MonitorConfig.class));
            }
            if (service.application() != null && service.application().length() > 0) {
                serviceConfig.setApplication((ApplicationConfig) applicationContext.getBean(service.application(), ApplicationConfig.class));
            }
            if (service.module() != null && service.module().length() > 0) {
                serviceConfig.setModule((ModuleConfig) applicationContext.getBean(service.module(), ModuleConfig.class));
            }
            if (service.provider() != null && service.provider().length() > 0) {
                serviceConfig.setProvider((ProviderConfig) applicationContext.getBean(service.provider(), ProviderConfig.class));
            } else {
            }
            if (service.protocol() != null && service.protocol().length > 0) {
                List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
                for (String protocolId : service.protocol()) {
                    if (protocolId != null && protocolId.length() > 0) {
                        protocolConfigs.add((ProtocolConfig) applicationContext.getBean(protocolId, ProtocolConfig.class));
                    }
                }
                serviceConfig.setProtocols(protocolConfigs);
            }
            try {
                serviceConfig.afterPropertiesSet();
            } catch (RuntimeException e) {
                throw (RuntimeException) e;
            } catch (Exception e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
        serviceConfigs.add(serviceConfig);
        serviceConfig.export();
    }
    return bean;
}
Also used : ArrayList(java.util.ArrayList) Service(com.alibaba.dubbo.config.annotation.Service) BeansException(org.springframework.beans.BeansException)

Example 2 with Service

use of com.alibaba.dubbo.config.annotation.Service in project dubbo by alibaba.

the class ServiceAnnotationBeanPostProcessor method registerServiceBean.

/**
 * Registers {@link ServiceBean} from new annotated {@link Service} {@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);
    Service service = findAnnotation(beanClass, Service.class);
    Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);
    String annotatedServiceBeanName = beanDefinitionHolder.getBeanName();
    AbstractBeanDefinition serviceBeanDefinition = buildServiceBeanDefinition(service, interfaceClass, annotatedServiceBeanName);
    // ServiceBean Bean name
    String beanName = generateServiceBeanName(interfaceClass, annotatedServiceBeanName);
    if (scanner.checkCandidate(beanName, serviceBeanDefinition)) {
        // check duplicated candidate bean
        registry.registerBeanDefinition(beanName, serviceBeanDefinition);
        if (logger.isInfoEnabled()) {
            logger.warn("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 : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Service(com.alibaba.dubbo.config.annotation.Service)

Example 3 with Service

use of com.alibaba.dubbo.config.annotation.Service in project dubbo by alibaba.

the class ServiceAnnotationBeanPostProcessor method registerServiceBeans.

/**
 * Registers Beans whose classes was annotated {@link Service}
 *
 * @param packagesToScan The base packages to scan
 * @param registry       {@link BeanDefinitionRegistry}
 */
private void registerServiceBeans(Set<String> packagesToScan, BeanDefinitionRegistry registry) {
    DubboClassPathBeanDefinitionScanner scanner = new DubboClassPathBeanDefinitionScanner(registry, environment, resourceLoader);
    BeanNameGenerator beanNameGenerator = resolveBeanNameGenerator(registry);
    scanner.setBeanNameGenerator(beanNameGenerator);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));
    for (String packageToScan : packagesToScan) {
        // Registers @Service Bean first
        scanner.scan(packageToScan);
        // Finds all BeanDefinitionHolders of @Service whether @ComponentScan scans or not.
        Set<BeanDefinitionHolder> beanDefinitionHolders = findServiceBeanDefinitionHolders(scanner, packageToScan, registry, beanNameGenerator);
        if (!CollectionUtils.isEmpty(beanDefinitionHolders)) {
            for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
                registerServiceBean(beanDefinitionHolder, registry, scanner);
            }
            if (logger.isInfoEnabled()) {
                logger.info(beanDefinitionHolders.size() + " annotated Dubbo's @Service Components { " + beanDefinitionHolders + " } were scanned under package[" + packageToScan + "]");
            }
        } else {
            if (logger.isWarnEnabled()) {
                logger.warn("No Spring Bean annotating Dubbo's @Service was found under package[" + packageToScan + "]");
            }
        }
    }
}
Also used : AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Service(com.alibaba.dubbo.config.annotation.Service) DubboClassPathBeanDefinitionScanner(com.alibaba.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner)

Aggregations

Service (com.alibaba.dubbo.config.annotation.Service)3 DubboClassPathBeanDefinitionScanner (com.alibaba.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner)1 ArrayList (java.util.ArrayList)1 BeansException (org.springframework.beans.BeansException)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)1 AnnotationBeanNameGenerator (org.springframework.context.annotation.AnnotationBeanNameGenerator)1 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)1