DATACMNS-1497 - Configuration infrastructure now considers scanning BeanNameGenerator.

We now implement the newly introduced ImportBeanDefinitionRegistrar overload additional provided with a BeanNameGenerator configured for the import scanning. We forward this into our bean name generation infrastructure as fallback.

Related tickets: spring-projects/spring-framework#22591
This commit is contained in:
Oliver Drotbohm
2019-03-14 21:59:48 +01:00
parent c4a51f1c24
commit 753d8bbb33
8 changed files with 102 additions and 23 deletions

View File

@@ -29,6 +29,9 @@ import javax.annotation.Nonnull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
@@ -42,6 +45,7 @@ import org.springframework.core.type.filter.RegexPatternTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.config.ConfigurationUtils;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@@ -82,11 +86,31 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
* @param resourceLoader must not be {@literal null}.
* @param environment must not be {@literal null}.
* @param registry must not be {@literal null}.
* @deprecated since 2.2. Prefer to use overload taking a {@link BeanNameGenerator} additionally.
*/
@Deprecated
public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation,
ResourceLoader resourceLoader, Environment environment, BeanDefinitionRegistry registry) {
this(metadata, annotation, resourceLoader, environment, registry, null);
}
super(environment, ConfigurationUtils.getRequiredClassLoader(resourceLoader), registry);
/**
* Creates a new {@link AnnotationRepositoryConfigurationSource} from the given {@link AnnotationMetadata} and
* annotation.
*
* @param metadata must not be {@literal null}.
* @param annotation must not be {@literal null}.
* @param resourceLoader must not be {@literal null}.
* @param environment must not be {@literal null}.
* @param registry must not be {@literal null}.
* @param generator can be {@literal null}.
*/
public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation,
ResourceLoader resourceLoader, Environment environment, BeanDefinitionRegistry registry,
@Nullable BeanNameGenerator generator) {
super(environment, ConfigurationUtils.getRequiredClassLoader(resourceLoader), registry,
defaultBeanNameGenerator(generator));
Assert.notNull(metadata, "Metadata must not be null!");
Assert.notNull(annotation, "Annotation must not be null!");
@@ -397,4 +421,20 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
return new String[0];
}
}
/**
* Returns the {@link BeanNameGenerator} to use falling back to an {@link AnnotationBeanNameGenerator} if either the
* given generator is {@literal null} or it's the one locally declared in {@link ConfigurationClassPostProcessor}'s
* {@code importBeanNameGenerator}. This is to make sure we only use the given {@link BeanNameGenerator} if it was
* customized.
*
* @param generator can be {@literal null}.
* @return
*/
private static BeanNameGenerator defaultBeanNameGenerator(@Nullable BeanNameGenerator generator) {
return generator == null || ConfigurationClassPostProcessor.IMPORT_BEAN_NAME_GENERATOR.equals(generator) //
? new AnnotationBeanNameGenerator() //
: generator;
}
}

View File

@@ -21,6 +21,7 @@ import javax.annotation.Nonnull;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
@@ -60,9 +61,11 @@ public abstract class RepositoryBeanDefinitionRegistrarSupport
/*
* (non-Javadoc)
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry)
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.beans.factory.support.BeanNameGenerator)
*/
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry,
BeanNameGenerator generator) {
Assert.notNull(annotationMetadata, "AnnotationMetadata must not be null!");
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
@@ -74,7 +77,7 @@ public abstract class RepositoryBeanDefinitionRegistrarSupport
}
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
annotationMetadata, getAnnotation(), resourceLoader, environment, registry);
annotationMetadata, getAnnotation(), resourceLoader, environment, registry, generator);
RepositoryConfigurationExtension extension = getExtension();
RepositoryConfigurationUtils.exposeRegistration(extension, registry, configurationSource);

View File

@@ -15,14 +15,13 @@
*/
package org.springframework.data.repository.config;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
@@ -33,12 +32,25 @@ import org.springframework.util.ClassUtils;
* @author Oliver Gierke
* @author Jens Schauder
*/
@RequiredArgsConstructor
public class RepositoryBeanNameGenerator {
private static final SpringDataAnnotationBeanNameGenerator DELEGATE = new SpringDataAnnotationBeanNameGenerator();
class RepositoryBeanNameGenerator {
private final ClassLoader beanClassLoader;
private final SpringDataAnnotationBeanNameGenerator delegate;
/**
* Creates a new {@link RepositoryBeanNameGenerator} for the given {@link ClassLoader} and {@link BeanNameGenerator}.
*
* @param beanClassLoader must not be {@literal null}.
* @param generator must not be {@literal null}.
*/
public RepositoryBeanNameGenerator(ClassLoader beanClassLoader, BeanNameGenerator generator) {
Assert.notNull(beanClassLoader, "Bean ClassLoader must not be null!");
Assert.notNull(generator, "BeanNameGenerator must not be null!");
this.beanClassLoader = beanClassLoader;
this.delegate = new SpringDataAnnotationBeanNameGenerator(generator);
}
/**
* Generate a bean name for the given bean definition.
@@ -53,7 +65,7 @@ public class RepositoryBeanNameGenerator {
? (AnnotatedBeanDefinition) definition //
: new AnnotatedGenericBeanDefinition(getRepositoryInterfaceFrom(definition));
return DELEGATE.generateBeanName(beanDefinition);
return delegate.generateBeanName(beanDefinition);
}
/**

View File

@@ -22,6 +22,7 @@ import java.util.Collections;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
@@ -53,14 +54,14 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
* @param registry must not be {@literal null}.
*/
public RepositoryConfigurationSourceSupport(Environment environment, ClassLoader classLoader,
BeanDefinitionRegistry registry) {
BeanDefinitionRegistry registry, BeanNameGenerator generator) {
Assert.notNull(environment, "Environment must not be null!");
Assert.notNull(classLoader, "ClassLoader must not be null!");
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
this.environment = environment;
this.beanNameGenerator = new RepositoryBeanNameGenerator(classLoader);
this.beanNameGenerator = new RepositoryBeanNameGenerator(classLoader, generator);
this.registry = registry;
}

View File

@@ -15,8 +15,12 @@
*/
package org.springframework.data.repository.config;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
/**
@@ -29,9 +33,10 @@ import org.springframework.context.annotation.AnnotationBeanNameGenerator;
* @since 2.0
* @soundtrack Nils Wülker - Never Left At All (feat. Rob Summerfield)
*/
public class SpringDataAnnotationBeanNameGenerator {
@RequiredArgsConstructor
class SpringDataAnnotationBeanNameGenerator {
private final AnnotationBeanNameGenerator delegate = new AnnotationBeanNameGenerator();
private final @NonNull BeanNameGenerator delegate;
/**
* Generates a bean name for the given {@link BeanDefinition}.

View File

@@ -68,7 +68,8 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
*/
public XmlRepositoryConfigurationSource(Element element, ParserContext context, Environment environment) {
super(environment, ConfigurationUtils.getRequiredClassLoader(context.getReaderContext()), context.getRegistry());
super(environment, ConfigurationUtils.getRequiredClassLoader(context.getReaderContext()), context.getRegistry(),
context.getReaderContext().getReader().getBeanNameGenerator());
Assert.notNull(element, "Element must not be null!");