Browse Source

DATACMNS-1172 - Limit repository custom implementation scanning to repository interface packages.

Custom repository implementation scan uses the repository interface package and its subpackages and no longer scans all configured base packages. Scan for fragment implementations defaults to the fragment interface package. Using the interface package for scanning aligns the behavior with the documentation.

Declaring the implementation along with the interface in the same package is an established design pattern allowing to limit the scanning scope. A limited scope improves scanning performance as it can skip elements on the classpath, that do not provide that particular package.

Previously, we scanned for implementations using the configured base packages that were also used to discover repository interfaces. These base packages can be broader for applications that spread repository interfaces across multiple packages.

Original pull request: #248.
pull/347/head
Mark Paluch 8 years ago committed by Oliver Gierke
parent
commit
da0d964d2e
  1. 3
      src/main/java/org/springframework/data/repository/config/CustomRepositoryImplementationDetector.java
  2. 12
      src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java
  3. 9
      src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java
  4. 39
      src/test/java/org/springframework/data/repository/config/DefaultRepositoryConfigurationUnitTests.java
  5. 19
      src/test/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupportUnitTests.java
  6. 8
      src/test/java/org/springframework/data/repository/config/SampleConfiguration.java
  7. 23
      src/test/java/org/springframework/data/repository/config/basepackage/PersonRepositoryImpl.java
  8. 24
      src/test/java/org/springframework/data/repository/config/basepackage/repo/PersonRepository.java
  9. 21
      src/test/java/org/springframework/data/repository/config/basepackage/repo/PersonRepositoryCustom.java

3
src/main/java/org/springframework/data/repository/config/CustomRepositoryImplementationDetector.java

@ -39,6 +39,7 @@ import org.springframework.util.StringUtils; @@ -39,6 +39,7 @@ import org.springframework.util.StringUtils;
* @author Oliver Gierke
* @author Mark Paluch
* @author Peter Rietzler
* @author Mark Paluch
*/
public class CustomRepositoryImplementationDetector {
@ -80,7 +81,7 @@ public class CustomRepositoryImplementationDetector { @@ -80,7 +81,7 @@ public class CustomRepositoryImplementationDetector {
// TODO 2.0: Extract into dedicated interface for custom implementation lookup configuration.
return detectCustomImplementation(configuration.getImplementationClassName(), //
configuration.getBasePackages(), //
configuration.getImplementationBasePackages(configuration.getRepositoryInterface()), //
configuration.getExcludeFilters());
}

12
src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java

@ -15,6 +15,8 @@ @@ -15,6 +15,8 @@
*/
package org.springframework.data.repository.config;
import java.util.Collections;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@ -26,6 +28,7 @@ import org.springframework.util.StringUtils; @@ -26,6 +28,7 @@ import org.springframework.util.StringUtils;
* Default implementation of {@link RepositoryConfiguration}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSource>
implements RepositoryConfiguration<T> {
@ -78,6 +81,15 @@ public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSou @@ -78,6 +81,15 @@ public class DefaultRepositoryConfiguration<T extends RepositoryConfigurationSou
return configurationSource.getBasePackages();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfiguration#getBasePackages(String)
*/
@Override
public Iterable<String> getImplementationBasePackages(String interfaceClassName) {
return Collections.singleton(ClassUtils.getPackageName(interfaceClassName));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfiguration#getRepositoryInterface()

9
src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java

@ -33,6 +33,15 @@ public interface RepositoryConfiguration<T extends RepositoryConfigurationSource @@ -33,6 +33,15 @@ public interface RepositoryConfiguration<T extends RepositoryConfigurationSource
*/
Iterable<String> getBasePackages();
/**
* Returns the base packages to scan for repository implementations.
*
* @param interfaceClassName class name of the interface.
* @return
* @since 1.13.8
*/
Iterable<String> getImplementationBasePackages(String interfaceClassName);
/**
* Returns the interface name of the repository.
*

39
src/test/java/org/springframework/data/repository/config/DefaultRepositoryConfigurationUnitTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 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 org.junit.Test; @@ -22,6 +22,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@ -29,6 +30,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key; @@ -29,6 +30,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
* Unit tests for {@link DefaultRepositoryConfiguration}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultRepositoryConfigurationUnitTests {
@ -48,4 +50,39 @@ public class DefaultRepositoryConfigurationUnitTests { @@ -48,4 +50,39 @@ public class DefaultRepositoryConfigurationUnitTests {
assertThat(configuration.getQueryLookupStrategyKey(), is((Object) Key.CREATE_IF_NOT_FOUND));
assertThat(configuration.isLazyInit(), is(false));
}
@Test // DATACMNS-1172
public void limitsImplementationBasePackages() {
Iterable<String> packages = getConfiguration(source).getImplementationBasePackages("com.acme.MyRepository");
assertThat(packages, hasItem("com.acme"));
}
@Test // DATACMNS-1172
public void limitsImplementationBasePackagesOfNestedClass() {
Iterable<String> packages = getConfiguration(source).getImplementationBasePackages(NestedInterface.class.getName());
assertThat(packages, hasItem("org.springframework.data.repository.config"));
}
private DefaultRepositoryConfiguration<RepositoryConfigurationSource> getConfiguration(
RepositoryConfigurationSource source) {
RootBeanDefinition beanDefinition = createBeanDefinition();
return new DefaultRepositoryConfiguration<RepositoryConfigurationSource>(source, beanDefinition);
}
private static RootBeanDefinition createBeanDefinition() {
RootBeanDefinition beanDefinition = new RootBeanDefinition("com.acme.MyRepository");
ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
constructorArgumentValues.addGenericArgumentValue(MyRepository.class);
beanDefinition.setConstructorArgumentValues(constructorArgumentValues);
return beanDefinition;
}
private interface NestedInterface {}
}

19
src/test/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupportUnitTests.java

@ -15,7 +15,8 @@ @@ -15,7 +15,8 @@
*/
package org.springframework.data.repository.config;
import static org.mockito.Matchers.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import java.lang.annotation.Annotation;
@ -31,12 +32,14 @@ import org.springframework.core.env.StandardEnvironment; @@ -31,12 +32,14 @@ import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.repository.config.basepackage.PersonRepositoryImpl;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
/**
* Integration test for {@link RepositoryBeanDefinitionRegistrarSupport}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class RepositoryBeanDefinitionRegistrarSupportUnitTests {
@ -66,6 +69,17 @@ public class RepositoryBeanDefinitionRegistrarSupportUnitTests { @@ -66,6 +69,17 @@ public class RepositoryBeanDefinitionRegistrarSupportUnitTests {
assertNoBeanDefinitionRegisteredFor("profileRepository");
}
@Test // DATACMNS-1172
public void shouldLimitImplementationBasePackages() {
AnnotationMetadata metadata = new StandardAnnotationMetadata(LimitsImplementationBasePackages.class, true);
registrar.registerBeanDefinitions(metadata, registry);
assertBeanDefinitionRegisteredFor("personRepository");
assertNoBeanDefinitionRegisteredFor("personRepositoryImpl");
}
/**
* @see DATACMNS-360
*/
@ -133,4 +147,7 @@ public class RepositoryBeanDefinitionRegistrarSupportUnitTests { @@ -133,4 +147,7 @@ public class RepositoryBeanDefinitionRegistrarSupportUnitTests {
return "commons";
}
}
@EnableRepositories(basePackageClasses = PersonRepositoryImpl.class)
static class LimitsImplementationBasePackages {}
}

8
src/test/java/org/springframework/data/repository/config/SampleConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012 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.
@ -17,8 +17,12 @@ package org.springframework.data.repository.config; @@ -17,8 +17,12 @@ package org.springframework.data.repository.config;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.repository.config.basepackage.repo.PersonRepository;
@EnableRepositories(excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = MyOtherRepository.class), basePackageClasses = AnnotationRepositoryConfigurationSourceUnitTests.class)
@EnableRepositories(
excludeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, value = MyOtherRepository.class),
@Filter(type = FilterType.ASSIGNABLE_TYPE, value = PersonRepository.class) },
basePackageClasses = AnnotationRepositoryConfigurationSourceUnitTests.class)
class SampleConfiguration {
}

23
src/test/java/org/springframework/data/repository/config/basepackage/PersonRepositoryImpl.java

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.config.basepackage;
import org.springframework.data.repository.config.basepackage.repo.PersonRepositoryCustom;
/**
* @author Mark Paluch
*/
public class PersonRepositoryImpl implements PersonRepositoryCustom {}

24
src/test/java/org/springframework/data/repository/config/basepackage/repo/PersonRepository.java

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.config.basepackage.repo;
import org.springframework.data.mapping.Person;
import org.springframework.data.repository.Repository;
/**
* @author Mark Paluch
*/
public interface PersonRepository extends Repository<Person, String>, PersonRepositoryCustom {}

21
src/test/java/org/springframework/data/repository/config/basepackage/repo/PersonRepositoryCustom.java

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.config.basepackage.repo;
/**
* @author Mark Paluch
*/
public interface PersonRepositoryCustom {}
Loading…
Cancel
Save