Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring package scan compatibility, support both alibaba Service and apache Service. #4375

Merged
merged 6 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ private static void deserializeInternal(Object result, JavaBeanDescriptor beanDe
for (Map.Entry<Object, Object> entry : beanDescriptor) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key != null && key instanceof JavaBeanDescriptor) {
if (key instanceof JavaBeanDescriptor) {
JavaBeanDescriptor keyDescriptor = (JavaBeanDescriptor) entry.getKey();
key = instantiateForDeserialize(keyDescriptor, loader, cache);
deserializeInternal(key, keyDescriptor, loader, cache);
}
if (value != null && value instanceof JavaBeanDescriptor) {
if (value instanceof JavaBeanDescriptor) {
JavaBeanDescriptor valueDescriptor = (JavaBeanDescriptor) entry.getValue();
value = instantiateForDeserialize(valueDescriptor, loader, cache);
deserializeInternal(value, valueDescriptor, loader, cache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object get(int index) {
*/
public boolean getBoolean(int index, boolean def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Boolean ? ((Boolean) tmp).booleanValue() : def;
return tmp instanceof Boolean ? ((Boolean) tmp).booleanValue() : def;
}

/**
Expand All @@ -59,7 +59,7 @@ public boolean getBoolean(int index, boolean def) {
*/
public int getInt(int index, int def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).intValue() : def;
return tmp instanceof Number ? ((Number) tmp).intValue() : def;
}

/**
Expand All @@ -71,7 +71,7 @@ public int getInt(int index, int def) {
*/
public long getLong(int index, long def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).longValue() : def;
return tmp instanceof Number ? ((Number) tmp).longValue() : def;
}

/**
Expand All @@ -83,7 +83,7 @@ public long getLong(int index, long def) {
*/
public float getFloat(int index, float def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).floatValue() : def;
return tmp instanceof Number ? ((Number) tmp).floatValue() : def;
}

/**
Expand All @@ -95,7 +95,7 @@ public float getFloat(int index, float def) {
*/
public double getDouble(int index, double def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
return tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object get(String key) {
*/
public boolean getBoolean(String key, boolean def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Boolean ? (Boolean) tmp : def;
return tmp instanceof Boolean ? (Boolean) tmp : def;
}

/**
Expand All @@ -59,7 +59,7 @@ public boolean getBoolean(String key, boolean def) {
*/
public int getInt(String key, int def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).intValue() : def;
return tmp instanceof Number ? ((Number) tmp).intValue() : def;
}

/**
Expand All @@ -71,7 +71,7 @@ public int getInt(String key, int def) {
*/
public long getLong(String key, long def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).longValue() : def;
return tmp instanceof Number ? ((Number) tmp).longValue() : def;
}

/**
Expand All @@ -83,7 +83,7 @@ public long getLong(String key, long def) {
*/
public float getFloat(String key, float def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).floatValue() : def;
return tmp instanceof Number ? ((Number) tmp).floatValue() : def;
}

/**
Expand All @@ -95,7 +95,7 @@ public float getFloat(String key, float def) {
*/
public double getDouble(String key, double def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
return tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions dubbo-config/dubbo-config-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<skip_maven_deploy>false</skip_maven_deploy>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-compatible</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-api</artifactId>
Expand Down Expand Up @@ -128,5 +133,6 @@
<artifactId>tomcat-embed-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -61,10 +61,12 @@
import java.util.Set;

import static org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.create;
import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveServiceInterfaceClass;
import static org.apache.dubbo.config.spring.util.ObjectUtils.of;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes;
import static org.springframework.util.ClassUtils.resolveClassName;

/**
Expand Down Expand Up @@ -132,6 +134,14 @@ private void registerServiceBeans(Set<String> packagesToScan, BeanDefinitionRegi

scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));

/**
* Add the compatibility for legacy Dubbo's @Service
*
* The issue : https://github.com/apache/dubbo/issues/4330
* @since 2.7.3
*/
scanner.addIncludeFilter(new AnnotationTypeFilter(com.alibaba.dubbo.config.annotation.Service.class));

for (String packageToScan : packagesToScan) {

// Registers @Service Bean first
Expand Down Expand Up @@ -250,17 +260,22 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean

Class<?> beanClass = resolveClass(beanDefinitionHolder);

Service service = findAnnotation(beanClass, Service.class);
Annotation service = findServiceAnnotation(beanClass);

Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);
/**
* The {@link AnnotationAttributes} of @Service annotation
*/
AnnotationAttributes serviceAnnotationAttributes = getAnnotationAttributes(service, false, false);

Class<?> interfaceClass = resolveServiceInterfaceClass(serviceAnnotationAttributes, beanClass);

String annotatedServiceBeanName = beanDefinitionHolder.getBeanName();

AbstractBeanDefinition serviceBeanDefinition =
buildServiceBeanDefinition(service, interfaceClass, annotatedServiceBeanName);
buildServiceBeanDefinition(service, serviceAnnotationAttributes, interfaceClass, annotatedServiceBeanName);

// ServiceBean Bean name
String beanName = generateServiceBeanName(service, interfaceClass);
String beanName = generateServiceBeanName(serviceAnnotationAttributes, interfaceClass);

if (scanner.checkCandidate(beanName, serviceBeanDefinition)) { // check duplicated candidate bean
registry.registerBeanDefinition(beanName, serviceBeanDefinition);
Expand All @@ -282,58 +297,37 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean

}


/**
* Find the {@link Annotation annotation} of @Service
*
* @param beanClass the {@link Class class} of Bean
* @return <code>null</code> if not found
* @since 2.7.3
*/
private Annotation findServiceAnnotation(Class<?> beanClass) {
Annotation service = findAnnotation(beanClass, Service.class);
if (service == null) {
service = findAnnotation(beanClass, com.alibaba.dubbo.config.annotation.Service.class);
}
return service;
}

/**
* Generates the bean name of {@link ServiceBean}
*
* @param service
* @param interfaceClass the class of interface annotated {@link Service}
* @param serviceAnnotationAttributes
* @param interfaceClass the class of interface annotated {@link Service}
* @return ServiceBean@interfaceClassName#annotatedServiceBeanName
* @since 2.5.9
* @since 2.7.3
*/
private String generateServiceBeanName(Service service, Class<?> interfaceClass) {
ServiceBeanNameBuilder builder = create(service, interfaceClass, environment);

private String generateServiceBeanName(AnnotationAttributes serviceAnnotationAttributes, Class<?> interfaceClass) {
ServiceBeanNameBuilder builder = create(interfaceClass, environment)
.group(serviceAnnotationAttributes.getString("group"))
.version(serviceAnnotationAttributes.getString("version"));
return builder.build();
}

private Class<?> resolveServiceInterfaceClass(Class<?> annotatedServiceBeanClass, Service service) {

Class<?> interfaceClass = service.interfaceClass();

if (void.class.equals(interfaceClass)) {

interfaceClass = null;

String interfaceClassName = service.interfaceName();

if (StringUtils.hasText(interfaceClassName)) {
if (ClassUtils.isPresent(interfaceClassName, classLoader)) {
interfaceClass = resolveClassName(interfaceClassName, classLoader);
}
}

}

if (interfaceClass == null) {
// Find all interfaces from the annotated class
// To resolve an issue : https://github.com/apache/dubbo/issues/3251
Class<?>[] allInterfaces = ClassUtils.getAllInterfacesForClass(annotatedServiceBeanClass);

if (allInterfaces.length > 0) {
interfaceClass = allInterfaces[0];
}

}

Assert.notNull(interfaceClass,
"@Service interfaceClass() or interfaceName() or interface class must be present!");

Assert.isTrue(interfaceClass.isInterface(),
"The type that was annotated @Service is not an interface!");

return interfaceClass;
}

private Class<?> resolveClass(BeanDefinitionHolder beanDefinitionHolder) {

BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
Expand Down Expand Up @@ -361,7 +355,19 @@ private Set<String> resolvePackagesToScan(Set<String> packagesToScan) {
return resolvedPackagesToScan;
}

private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class<?> interfaceClass,
/**
* Build the {@link AbstractBeanDefinition Bean Definition}
*
* @param serviceAnnotation
* @param serviceAnnotationAttributes
* @param interfaceClass
* @param annotatedServiceBeanName
* @return
* @since 2.7.3
*/
private AbstractBeanDefinition buildServiceBeanDefinition(Annotation serviceAnnotation,
AnnotationAttributes serviceAnnotationAttributes,
Class<?> interfaceClass,
String annotatedServiceBeanName) {

BeanDefinitionBuilder builder = rootBeanDefinition(ServiceBean.class);
Expand All @@ -373,43 +379,43 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class
String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol",
"interface", "interfaceName", "parameters");

propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(service, environment, ignoreAttributeNames));
propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(serviceAnnotation, environment, ignoreAttributeNames));

// References "ref" property to annotated-@Service Bean
addPropertyReference(builder, "ref", annotatedServiceBeanName);
// Set interface
builder.addPropertyValue("interface", interfaceClass.getName());
// Convert parameters into map
builder.addPropertyValue("parameters", convertParameters(service.parameters()));
builder.addPropertyValue("parameters", convertParameters(serviceAnnotationAttributes.getStringArray("parameters")));

/**
* Add {@link org.apache.dubbo.config.ProviderConfig} Bean reference
*/
String providerConfigBeanName = service.provider();
String providerConfigBeanName = serviceAnnotationAttributes.getString("provider");
if (StringUtils.hasText(providerConfigBeanName)) {
addPropertyReference(builder, "provider", providerConfigBeanName);
}

/**
* Add {@link org.apache.dubbo.config.MonitorConfig} Bean reference
*/
String monitorConfigBeanName = service.monitor();
String monitorConfigBeanName = serviceAnnotationAttributes.getString("monitor");
if (StringUtils.hasText(monitorConfigBeanName)) {
addPropertyReference(builder, "monitor", monitorConfigBeanName);
}

/**
* Add {@link org.apache.dubbo.config.ApplicationConfig} Bean reference
*/
String applicationConfigBeanName = service.application();
String applicationConfigBeanName = serviceAnnotationAttributes.getString("application");
if (StringUtils.hasText(applicationConfigBeanName)) {
addPropertyReference(builder, "application", applicationConfigBeanName);
}

/**
* Add {@link org.apache.dubbo.config.ModuleConfig} Bean reference
*/
String moduleConfigBeanName = service.module();
String moduleConfigBeanName = serviceAnnotationAttributes.getString("module");
if (StringUtils.hasText(moduleConfigBeanName)) {
addPropertyReference(builder, "module", moduleConfigBeanName);
}
Expand All @@ -418,7 +424,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class
/**
* Add {@link org.apache.dubbo.config.RegistryConfig} Bean reference
*/
String[] registryConfigBeanNames = service.registry();
String[] registryConfigBeanNames = serviceAnnotationAttributes.getStringArray("registry");

List<RuntimeBeanReference> registryRuntimeBeanReferences = toRuntimeBeanReferences(registryConfigBeanNames);

Expand All @@ -429,7 +435,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class
/**
* Add {@link org.apache.dubbo.config.ProtocolConfig} Bean reference
*/
String[] protocolConfigBeanNames = service.protocol();
String[] protocolConfigBeanNames = serviceAnnotationAttributes.getStringArray("protocol");

List<RuntimeBeanReference> protocolRuntimeBeanReferences = toRuntimeBeanReferences(protocolConfigBeanNames);

Expand Down
Loading