Support *Aware ImportSelectors
Implementations of Spring's ImportSelector interface may now implement any of the following *Aware interfaces and have their respective methods called prior to #registerBeanDefinitions: - BeanFactoryAware - BeanClassLoaderAware - ResourceLoaderAware Issue: SPR-10530
This commit is contained in:
@@ -379,6 +379,7 @@ class ConfigurationClassParser {
|
||||
// the candidate class is an ImportSelector -> delegate to it to determine imports
|
||||
Class<?> candidateClass = (candidate instanceof Class ? (Class) candidate : this.resourceLoader.getClassLoader().loadClass((String) candidate));
|
||||
ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);
|
||||
invokeAwareMethods(selector);
|
||||
processImport(configClass, Arrays.asList(selector.selectImports(importingClassMetadata)), false);
|
||||
}
|
||||
else if (checkAssignability(ImportBeanDefinitionRegistrar.class, candidateToCheck)) {
|
||||
@@ -416,21 +417,21 @@ class ConfigurationClassParser {
|
||||
|
||||
/**
|
||||
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
|
||||
* {@link BeanFactoryAware} contracts if implemented by the given {@code registrar}.
|
||||
* {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.
|
||||
*/
|
||||
private void invokeAwareMethods(ImportBeanDefinitionRegistrar registrar) {
|
||||
if (registrar instanceof Aware) {
|
||||
if (registrar instanceof ResourceLoaderAware) {
|
||||
((ResourceLoaderAware) registrar).setResourceLoader(this.resourceLoader);
|
||||
private void invokeAwareMethods(Object importStrategyBean) {
|
||||
if (importStrategyBean instanceof Aware) {
|
||||
if (importStrategyBean instanceof ResourceLoaderAware) {
|
||||
((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader);
|
||||
}
|
||||
if (registrar instanceof BeanClassLoaderAware) {
|
||||
if (importStrategyBean instanceof BeanClassLoaderAware) {
|
||||
ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ?
|
||||
((ConfigurableBeanFactory) this.registry).getBeanClassLoader() :
|
||||
this.resourceLoader.getClassLoader());
|
||||
((BeanClassLoaderAware) registrar).setBeanClassLoader(classLoader);
|
||||
((BeanClassLoaderAware) importStrategyBean).setBeanClassLoader(classLoader);
|
||||
}
|
||||
if (registrar instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
|
||||
((BeanFactoryAware) registrar).setBeanFactory((BeanFactory) this.registry);
|
||||
if (importStrategyBean instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
|
||||
((BeanFactoryAware) importStrategyBean).setBeanFactory((BeanFactory) this.registry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -23,6 +23,15 @@ import org.springframework.core.type.AnnotationMetadata;
|
||||
* class(es) should be imported based on a given selection criteria, usually one or more
|
||||
* annotation attributes.
|
||||
*
|
||||
* <p>An {@link ImportSelector} may implement any of the following
|
||||
* {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective
|
||||
* methods will be called prior to {@link #selectImports}:
|
||||
* <ul>
|
||||
* <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li>
|
||||
* <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li>
|
||||
* <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
* @see Import
|
||||
|
||||
Reference in New Issue
Block a user