Search in sources :

Example 11 with SofaService

use of com.alipay.sofa.runtime.api.annotation.SofaService in project sofa-boot by alipay.

the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinition.

@SuppressWarnings("unchecked")
private void generateSofaServiceDefinition(String beanId, SofaService sofaServiceAnnotation, Class<?> beanClass, BeanDefinition beanDefinition, BeanDefinitionRegistry registry) {
    if (sofaServiceAnnotation == null) {
        return;
    }
    AnnotationWrapperBuilder<SofaService> wrapperBuilder = AnnotationWrapperBuilder.wrap(sofaServiceAnnotation).withBinder(binder);
    sofaServiceAnnotation = wrapperBuilder.build();
    Class<?> interfaceType = sofaServiceAnnotation.interfaceType();
    if (interfaceType.equals(void.class)) {
        Class<?>[] interfaces = beanClass.getInterfaces();
        if (beanClass.isInterface() || interfaces == null || interfaces.length == 0) {
            interfaceType = beanClass;
        } else if (interfaces.length == 1) {
            interfaceType = interfaces[0];
        } else {
            throw new FatalBeanException(ErrorCode.convert("01-02004", beanId));
        }
    }
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
    String serviceId = SofaBeanNameGenerator.generateSofaServiceBeanName(interfaceType, sofaServiceAnnotation.uniqueId());
    if (!registry.containsBeanDefinition(serviceId)) {
        builder.getRawBeanDefinition().setScope(beanDefinition.getScope());
        builder.setLazyInit(beanDefinition.isLazyInit());
        builder.getRawBeanDefinition().setBeanClass(ServiceFactoryBean.class);
        builder.addAutowiredProperty(AbstractContractDefinitionParser.SOFA_RUNTIME_CONTEXT);
        builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_CONVERTER_FACTORY);
        builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_ADAPTER_FACTORY);
        builder.addPropertyValue(AbstractContractDefinitionParser.INTERFACE_CLASS_PROPERTY, interfaceType);
        builder.addPropertyValue(AbstractContractDefinitionParser.UNIQUE_ID_PROPERTY, sofaServiceAnnotation.uniqueId());
        builder.addPropertyValue(AbstractContractDefinitionParser.BINDINGS, getSofaServiceBinding(sofaServiceAnnotation, sofaServiceAnnotation.bindings()));
        builder.addPropertyReference(ServiceDefinitionParser.REF, beanId);
        builder.addPropertyValue(ServiceDefinitionParser.BEAN_ID, beanId);
        builder.addPropertyValue(AbstractContractDefinitionParser.DEFINITION_BUILDING_API_TYPE, true);
        builder.addDependsOn(beanId);
        registry.registerBeanDefinition(serviceId, builder.getBeanDefinition());
    } else {
        SofaLogger.warn("SofaService was already registered: {}", serviceId);
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) FatalBeanException(org.springframework.beans.FatalBeanException) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService)

Example 12 with SofaService

use of com.alipay.sofa.runtime.api.annotation.SofaService in project sofa-boot by alipay.

the class ServiceFactoryBean method hasSofaServiceAnnotation.

private boolean hasSofaServiceAnnotation() {
    Class<?> implementationClazz = ref.getClass();
    SofaService sofaService = implementationClazz.getAnnotation(SofaService.class);
    if (sofaService == null) {
        return false;
    }
    String annotationUniqueId = sofaService.uniqueId();
    if ((uniqueId == null || uniqueId.isEmpty()) && (annotationUniqueId == null || annotationUniqueId.isEmpty())) {
        return true;
    }
    return annotationUniqueId.equals(uniqueId);
}
Also used : SofaService(com.alipay.sofa.runtime.api.annotation.SofaService)

Example 13 with SofaService

use of com.alipay.sofa.runtime.api.annotation.SofaService in project sofa-boot by sofastack.

the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinition.

@SuppressWarnings("unchecked")
private void generateSofaServiceDefinition(String beanId, SofaService sofaServiceAnnotation, Class<?> beanClass, BeanDefinition beanDefinition, BeanDefinitionRegistry registry) {
    if (sofaServiceAnnotation == null) {
        return;
    }
    AnnotationWrapperBuilder<SofaService> wrapperBuilder = AnnotationWrapperBuilder.wrap(sofaServiceAnnotation).withBinder(binder);
    sofaServiceAnnotation = wrapperBuilder.build();
    Class<?> interfaceType = sofaServiceAnnotation.interfaceType();
    if (interfaceType.equals(void.class)) {
        Class<?>[] interfaces = beanClass.getInterfaces();
        if (beanClass.isInterface() || interfaces == null || interfaces.length == 0) {
            interfaceType = beanClass;
        } else if (interfaces.length == 1) {
            interfaceType = interfaces[0];
        } else {
            throw new FatalBeanException(ErrorCode.convert("01-02004", beanId));
        }
    }
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
    String serviceId = SofaBeanNameGenerator.generateSofaServiceBeanName(interfaceType, sofaServiceAnnotation.uniqueId());
    if (!registry.containsBeanDefinition(serviceId)) {
        builder.getRawBeanDefinition().setScope(beanDefinition.getScope());
        builder.setLazyInit(beanDefinition.isLazyInit());
        builder.getRawBeanDefinition().setBeanClass(ServiceFactoryBean.class);
        builder.addAutowiredProperty(AbstractContractDefinitionParser.SOFA_RUNTIME_CONTEXT);
        builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_CONVERTER_FACTORY);
        builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_ADAPTER_FACTORY);
        builder.addPropertyValue(AbstractContractDefinitionParser.INTERFACE_CLASS_PROPERTY, interfaceType);
        builder.addPropertyValue(AbstractContractDefinitionParser.UNIQUE_ID_PROPERTY, sofaServiceAnnotation.uniqueId());
        builder.addPropertyValue(AbstractContractDefinitionParser.BINDINGS, getSofaServiceBinding(sofaServiceAnnotation, sofaServiceAnnotation.bindings()));
        builder.addPropertyReference(ServiceDefinitionParser.REF, beanId);
        builder.addPropertyValue(ServiceDefinitionParser.BEAN_ID, beanId);
        builder.addPropertyValue(AbstractContractDefinitionParser.DEFINITION_BUILDING_API_TYPE, true);
        builder.addDependsOn(beanId);
        registry.registerBeanDefinition(serviceId, builder.getBeanDefinition());
    } else {
        SofaLogger.warn("SofaService was already registered: {}", serviceId);
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) FatalBeanException(org.springframework.beans.FatalBeanException) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService)

Example 14 with SofaService

use of com.alipay.sofa.runtime.api.annotation.SofaService in project sofa-boot by sofastack.

the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinitionOnClass.

private void generateSofaServiceDefinitionOnClass(String beanId, Class<?> beanClass, BeanDefinition beanDefinition, BeanDefinitionRegistry registry) {
    // See issue: https://github.com/sofastack/sofa-boot/issues/835
    SofaService sofaServiceAnnotation = AnnotationUtils.findAnnotation(beanClass, SofaService.class);
    generateSofaServiceDefinition(beanId, sofaServiceAnnotation, beanClass, beanDefinition, registry);
}
Also used : SofaService(com.alipay.sofa.runtime.api.annotation.SofaService)

Aggregations

SofaService (com.alipay.sofa.runtime.api.annotation.SofaService)14 FatalBeanException (org.springframework.beans.FatalBeanException)6 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)6 PlaceHolderBinder (com.alipay.sofa.boot.annotation.PlaceHolderBinder)5 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)5 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)4 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)4 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)4 AnnotationWrapperBuilder (com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder)3 ErrorCode (com.alipay.sofa.boot.error.ErrorCode)3 BeanDefinitionUtil (com.alipay.sofa.boot.util.BeanDefinitionUtil)3 SofaReference (com.alipay.sofa.runtime.api.annotation.SofaReference)3 SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)3 BindingType (com.alipay.sofa.runtime.api.binding.BindingType)3 SofaLogger (com.alipay.sofa.runtime.log.SofaLogger)3 Binding (com.alipay.sofa.runtime.spi.binding.Binding)3 SofaRuntimeContext (com.alipay.sofa.runtime.spi.component.SofaRuntimeContext)3 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)3 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)3 BindingConverterFactory (com.alipay.sofa.runtime.spi.service.BindingConverterFactory)3