diff --git a/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java b/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java index d1d8b2e83..411a9dbbf 100644 --- a/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java +++ b/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java @@ -95,7 +95,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura // Default configuration - return package of annotated class if (value.length == 0 && basePackages.length == 0 && basePackageClasses.length == 0) { String className = metadata.getClassName(); - return Collections.singleton(className.substring(0, className.lastIndexOf('.'))); + return Collections.singleton(ClassUtils.getPackageName(className)); } Set packages = new HashSet(); diff --git a/src/test/java/TypeInDefaultPackage.java b/src/test/java/TypeInDefaultPackage.java new file mode 100644 index 000000000..46e7d8c4a --- /dev/null +++ b/src/test/java/TypeInDefaultPackage.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 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. + */ +import org.springframework.data.repository.config.EnableRepositories; + +/** + * Dummy type to be able to reference the default package. + * + * @author Oliver Gierke + */ +@EnableRepositories +public class TypeInDefaultPackage { + +} diff --git a/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java b/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java index e10fead16..c4d2a3508 100644 --- a/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java +++ b/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java @@ -120,6 +120,20 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { assertThat(source.getAttribute("namedQueriesLocation"), is("bar")); } + /** + * @see DATACMNS-502 + */ + @Test + public void returnsEmptyStringForBasePackage() throws Exception { + + StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(getClass().getClassLoader().loadClass( + "TypeInDefaultPackage")); + RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata, + EnableRepositories.class, resourceLoader, environment); + + assertThat(configurationSource.getBasePackages(), hasItem("")); + } + private AnnotationRepositoryConfigurationSource getConfigSource(Class type) { AnnotationMetadata metadata = new StandardAnnotationMetadata(type);