DATACMNS-90 - Allow Repository namespace handler to be configured to scan for inner class repositories.

We now optionally consider nested repository interfaces defined as inner-classes. This can be configured by setting the considerNestedRepositories property on EnableXXXRepository annotations or the consider-nested-repositories attribute to true - it defaults to false.

Original pull request: #50.
This commit is contained in:
Thomas Darimont
2013-10-15 16:35:10 +02:00
committed by Oliver Gierke
parent e76f820469
commit 60534974bf
11 changed files with 376 additions and 14 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.core.type.StandardAnnotationMetadata;
* Unit tests for {@link AnnotationRepositoryConfigurationSource}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class AnnotationRepositoryConfigurationSourceUnitTests {
@@ -70,6 +71,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
Iterable<String> packages = source.getBasePackages();
assertThat(packages, hasItem(DefaultConfiguration.class.getPackage().getName()));
assertThat(source.isConsideringNestedRepositoriesEnabled(), is(false));
}
@Test
@@ -83,17 +85,27 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
assertThat(packages, hasItem("foo"));
}
public static class Person {
/**
* @see DATACMNS-90
*/
@Test
public void returnsConsiderNestedRepositories() {
AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfigurationWithNestedRepositories.class);
RepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata,
EnableRepositories.class, environment);
assertThat(source.isConsideringNestedRepositoriesEnabled(), is(true));
}
public static class Person {}
@EnableRepositories
static class DefaultConfiguration {
}
static class DefaultConfiguration {}
@EnableRepositories(basePackages = "foo")
static class DefaultConfigurationWithBasePackage {
static class DefaultConfigurationWithBasePackage {}
}
@EnableRepositories(considerNestedRepositories = true)
static class DefaultConfigurationWithNestedRepositories {}
}

View File

@@ -44,4 +44,6 @@ public @interface EnableRepositories {
String namedQueriesLocation() default "";
String repositoryImplementationPostfix() default "Impl";
boolean considerNestedRepositories() default false;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 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.
@@ -23,16 +23,20 @@ import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSourceUnitTests.Person;
import org.springframework.data.repository.sample.SampleAnnotatedRepository;
/**
* Unit tests for {@link RepositoryComponentProvider}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class RepositoryComponentProviderUnitTests {
@@ -57,4 +61,23 @@ public class RepositoryComponentProviderUnitTests {
assertThat(components.size(), is(1));
assertThat(components.iterator().next().getBeanClassName(), is(MyOtherRepository.class.getName()));
}
/**
* @DATACMNS-90
*/
@Test
public void shouldConsiderNestedRepositoryInterfacesIfEnabled() {
RepositoryComponentProvider provider = new RepositoryComponentProvider(Collections.<TypeFilter> emptyList());
provider.setConsiderNestedRepositoryInterfaces(true);
Set<BeanDefinition> components = provider.findCandidateComponents("org.springframework.data.repository.config");
String nestedRepositoryClassName = "org.springframework.data.repository.config.RepositoryComponentProviderUnitTests$MyNestedRepository";
assertThat(components.size(), is(3));
assertThat(components,
Matchers.<BeanDefinition> hasItem(hasProperty("beanClassName", is(nestedRepositoryClassName))));
}
public interface MyNestedRepository extends Repository<Person, Long> {}
}

View File

@@ -33,7 +33,7 @@ import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.test.util.ReflectionTestUtils;
/**
* Integratin tests for the initializer namespace elements.
* Integration tests for the initializer name-space elements.
*
* @author Oliver Gierke
*/