From 97f8a0aff65cb38c712fe4da6aa48180d6f9beae Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 18 Jun 2015 17:44:05 +0200 Subject: [PATCH] DATACMNS-717 - AnnotatedTypeScanner now uses Environment and ResourceLoader configured. We now implement EnvironmentAware and ResourceLoaderAware to respect the current environment setup when scanning for types. We use the ResourceLoader's ClassLoader if configured but indicate to fall back to the default one if none configured. --- .../data/util/AnnotatedTypeScanner.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/data/util/AnnotatedTypeScanner.java b/src/main/java/org/springframework/data/util/AnnotatedTypeScanner.java index de5f33a43..e362692f5 100644 --- a/src/main/java/org/springframework/data/util/AnnotatedTypeScanner.java +++ b/src/main/java/org/springframework/data/util/AnnotatedTypeScanner.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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,7 +22,11 @@ import java.util.Set; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; +import org.springframework.core.env.Environment; +import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.util.ClassUtils; @@ -31,11 +35,14 @@ import org.springframework.util.ClassUtils; * * @author Oliver Gierke */ -public class AnnotatedTypeScanner { +public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAware { private final Iterable> annotationTypess; private final boolean considerInterfaces; + private ResourceLoader resourceLoader; + private Environment environment; + /** * Creates a new {@link AnnotatedTypeScanner} for the given annotation types. * @@ -57,6 +64,24 @@ public class AnnotatedTypeScanner { this.considerInterfaces = considerInterfaces; } + /* + * (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; + } + public Set> findTypes(String... basePackages) { return findTypes(Arrays.asList(basePackages)); } @@ -65,6 +90,14 @@ public class AnnotatedTypeScanner { ClassPathScanningCandidateComponentProvider provider = new InterfaceAwareScanner(considerInterfaces); + if (resourceLoader != null) { + provider.setResourceLoader(resourceLoader); + } + + if (environment != null) { + provider.setEnvironment(environment); + } + for (Class annotationType : annotationTypess) { provider.addIncludeFilter(new AnnotationTypeFilter(annotationType, true, considerInterfaces)); } @@ -75,7 +108,8 @@ public class AnnotatedTypeScanner { for (BeanDefinition definition : provider.findCandidateComponents(basePackage)) { try { - types.add(ClassUtils.forName(definition.getBeanClassName(), getClass().getClassLoader())); + types.add(ClassUtils.forName(definition.getBeanClassName(), + resourceLoader == null ? null : resourceLoader.getClassLoader())); } catch (ClassNotFoundException o_O) { throw new IllegalStateException(o_O); } @@ -106,8 +140,8 @@ public class AnnotatedTypeScanner { */ @Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { - return super.isCandidateComponent(beanDefinition) || considerInterfaces - && beanDefinition.getMetadata().isInterface(); + return super.isCandidateComponent(beanDefinition) + || considerInterfaces && beanDefinition.getMetadata().isInterface(); } } }