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 a411bd3931
commit 8bddd62c7a
8 changed files with 70 additions and 22 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;
@@ -74,9 +75,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);
super(environment, 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

@@ -21,6 +21,7 @@ import java.util.HashSet;
import java.util.Set;
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,16 +39,20 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
protected static final String DEFAULT_REPOSITORY_IMPL_POSTFIX = "Impl";
private final Environment environment;
private final BeanDefinitionRegistry registry;
/**
* Creates a new {@link RepositoryConfigurationSourceSupport} with the given environment.
*
* @param environment must not be {@literal null}.
*/
public RepositoryConfigurationSourceSupport(Environment environment) {
public RepositoryConfigurationSourceSupport(Environment environment, BeanDefinitionRegistry registry) {
Assert.notNull(environment, "Environment must not be null!");
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
this.environment = environment;
this.registry = registry;
}
/*
@@ -56,10 +61,10 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
*/
public Collection<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);
for (TypeFilter filter : getExcludeFilters()) {
scanner.addExcludeFilter(filter);

View File

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

View File

@@ -17,6 +17,7 @@ package org.springframework.data.repository.config;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -25,6 +26,7 @@ import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
@@ -45,6 +47,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
RepositoryConfigurationSource source;
Environment environment;
ResourceLoader resourceLoader;
BeanDefinitionRegistry registry;
@Before
public void setUp() {
@@ -52,8 +55,10 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
AnnotationMetadata annotationMetadata = new StandardAnnotationMetadata(SampleConfiguration.class, true);
environment = new StandardEnvironment();
resourceLoader = new DefaultResourceLoader();
registry = mock(BeanDefinitionRegistry.class);
source = new AnnotationRepositoryConfigurationSource(annotationMetadata, EnableRepositories.class, resourceLoader,
environment);
environment, registry);
}
@Test // DATACMNS-47
@@ -109,10 +114,10 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
@Test // DATACMNS-502
public void returnsEmptyStringForBasePackage() throws Exception {
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(getClass().getClassLoader().loadClass(
"TypeInDefaultPackage"), true);
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(
getClass().getClassLoader().loadClass("TypeInDefaultPackage"), true);
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
EnableRepositories.class, resourceLoader, environment);
EnableRepositories.class, resourceLoader, environment, registry);
assertThat(configurationSource.getBasePackages(), hasItem(""));
}
@@ -129,7 +134,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
AnnotationMetadata metadata = new StandardAnnotationMetadata(ConfigWithSampleAnnotation.class, true);
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
SampleAnnotation.class, resourceLoader, environment);
SampleAnnotation.class, resourceLoader, environment, registry);
assertThat(configurationSource.getRepositoryBaseClassName(), is(nullValue()));
}
@@ -137,7 +142,8 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
private AnnotationRepositoryConfigurationSource getConfigSource(Class<?> type) {
AnnotationMetadata metadata = new StandardAnnotationMetadata(type, true);
return new AnnotationRepositoryConfigurationSource(metadata, EnableRepositories.class, resourceLoader, environment);
return new AnnotationRepositoryConfigurationSource(metadata, EnableRepositories.class, resourceLoader, environment,
registry);
}
public static class Person {}

View File

@@ -17,6 +17,7 @@ package org.springframework.data.repository.config;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Collections;
@@ -28,6 +29,7 @@ import org.hamcrest.Description;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.repository.Repository;
@@ -42,10 +44,13 @@ import org.springframework.data.repository.sample.SampleAnnotatedRepository;
*/
public class RepositoryComponentProviderUnitTests {
BeanDefinitionRegistry registry = mock(BeanDefinitionRegistry.class);
@Test
public void findsAnnotatedRepositoryInterface() {
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.<TypeFilter> emptyList());
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.<TypeFilter> emptyList(),
registry);
Set<BeanDefinition> components = provider.findCandidateComponents("org.springframework.data.repository.sample");
assertThat(components.size(), is(3));
@@ -57,7 +62,7 @@ public class RepositoryComponentProviderUnitTests {
List<? extends TypeFilter> filters = Arrays.asList(new AssignableTypeFilter(MyOtherRepository.class));
RepositoryComponentProvider provider = new RepositoryComponentProvider(filters);
RepositoryComponentProvider provider = new RepositoryComponentProvider(filters, registry);
Set<BeanDefinition> components = provider.findCandidateComponents("org.springframework.data.repository");
assertThat(components.size(), is(1));
@@ -67,7 +72,8 @@ public class RepositoryComponentProviderUnitTests {
@Test // DATACMNS-90
public void shouldConsiderNestedRepositoryInterfacesIfEnabled() {
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.<TypeFilter> emptyList());
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.<TypeFilter> emptyList(),
registry);
provider.setConsiderNestedRepositoryInterfaces(true);
Set<BeanDefinition> components = provider.findCandidateComponents("org.springframework.data.repository.config");
@@ -78,6 +84,20 @@ public class RepositoryComponentProviderUnitTests {
Matchers.<BeanDefinition> hasItem(hasProperty("beanClassName", is(nestedRepositoryClassName))));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-1098
public void rejectsNullBeanDefinitionRegistry() {
new RepositoryComponentProvider(Collections.<TypeFilter> emptyList(), null);
}
@Test // DATACMNS-1098
public void exposesBeanDefinitionRegistry() {
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.<TypeFilter> emptyList(),
registry);
assertThat(provider.getRegistry(), is(registry));
}
static class BeanDefinitionOfTypeMatcher extends BaseMatcher<BeanDefinition> {
private final Class<?> expectedType;

View File

@@ -45,8 +45,10 @@ public class RepositoryConfigurationDelegateUnitTests {
StandardEnvironment environment = new StandardEnvironment();
GenericApplicationContext context = new GenericApplicationContext();
RepositoryConfigurationSource configSource = new AnnotationRepositoryConfigurationSource(
new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment);
new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment,
context.getDefaultListableBeanFactory());
RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configSource, context, environment);