Introduce ImportAware interface

@Configuration classes may implement ImportAware in order to be injected
with the AnnotationMetadata of their @Import'ing class.

Includes the introduction of a new PriorityOrdered
ImportAwareBeanPostProcessor that handles injection of the
importing class metadata.
This commit is contained in:
Chris Beams
2011-05-06 19:05:42 +00:00
parent 89005a5b70
commit cdb01cbd37
5 changed files with 256 additions and 4 deletions

View File

@@ -0,0 +1,137 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
/**
* Tests that an ImportAware @Configuration classes gets injected with the
* annotation metadata of the @Configuration class that imported it.
*
* @author Chris Beams
* @since 3.1
*/
public class ImportAwareTests {
@Test
public void directlyAnnotatedWithImport() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ImportingConfig.class);
ctx.refresh();
ctx.getBean("importedConfigBean");
ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
assertThat("import metadata was not injected", importMetadata, notNullValue());
assertThat(importMetadata.getClassName(), is(ImportingConfig.class.getName()));
Map<String, Object> importAttribs = importMetadata.getAnnotationAttributes(Import.class.getName());
Class<?>[] importedClasses = (Class<?>[])importAttribs.get("value");
assertThat(importedClasses[0].getName(), is(ImportedConfig.class.getName()));
}
@Test
public void indirectlyAnnotatedWithImport() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(IndirectlyImportingConfig.class);
ctx.refresh();
ctx.getBean("importedConfigBean");
ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
assertThat("import metadata was not injected", importMetadata, notNullValue());
assertThat(importMetadata.getClassName(), is(IndirectlyImportingConfig.class.getName()));
Map<String, Object> enableAttribs = importMetadata.getAnnotationAttributes(EnableImportedConfig.class.getName());
String foo = (String)enableAttribs.get("foo");
assertThat(foo, is("xyz"));
}
@Configuration
@Import(ImportedConfig.class)
static class ImportingConfig {
}
@Configuration
@EnableImportedConfig(foo="xyz")
static class IndirectlyImportingConfig {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ImportedConfig.class)
public @interface EnableImportedConfig {
String foo() default "";
}
@Configuration
static class ImportedConfig implements ImportAware {
AnnotationMetadata importMetadata;
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.importMetadata = importMetadata;
}
@Bean
public BPP importedConfigBean() {
return new BPP();
}
@Bean
public AsyncAnnotationBeanPostProcessor asyncBPP() {
return new AsyncAnnotationBeanPostProcessor();
}
}
static class BPP implements BeanFactoryAware, BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
return bean;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("ImportAwareTests.BPP.setBeanFactory()");
}
}
}

View File

@@ -193,8 +193,8 @@ public class ScopingTests {
@Test
public void testScopedConfigurationBeanDefinitionCount() throws Exception {
// count the beans
// 6 @Beans + 1 Configuration + 2 scoped proxy
assertEquals(9, ctx.getBeanDefinitionCount());
// 6 @Beans + 1 Configuration + 2 scoped proxy + 1 importRegistry
assertEquals(10, ctx.getBeanDefinitionCount());
}
// /**