Skip to content

Commit 40458b1

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
1 parent e944d27 commit 40458b1

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
@@ -338,6 +338,7 @@ private void processImport(ConfigurationClass configClass, String[] classesToImp
338338
try {
339339
ImportSelector selector = BeanUtils.instantiateClass(
340340
this.resourceLoader.getClassLoader().loadClass(candidate), ImportSelector.class);
341+
invokeAwareMethods(selector);
341342
processImport(configClass, selector.selectImports(importingClassMetadata), false);
342343
}
343344
catch (ClassNotFoundException ex) {
@@ -369,21 +370,21 @@ else if (reader.match(ImportBeanDefinitionRegistrar.class)) {
369370

370371
/**
371372
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
372-
* {@link BeanFactoryAware} contracts if implemented by the given {@code registrar}.
373+
* {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.
373374
*/
374-
private void invokeAwareMethods(ImportBeanDefinitionRegistrar registrar) {
375-
if (registrar instanceof Aware) {
376-
if (registrar instanceof ResourceLoaderAware) {
377-
((ResourceLoaderAware) registrar).setResourceLoader(this.resourceLoader);
375+
private void invokeAwareMethods(Object importStrategyBean) {
376+
if (importStrategyBean instanceof Aware) {
377+
if (importStrategyBean instanceof ResourceLoaderAware) {
378+
((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader);
378379
}
379-
if (registrar instanceof BeanClassLoaderAware) {
380+
if (importStrategyBean instanceof BeanClassLoaderAware) {
380381
ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ?
381382
((ConfigurableBeanFactory) this.registry).getBeanClassLoader() :
382383
this.resourceLoader.getClassLoader());
383-
((BeanClassLoaderAware) registrar).setBeanClassLoader(classLoader);
384+
((BeanClassLoaderAware) importStrategyBean).setBeanClassLoader(classLoader);
384385
}
385-
if (registrar instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
386-
((BeanFactoryAware) registrar).setBeanFactory((BeanFactory) this.registry);
386+
if (importStrategyBean instanceof BeanFactoryAware && this.registry instanceof BeanFactory) {
387+
((BeanFactoryAware) importStrategyBean).setBeanFactory((BeanFactory) this.registry);
387388
}
388389
}
389390
}

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)