DATACMNS-1098 - RepositoryComponentScanner now exposes BeanDefinitionRegistry.

We now override ClassPathScanningCandidateComponentProvider's getRegistry() to make sure custom conditions on repository candidates can use the currently available BeanDefinitionRegistry in their implementations. To achieve that we forward the BeanDefinitionRegistry at hand through the configuration infrastructure (both XML and annotation side of things).
This commit is contained in:
Oliver Gierke
2017-06-26 16:01:55 +02:00
parent 6cd5f7c052
commit fce40e981c
8 changed files with 65 additions and 21 deletions

View File

@@ -25,6 +25,7 @@ import java.util.Set;
import java.util.regex.Pattern;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
@@ -76,9 +77,9 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
* @param environment
*/
public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation,
ResourceLoader resourceLoader, Environment environment) {
ResourceLoader resourceLoader, Environment environment, BeanDefinitionRegistry registry) {
super(environment, resourceLoader.getClassLoader());
super(environment, resourceLoader.getClassLoader(), registry);
Assert.notNull(metadata, "Metadata must not be null!");
Assert.notNull(annotation, "Annotation must not be null!");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 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.
@@ -32,8 +32,8 @@ import org.springframework.util.Assert;
*
* @author Oliver Gierke
*/
public abstract class RepositoryBeanDefinitionRegistrarSupport implements ImportBeanDefinitionRegistrar,
ResourceLoaderAware, EnvironmentAware {
public abstract class RepositoryBeanDefinitionRegistrarSupport
implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {
private ResourceLoader resourceLoader;
private Environment environment;
@@ -72,7 +72,7 @@ public abstract class RepositoryBeanDefinitionRegistrarSupport implements Import
}
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
annotationMetadata, getAnnotation(), resourceLoader, environment);
annotationMetadata, getAnnotation(), resourceLoader, environment, registry);
RepositoryConfigurationExtension extension = getExtension();
RepositoryConfigurationUtils.exposeRegistration(extension, registry, configurationSource);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 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.
@@ -22,6 +22,7 @@ import java.util.Set;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.classreading.MetadataReader;
@@ -47,6 +48,7 @@ class RepositoryComponentProvider extends ClassPathScanningCandidateComponentPro
private static final String METHOD_NOT_PUBLIC = "AnnotationConfigUtils.processCommonDefinitionAnnotations(…) is not public! Make sure you're using Spring 3.2.5 or better. The class was loaded from %s.";
private boolean considerNestedRepositoryInterfaces;
private BeanDefinitionRegistry registry;
/**
* Creates a new {@link RepositoryComponentProvider} using the given {@link TypeFilter} to include components to be
@@ -55,13 +57,16 @@ class RepositoryComponentProvider extends ClassPathScanningCandidateComponentPro
* @param includeFilters the {@link TypeFilter}s to select repository interfaces to consider, must not be
* {@literal null}.
*/
public RepositoryComponentProvider(Iterable<? extends TypeFilter> includeFilters) {
public RepositoryComponentProvider(Iterable<? extends TypeFilter> includeFilters, BeanDefinitionRegistry registry) {
super(false);
assertRequiredSpringVersionPresent();
Assert.notNull(includeFilters, "Include filters must not be null!");
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
this.registry = registry;
if (includeFilters.iterator().hasNext()) {
for (TypeFilter filter : includeFilters) {
@@ -129,6 +134,15 @@ class RepositoryComponentProvider extends ClassPathScanningCandidateComponentPro
return candidates;
}
/*
* (non-Javadoc)
* @see org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#getRegistry()
*/
@Override
protected BeanDefinitionRegistry getRegistry() {
return registry;
}
/**
* @return the considerNestedRepositoryInterfaces
*/

View File

@@ -18,6 +18,7 @@ package org.springframework.data.repository.config;
import java.util.Collections;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.filter.TypeFilter;
@@ -38,6 +39,7 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
private final Environment environment;
private final RepositoryBeanNameGenerator beanNameGenerator;
private final BeanDefinitionRegistry registry;
/**
* Creates a new {@link RepositoryConfigurationSourceSupport} with the given environment.
@@ -45,13 +47,16 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
* @param environment must not be {@literal null}.
* @param classLoader must not be {@literal null}.
*/
public RepositoryConfigurationSourceSupport(Environment environment, ClassLoader classLoader) {
public RepositoryConfigurationSourceSupport(Environment environment, ClassLoader classLoader,
BeanDefinitionRegistry registry) {
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.registry = registry;
}
/*
@@ -61,10 +66,10 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
@Override
public Streamable<BeanDefinition> getCandidates(ResourceLoader loader) {
RepositoryComponentProvider scanner = new RepositoryComponentProvider(getIncludeFilters());
RepositoryComponentProvider scanner = new RepositoryComponentProvider(getIncludeFilters(), registry);
scanner.setConsiderNestedRepositoryInterfaces(shouldConsiderNestedRepositories());
scanner.setResourceLoader(loader);
scanner.setEnvironment(environment);
scanner.setResourceLoader(loader);
getExcludeFilters().forEach(it -> scanner.addExcludeFilter(it));

View File

@@ -64,7 +64,7 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
*/
public XmlRepositoryConfigurationSource(Element element, ParserContext context, Environment environment) {
super(environment, context.getReaderContext().getResourceLoader().getClassLoader());
super(environment, context.getReaderContext().getResourceLoader().getClassLoader(), context.getRegistry());
Assert.notNull(element, "Element must not be null!");
Assert.notNull(context, "Context must not be null!");