DATACMNS-360 - Scanning for repository interfaces now considers active profiles.

The configuration entry points (RepositoryBeanDefinitionParser and RepositoryBeanDefinitionRegistrar) now hand the Environment they're running in forward into the infrastructure scanning for repositories. Thus it will correctly find or not find repository interfaces based on which profiles are activated or not.
This commit is contained in:
Oliver Gierke
2013-08-25 18:29:52 +02:00
parent eb4b801dc9
commit 5ccb0bc9c2
8 changed files with 140 additions and 34 deletions

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.
@@ -26,6 +26,7 @@ import java.util.Set;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
@@ -57,8 +58,12 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
*
* @param metadata must not be {@literal null}.
* @param annotation must not be {@literal null}.
* @param environment
*/
public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation) {
public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation,
Environment environment) {
super(environment);
Assert.notNull(metadata);
Assert.notNull(annotation);
@@ -196,23 +201,23 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
for (Class<?> filterClass : filterAttributes.getClassArray("value")) {
switch (filterType) {
case ANNOTATION:
Assert.isAssignable(Annotation.class, filterClass, "An error occured when processing a @ComponentScan "
+ "ANNOTATION type filter: ");
@SuppressWarnings("unchecked")
Class<Annotation> annoClass = (Class<Annotation>) filterClass;
typeFilters.add(new AnnotationTypeFilter(annoClass));
break;
case ASSIGNABLE_TYPE:
typeFilters.add(new AssignableTypeFilter(filterClass));
break;
case CUSTOM:
Assert.isAssignable(TypeFilter.class, filterClass, "An error occured when processing a @ComponentScan "
+ "CUSTOM type filter: ");
typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
break;
default:
throw new IllegalArgumentException("unknown filter type " + filterType);
case ANNOTATION:
Assert.isAssignable(Annotation.class, filterClass, "An error occured when processing a @ComponentScan "
+ "ANNOTATION type filter: ");
@SuppressWarnings("unchecked")
Class<Annotation> annoClass = (Class<Annotation>) filterClass;
typeFilters.add(new AnnotationTypeFilter(annoClass));
break;
case ASSIGNABLE_TYPE:
typeFilters.add(new AssignableTypeFilter(filterClass));
break;
case CUSTOM:
Assert.isAssignable(TypeFilter.class, filterClass, "An error occured when processing a @ComponentScan "
+ "CUSTOM type filter: ");
typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
break;
default:
throw new IllegalArgumentException("unknown filter type " + filterType);
}
}
return typeFilters;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-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.
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
@@ -62,7 +63,8 @@ public class RepositoryBeanDefinitionParser implements BeanDefinitionParser {
try {
XmlRepositoryConfigurationSource configSource = new XmlRepositoryConfigurationSource(element, parser);
Environment environment = parser.getDelegate().getEnvironment();
XmlRepositoryConfigurationSource configSource = new XmlRepositoryConfigurationSource(element, parser, environment);
for (RepositoryConfiguration<XmlRepositoryConfigurationSource> config : extension.getRepositoryConfigurations(
configSource, parser.getReaderContext().getResourceLoader())) {

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.
@@ -22,8 +22,10 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
@@ -36,15 +38,17 @@ import org.springframework.util.ClassUtils;
* @author Oliver Gierke
*/
public abstract class RepositoryBeanDefinitionRegistrarSupport implements ImportBeanDefinitionRegistrar,
BeanClassLoaderAware, ResourceLoaderAware {
BeanClassLoaderAware, ResourceLoaderAware, EnvironmentAware {
private ResourceLoader resourceLoader;
private ClassLoader beanClassLoader;
private Environment environment;
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang.ClassLoader)
*/
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@@ -53,10 +57,20 @@ public abstract class RepositoryBeanDefinitionRegistrarSupport implements Import
* (non-Javadoc)
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
*/
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
/*
* (non-Javadoc)
* @see org.springframework.context.EnvironmentAware#setEnvironment(org.springframework.core.env.Environment)
*/
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
/*
* (non-Javadoc)
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry)
@@ -74,7 +88,7 @@ public abstract class RepositoryBeanDefinitionRegistrarSupport implements Import
defaultExternalResources(registry);
AnnotationRepositoryConfigurationSource configuration = new AnnotationRepositoryConfigurationSource(
annotationMetadata, getAnnotation());
annotationMetadata, getAnnotation(), environment);
RepositoryConfigurationExtension extension = getExtension();
extension.registerBeansForRoot(registry, configuration);

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.
@@ -22,6 +22,8 @@ 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;
import org.springframework.core.type.filter.TypeFilter;
@@ -34,6 +36,18 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
protected static final String DEFAULT_REPOSITORY_IMPL_POSTFIX = "Impl";
private final Environment environment;
/**
* Creates a new {@link RepositoryConfigurationSourceSupport} with the given environment. Defaults to a plain
* {@link StandardEnvironment} in case the given argument is {@literal null}.
*
* @param environment nullable, defaults to a {@link StandardEnvironment}.
*/
public RepositoryConfigurationSourceSupport(Environment environment) {
this.environment = environment == null ? new StandardEnvironment() : environment;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfiguration#getCandidates(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)
@@ -42,6 +56,7 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
ClassPathScanningCandidateComponentProvider scanner = new RepositoryComponentProvider(getIncludeFilters());
scanner.setResourceLoader(loader);
scanner.setEnvironment(environment);
for (TypeFilter filter : getExcludeFilters()) {
scanner.addExcludeFilter(filter);

View File

@@ -18,6 +18,7 @@ package org.springframework.data.repository.config;
import java.util.Arrays;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.config.TypeFilterParser;
import org.springframework.data.config.TypeFilterParser.Type;
@@ -50,8 +51,11 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
*
* @param element must not be {@literal null}.
* @param context must not be {@literal null}.
* @param environment must not be {@literal null}.
*/
public XmlRepositoryConfigurationSource(Element element, ParserContext context) {
public XmlRepositoryConfigurationSource(Element element, ParserContext context, Environment environment) {
super(environment);
Assert.notNull(element);
Assert.notNull(context);