Skip to content

Commit db0d780

Browse files
committed
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
1 parent f05d088 commit db0d780

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ private void processImport(ConfigurationClass configClass, Collection<?> classes
379379
// the candidate class is an ImportSelector -> delegate to it to determine imports
380380
Class<?> candidateClass = (candidate instanceof Class ? (Class) candidate : this.resourceLoader.getClassLoader().loadClass((String) candidate));
381381
ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);
382+
invokeAwareMethods(selector);
382383
processImport(configClass, Arrays.asList(selector.selectImports(importingClassMetadata)), false);
383384
}
384385
else if (checkAssignability(ImportBeanDefinitionRegistrar.class, candidateToCheck)) {
@@ -416,21 +417,21 @@ private boolean checkAssignability(Class<?> clazz, Object candidate) throws IOEx
416417

417418
/**
418419
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
419-
* {@link BeanFactoryAware} contracts if implemented by the given {@code registrar}.
420+
* {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.
420421
*/
421-
private void invokeAwareMethods(ImportBeanDefinitionRegistrar registrar) {
422-
if (registrar instanceof Aware) {
423-
if (registrar instanceof ResourceLoaderAware) {
424-
((ResourceLoaderAware) registrar).setResourceLoader(this.resourceLoader);
422+
private void invokeAwareMethods(Object importStrategyBean) {
423+
if (importStrategyBean instanceof Aware) {
424+
if (importStrategyBean instanceof ResourceLoaderAware) {
425+
((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader);
425426
}
426-
if (registrar instanceof BeanClassLoaderAware) {
427+
if (importStrategyBean instanceof BeanClassLoaderAware) {
427428
ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ?
428429
((ConfigurableBeanFactory) this.registry).getBeanClassLoader() :
429430
this.resourceLoader.getClassLoader());
430-
((BeanClassLoaderAware) registrar).setBeanClassLoader(classLoader);
431+
((BeanClassLoaderAware) importStrategyBean).setBeanClassLoader(classLoader);
431432
}
432-
if (registrar instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
433-
((BeanFactoryAware) registrar).setBeanFactory((BeanFactory) this.registry);
433+
if (importStrategyBean instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
434+
((BeanFactoryAware) importStrategyBean).setBeanFactory((BeanFactory) this.registry);
434435
}
435436
}
436437
}

spring-context/src/main/java/org/springframework/context/annotation/ImportSelector.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,15 @@
2323
* class(es) should be imported based on a given selection criteria, usually one or more
2424
* annotation attributes.
2525
*
26+
* <p>An {@link ImportSelector} may implement any of the following
27+
* {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective
28+
* methods will be called prior to {@link #selectImports}:
29+
* <ul>
30+
* <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li>
31+
* <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li>
32+
* <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li>
33+
* </ul>
34+
*
2635
* @author Chris Beams
2736
* @since 3.1
2837
* @see Import

0 commit comments

Comments
 (0)