Support *Aware ImportBeanDefinitionRegistars

Implementations of Spring's ImportBeanDefinitionRegistrar interface may
now implement any of the following *Aware interfaces and have their
respective methods called prior to #registerBeanDefinitions:

 - BeanFactoryAware
 - BeanClassLoaderAware
 - ResourceLoaderAware

Issue: SPR-9568
This commit is contained in:
Oliver Gierke
2012-11-12 15:33:46 +01:00
committed by Chris Beams
parent f5080f7d70
commit 146a66fe0b
3 changed files with 135 additions and 4 deletions

View File

@@ -16,8 +16,6 @@
package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
@@ -31,13 +29,19 @@ import java.util.Set;
import java.util.Stack;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
@@ -52,6 +56,8 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.util.StringUtils;
import static org.springframework.context.annotation.MetadataUtils.*;
/**
* Parses a {@link Configuration} class definition, populating a collection of
* {@link ConfigurationClass} objects (parsing a single Configuration class may result in
@@ -318,6 +324,7 @@ class ConfigurationClassParser {
// the candidate class is an ImportBeanDefinitionRegistrar -> delegate to it to register additional bean definitions
try {
ImportBeanDefinitionRegistrar registrar = BeanUtils.instantiateClass(Class.forName(candidate), ImportBeanDefinitionRegistrar.class);
invokeAwareMethods(registrar);
registrar.registerBeanDefinitions(importingClassMetadata, registry);
}
catch (ClassNotFoundException ex) {
@@ -334,6 +341,29 @@ class ConfigurationClassParser {
}
}
/**
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
* {@link BeanFactoryAware} contracts if implemented by the given {@code registrar}.
*/
private void invokeAwareMethods(ImportBeanDefinitionRegistrar registrar) {
if (registrar instanceof Aware) {
if (registrar instanceof ResourceLoaderAware) {
((ResourceLoaderAware) registrar).setResourceLoader(resourceLoader);
}
if (registrar instanceof BeanClassLoaderAware) {
ClassLoader classLoader =
registry instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) registry).getBeanClassLoader() :
resourceLoader.getClassLoader();
((BeanClassLoaderAware) registrar).setBeanClassLoader(classLoader);
}
if (registrar instanceof BeanFactoryAware && registry instanceof BeanFactory) {
((BeanFactoryAware) registrar).setBeanFactory((BeanFactory) registry);
}
}
}
/**
* Validate each {@link ConfigurationClass} object.
* @see ConfigurationClass#validate
@@ -356,7 +386,6 @@ class ConfigurationClassParser {
return this.importStack;
}
interface ImportRegistry {
String getImportingClassFor(String importedClass);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -29,6 +29,15 @@ import org.springframework.core.type.AnnotationMetadata;
* may be provided to the @{@link Import} annotation (or may also be returned from an
* {@code ImportSelector}).
*
* <p>An {@link ImportBeanDefinitionRegistrar} may implement any of the following
* {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective
* methods will be called prior to {@link #registerBeanDefinitions}:
* <ul>
* <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}
* <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}
* <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}
* </ul>
*
* <p>See implementations and associated unit tests for usage examples.
*
* @author Chris Beams