Browse Source
@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. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4259 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/merge
5 changed files with 256 additions and 4 deletions
@ -0,0 +1,38 @@ |
|||||||
|
/* |
||||||
|
* 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 org.springframework.beans.factory.Aware; |
||||||
|
import org.springframework.core.type.AnnotationMetadata; |
||||||
|
|
||||||
|
/** |
||||||
|
* Interface to be implemented by any @{@link Configuration} class that wishes |
||||||
|
* to be injected with the {@link AnnotationMetadata} of the @{@code Configuration} |
||||||
|
* class that imported it. Useful in conjunction with annotations that |
||||||
|
* use @{@link Import} as a meta-annotation. |
||||||
|
* |
||||||
|
* @author Chris Beams |
||||||
|
* @since 3.1 |
||||||
|
*/ |
||||||
|
public interface ImportAware extends Aware { |
||||||
|
|
||||||
|
/** |
||||||
|
* Set the annotation metadata of the importing @{@code Configuration} class. |
||||||
|
*/ |
||||||
|
void setImportMetadata(AnnotationMetadata importMetadata); |
||||||
|
|
||||||
|
} |
||||||
@ -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()"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue