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:
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.repository.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -23,6 +24,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
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
|
||||
@@ -110,7 +115,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
|
||||
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()).contains("");
|
||||
}
|
||||
@@ -127,7 +132,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()).isNotPresent();
|
||||
}
|
||||
@@ -135,7 +140,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 {}
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
package org.springframework.data.repository.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -26,6 +26,7 @@ import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
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;
|
||||
@@ -40,10 +41,12 @@ import org.springframework.data.repository.sample.SampleAnnotatedRepository;
|
||||
*/
|
||||
public class RepositoryComponentProviderUnitTests {
|
||||
|
||||
BeanDefinitionRegistry registry = mock(BeanDefinitionRegistry.class);
|
||||
|
||||
@Test
|
||||
public void findsAnnotatedRepositoryInterface() {
|
||||
|
||||
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.emptyList());
|
||||
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.emptyList(), registry);
|
||||
Set<BeanDefinition> components = provider.findCandidateComponents("org.springframework.data.repository.sample");
|
||||
|
||||
assertThat(components).hasSize(3);
|
||||
@@ -56,7 +59,7 @@ public class RepositoryComponentProviderUnitTests {
|
||||
|
||||
List<? extends TypeFilter> filters = Collections.singletonList(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).hasSize(1);
|
||||
@@ -66,7 +69,7 @@ public class RepositoryComponentProviderUnitTests {
|
||||
@Test // DATACMNS-90
|
||||
public void shouldConsiderNestedRepositoryInterfacesIfEnabled() {
|
||||
|
||||
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.emptyList());
|
||||
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.emptyList(), registry);
|
||||
provider.setConsiderNestedRepositoryInterfaces(true);
|
||||
|
||||
Set<BeanDefinition> components = provider.findCandidateComponents("org.springframework.data.repository.config");
|
||||
@@ -76,6 +79,19 @@ public class RepositoryComponentProviderUnitTests {
|
||||
assertThat(components).extracting(BeanDefinition::getBeanClassName).contains(nestedRepositoryClassName);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATACMNS-1098
|
||||
public void rejectsNullBeanDefinitionRegistry() {
|
||||
new RepositoryComponentProvider(Collections.emptyList(), null);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1098
|
||||
public void exposesBeanDefinitionRegistry() {
|
||||
|
||||
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.emptyList(), registry);
|
||||
|
||||
assertThat(provider.getRegistry()).isEqualTo(registry);
|
||||
}
|
||||
|
||||
static class BeanDefinitionOfTypeMatcher extends BaseMatcher<BeanDefinition> {
|
||||
|
||||
private final Class<?> expectedType;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user