提交 db0d780d 编写于 作者: P Phillip Webb

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
上级 f05d0885
...@@ -379,6 +379,7 @@ class ConfigurationClassParser { ...@@ -379,6 +379,7 @@ class ConfigurationClassParser {
// the candidate class is an ImportSelector -> delegate to it to determine imports // 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)); Class<?> candidateClass = (candidate instanceof Class ? (Class) candidate : this.resourceLoader.getClassLoader().loadClass((String) candidate));
ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class); ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);
invokeAwareMethods(selector);
processImport(configClass, Arrays.asList(selector.selectImports(importingClassMetadata)), false); processImport(configClass, Arrays.asList(selector.selectImports(importingClassMetadata)), false);
} }
else if (checkAssignability(ImportBeanDefinitionRegistrar.class, candidateToCheck)) { else if (checkAssignability(ImportBeanDefinitionRegistrar.class, candidateToCheck)) {
...@@ -416,21 +417,21 @@ class ConfigurationClassParser { ...@@ -416,21 +417,21 @@ class ConfigurationClassParser {
/** /**
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and * 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) { private void invokeAwareMethods(Object importStrategyBean) {
if (registrar instanceof Aware) { if (importStrategyBean instanceof Aware) {
if (registrar instanceof ResourceLoaderAware) { if (importStrategyBean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) registrar).setResourceLoader(this.resourceLoader); ((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader);
} }
if (registrar instanceof BeanClassLoaderAware) { if (importStrategyBean instanceof BeanClassLoaderAware) {
ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ? ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) this.registry).getBeanClassLoader() : ((ConfigurableBeanFactory) this.registry).getBeanClassLoader() :
this.resourceLoader.getClassLoader()); this.resourceLoader.getClassLoader());
((BeanClassLoaderAware) registrar).setBeanClassLoader(classLoader); ((BeanClassLoaderAware) importStrategyBean).setBeanClassLoader(classLoader);
} }
if (registrar instanceof BeanFactoryAware && this.registry instanceof BeanFactory) { if (importStrategyBean instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
((BeanFactoryAware) registrar).setBeanFactory((BeanFactory) this.registry); ((BeanFactoryAware) importStrategyBean).setBeanFactory((BeanFactory) this.registry);
} }
} }
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -23,6 +23,15 @@ import org.springframework.core.type.AnnotationMetadata; ...@@ -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 * class(es) should be imported based on a given selection criteria, usually one or more
* annotation attributes. * 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 * @author Chris Beams
* @since 3.1 * @since 3.1
* @see Import * @see Import
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册