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

@@ -48,6 +48,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
private static final String NAMED_QUERIES_LOCATION = "namedQueriesLocation";
private static final String QUERY_LOOKUP_STRATEGY = "queryLookupStrategy";
private static final String REPOSITORY_FACTORY_BEAN_CLASS = "repositoryFactoryBeanClass";
private static final String CONSIDER_NESTED_REPOSITORIES = "considerNestedRepositories";
private final AnnotationMetadata metadata;
private final AnnotationAttributes attributes;
@@ -222,4 +223,12 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
}
return typeFilters;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationSourceSupport#isConsideringNestedRepositoriesEnabled()
*/
@Override
public boolean isConsideringNestedRepositoriesEnabled() {
return attributes.getBoolean(CONSIDER_NESTED_REPOSITORIES);
}
}

View File

@@ -1,3 +1,18 @@
/*
* 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.
* 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;
import java.io.IOException;
@@ -26,6 +41,8 @@ import org.springframework.util.Assert;
*/
class RepositoryComponentProvider extends ClassPathScanningCandidateComponentProvider {
private boolean considerNestedRepositoryInterfaces;
/**
* Creates a new {@link RepositoryComponentProvider} using the given {@link TypeFilter} to include components to be
* picked up.
@@ -83,8 +100,26 @@ class RepositoryComponentProvider extends ClassPathScanningCandidateComponentPro
boolean isNonRepositoryInterface = !ClassUtils.isGenericRepositoryInterface(beanDefinition.getBeanClassName());
boolean isTopLevelType = !beanDefinition.getMetadata().hasEnclosingClass();
boolean isConsiderNestedRepositories = isConsiderNestedRepositoryInterfaces();
return isNonRepositoryInterface && isTopLevelType;
return isNonRepositoryInterface && (isTopLevelType || isConsiderNestedRepositories);
}
/**
* @return the considerNestedRepositoryInterfaces
*/
public boolean isConsiderNestedRepositoryInterfaces() {
return considerNestedRepositoryInterfaces;
}
/**
* Controls whether nested inner-class {@link Repository} interface definitions should be considered for automatic
* discovery. This defaults to {@literal false}.
*
* @param considerNestedRepositoryInterfaces
*/
public void setConsiderNestedRepositoryInterfaces(boolean considerNestedRepositoryInterfaces) {
this.considerNestedRepositoryInterfaces = considerNestedRepositoryInterfaces;
}
/**

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.
@@ -25,6 +25,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
* Interface containing the configurable options for the Spring Data repository subsystem.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public interface RepositoryConfigurationSource {
@@ -76,4 +77,9 @@ public interface RepositoryConfigurationSource {
* @return
*/
Collection<String> getCandidates(ResourceLoader loader);
/**
* @return true if the container should look for nested repository interface definitions.
*/
boolean isConsideringNestedRepositoriesEnabled();
}

View File

@@ -21,7 +21,6 @@ import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ResourceLoader;
@@ -54,7 +53,8 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
*/
public Collection<String> getCandidates(ResourceLoader loader) {
ClassPathScanningCandidateComponentProvider scanner = new RepositoryComponentProvider(getIncludeFilters());
RepositoryComponentProvider scanner = new RepositoryComponentProvider(getIncludeFilters());
scanner.setConsiderNestedRepositoryInterfaces(isConsideringNestedRepositoriesEnabled());
scanner.setResourceLoader(loader);
scanner.setEnvironment(environment);
@@ -93,4 +93,10 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
protected Iterable<TypeFilter> getIncludeFilters() {
return Collections.emptySet();
}
/**
* Controls whether nested repository-interfaces (e.g. defined as inner classes) should be considered by the
* repository infrastructure.
*/
public abstract boolean isConsideringNestedRepositoriesEnabled();
}

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.
@@ -31,6 +31,7 @@ import org.w3c.dom.Element;
* XML based {@link RepositoryConfigurationSource}. Uses configuration defined on {@link Element} attributes.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSourceSupport {
@@ -39,6 +40,7 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
private static final String NAMED_QUERIES_LOCATION = "named-queries-location";
private static final String REPOSITORY_IMPL_POSTFIX = "repository-impl-postfix";
private static final String REPOSITORY_FACTORY_BEAN_CLASS_NAME = "factory-class";
private static final String CONSIDER_NESTED_REPOSITORIES = "consider-nested-repositories";
private final Element element;
private final ParserContext context;
@@ -148,4 +150,14 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
String attribute = element.getAttribute(attributeName);
return StringUtils.hasText(attribute) ? attribute : null;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationSourceSupport#isConsideringNestedRepositoriesEnabled()
*/
@Override
public boolean isConsideringNestedRepositoriesEnabled() {
String attribute = getNullDefaultedAttribute(element, CONSIDER_NESTED_REPOSITORIES);
return attribute != null && Boolean.parseBoolean(attribute);
}
}