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);
}
}
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);
}
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);
}
}
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);
}
Aggregations