DATACMNS-1115 - Improve RepositoryBeanNameGenerator to properly resolve bean names for indexed BeanDefinitions.

Previously, RepositoryBeanNameGenerator applied the custom bean name lookup if the BeanDefinition given was not a ScannedGenericBeanDefinition. That in turn had been the case for custom implementation classes that were obtained through classpath scanning. With Spring 5 an index file can be used by the scanner, which in turn will cause AnnotatedGenericBeanDefinition instances being returned. That caused the code path to lookup the first constructor argument to kick in (usually used to obtain the repository interface from repository factory beans) and cause a NullPointerException.

We now forward AnnotatedBeanDefinitions as is and only apply the custom lookup for everything else, i.e. the bean definitions used for the factories.
This commit is contained in:
Oliver Gierke
2017-07-19 01:28:58 +02:00
parent d5728b6e3f
commit d692702663
2 changed files with 35 additions and 22 deletions

View File

@@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefiniti
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.util.ClassUtils;
/**
@@ -49,7 +48,9 @@ public class RepositoryBeanNameGenerator {
*/
public String generateBeanName(BeanDefinition definition) {
AnnotatedBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(getRepositoryInterfaceFrom(definition));
AnnotatedBeanDefinition beanDefinition = definition instanceof AnnotatedBeanDefinition
? (AnnotatedBeanDefinition) definition
: new AnnotatedGenericBeanDefinition(getRepositoryInterfaceFrom(definition));
return DELEGATE.generateBeanName(beanDefinition, null);
}
@@ -62,18 +63,7 @@ public class RepositoryBeanNameGenerator {
* @return
*/
private Class<?> getRepositoryInterfaceFrom(BeanDefinition beanDefinition) {
if (beanDefinition instanceof ScannedGenericBeanDefinition) {
try {
return ((ScannedGenericBeanDefinition) beanDefinition).resolveBeanClass(beanClassLoader);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Could not resolve bean class.", e);
}
} else {
return getRepositoryInterfaceFromFactory(beanDefinition);
}
return getRepositoryInterfaceFromFactory(beanDefinition);
}
private Class<?> getRepositoryInterfaceFromFactory(BeanDefinition beanDefinition) {