Browse Source

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.
pull/347/head
Oliver Gierke 9 years ago
parent
commit
24a39e44ea
  1. 13
      src/main/java/org/springframework/data/repository/config/RepositoryBeanNameGenerator.java
  2. 36
      src/test/java/org/springframework/data/repository/config/RepositoryBeanNameGeneratorUnitTests.java

13
src/main/java/org/springframework/data/repository/config/RepositoryBeanNameGenerator.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -51,14 +51,17 @@ public class RepositoryBeanNameGenerator implements BeanNameGenerator, BeanClass @@ -51,14 +51,17 @@ public class RepositoryBeanNameGenerator implements BeanNameGenerator, BeanClass
*/
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
AnnotatedBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(getRepositoryInterfaceFrom(definition));
AnnotatedBeanDefinition beanDefinition = definition instanceof AnnotatedBeanDefinition //
? (AnnotatedBeanDefinition) definition //
: new AnnotatedGenericBeanDefinition(getRepositoryInterfaceFrom(definition));
return DELEGATE.generateBeanName(beanDefinition, registry);
}
/**
* Returns the type configured for the {@code repositoryInterface} property of the given bean definition. Uses a
* potential {@link Class} being configured as is or tries to load a class with the given value's {@link #toString()}
* representation.
* Returns the type configured for the {@code repositoryInterface} constructor argument of the given bean definition.
* Uses a potential {@link Class} being configured as is or tries to load a class with the given value's
* {@link #toString()} representation.
*
* @param beanDefinition
* @return

36
src/test/java/org/springframework/data/repository/config/RepositoryBeanNameGeneratorUnitTests.java

@ -18,15 +18,22 @@ package org.springframework.data.repository.config; @@ -18,15 +18,22 @@ package org.springframework.data.repository.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import javax.inject.Named;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
/**
@ -36,6 +43,8 @@ import org.springframework.data.repository.core.support.RepositoryFactoryBeanSup @@ -36,6 +43,8 @@ import org.springframework.data.repository.core.support.RepositoryFactoryBeanSup
*/
public class RepositoryBeanNameGeneratorUnitTests {
static final String SAMPLE_IMPLEMENTATION_BEAN_NAME = "repositoryBeanNameGeneratorUnitTests.SomeImplementation";
BeanNameGenerator generator;
BeanDefinitionRegistry registry;
@ -59,6 +68,25 @@ public class RepositoryBeanNameGeneratorUnitTests { @@ -59,6 +68,25 @@ public class RepositoryBeanNameGeneratorUnitTests {
assertThat(generator.generateBeanName(getBeanDefinitionFor(AnnotatedInterface.class), registry), is("specialName"));
}
@Test // DATACMNS-1115
public void usesClassNameOfScannedBeanDefinition() throws IOException {
MetadataReaderFactory factory = new SimpleMetadataReaderFactory();
MetadataReader reader = factory.getMetadataReader(SomeImplementation.class.getName());
BeanDefinition definition = new ScannedGenericBeanDefinition(reader);
assertThat(generator.generateBeanName(definition, registry), is(SAMPLE_IMPLEMENTATION_BEAN_NAME));
}
@Test // DATACMNS-1115
public void usesClassNameOfAnnotatedBeanDefinition() {
BeanDefinition definition = new AnnotatedGenericBeanDefinition(SomeImplementation.class);
assertThat(generator.generateBeanName(definition, registry), is(SAMPLE_IMPLEMENTATION_BEAN_NAME));
}
private BeanDefinition getBeanDefinitionFor(Class<?> repositoryInterface) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(RepositoryFactoryBeanSupport.class);
@ -66,12 +94,10 @@ public class RepositoryBeanNameGeneratorUnitTests { @@ -66,12 +94,10 @@ public class RepositoryBeanNameGeneratorUnitTests {
return builder.getBeanDefinition();
}
interface PlainInterface {
}
interface PlainInterface {}
@Named("specialName")
interface AnnotatedInterface {
interface AnnotatedInterface {}
}
class SomeImplementation {}
}

Loading…
Cancel
Save