提交 4d338324 编写于 作者: P Phillip Webb

Support EnvironmentAware ImportSelector/Registrar

Add support for the EnvironmentAware interface with ImportSelector
and ImportBeanDefinitionRegistrar implementations.

Issue: SPR-10602
上级 d7ec20a2
...@@ -47,6 +47,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; ...@@ -47,6 +47,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.NestedIOException; import org.springframework.core.NestedIOException;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
...@@ -486,6 +487,9 @@ class ConfigurationClassParser { ...@@ -486,6 +487,9 @@ class ConfigurationClassParser {
*/ */
private void invokeAwareMethods(Object importStrategyBean) { private void invokeAwareMethods(Object importStrategyBean) {
if (importStrategyBean instanceof Aware) { if (importStrategyBean instanceof Aware) {
if (importStrategyBean instanceof EnvironmentAware) {
((EnvironmentAware) importStrategyBean).setEnvironment(this.environment);
}
if (importStrategyBean instanceof ResourceLoaderAware) { if (importStrategyBean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader); ((ResourceLoaderAware) importStrategyBean).setResourceLoader(this.resourceLoader);
} }
......
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -33,6 +33,7 @@ import org.springframework.core.type.AnnotationMetadata; ...@@ -33,6 +33,7 @@ import org.springframework.core.type.AnnotationMetadata;
* {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective * {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective
* methods will be called prior to {@link #registerBeanDefinitions}: * methods will be called prior to {@link #registerBeanDefinitions}:
* <ul> * <ul>
* <li>{@link org.springframework.context.EnvironmentAware}</li>
* <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware} * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}
* <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware} * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}
* <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware} * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}
......
...@@ -27,6 +27,7 @@ import org.springframework.core.type.AnnotationMetadata; ...@@ -27,6 +27,7 @@ import org.springframework.core.type.AnnotationMetadata;
* {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective * {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective
* methods will be called prior to {@link #selectImports}: * methods will be called prior to {@link #selectImports}:
* <ul> * <ul>
* <li>{@link org.springframework.context.EnvironmentAware}</li>
* <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li> * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li>
* <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li> * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li>
* <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li> * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li>
......
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,26 +16,27 @@ ...@@ -16,26 +16,27 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/** /**
* Integration tests for {@link ImportBeanDefinitionRegistrar}. * Integration tests for {@link ImportBeanDefinitionRegistrar}.
* *
...@@ -53,6 +54,7 @@ public class ImportBeanDefinitionRegistrarTests { ...@@ -53,6 +54,7 @@ public class ImportBeanDefinitionRegistrarTests {
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory())); assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader())); assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
assertThat(SampleRegistrar.resourceLoader, is(notNullValue())); assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
} }
@Sample @Sample
...@@ -69,11 +71,12 @@ public class ImportBeanDefinitionRegistrarTests { ...@@ -69,11 +71,12 @@ public class ImportBeanDefinitionRegistrarTests {
} }
static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware, static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware { BeanFactoryAware, EnvironmentAware {
static ClassLoader classLoader; static ClassLoader classLoader;
static ResourceLoader resourceLoader; static ResourceLoader resourceLoader;
static BeanFactory beanFactory; static BeanFactory beanFactory;
static Environment environment;
@Override @Override
public void setBeanClassLoader(ClassLoader classLoader) { public void setBeanClassLoader(ClassLoader classLoader) {
...@@ -90,6 +93,11 @@ public class ImportBeanDefinitionRegistrarTests { ...@@ -90,6 +93,11 @@ public class ImportBeanDefinitionRegistrarTests {
SampleRegistrar.resourceLoader = resourceLoader; SampleRegistrar.resourceLoader = resourceLoader;
} }
@Override
public void setEnvironment(Environment environment) {
SampleRegistrar.environment = environment;
}
@Override @Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
} }
......
...@@ -16,11 +16,6 @@ ...@@ -16,11 +16,6 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.spy;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
...@@ -28,12 +23,27 @@ import java.lang.annotation.Target; ...@@ -28,12 +23,27 @@ import java.lang.annotation.Target;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.MessageSource;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrarTests.SampleRegistrar;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
/** /**
* Tests for {@link ImportSelector} and {@link DeferredImportSelector}. * Tests for {@link ImportSelector} and {@link DeferredImportSelector}.
* *
...@@ -50,10 +60,62 @@ public class ImportSelectorTests { ...@@ -50,10 +60,62 @@ public class ImportSelectorTests {
context.refresh(); context.refresh();
context.getBean(Config.class); context.getBean(Config.class);
InOrder ordered = inOrder(beanFactory); InOrder ordered = inOrder(beanFactory);
ordered.verify(beanFactory).registerBeanDefinition(eq("a"), any(BeanDefinition.class)); ordered.verify(beanFactory).registerBeanDefinition(eq("a"), (BeanDefinition) anyObject());
ordered.verify(beanFactory).registerBeanDefinition(eq("b"), any(BeanDefinition.class)); ordered.verify(beanFactory).registerBeanDefinition(eq("b"), (BeanDefinition) anyObject());
ordered.verify(beanFactory).registerBeanDefinition(eq("d"), any(BeanDefinition.class)); ordered.verify(beanFactory).registerBeanDefinition(eq("d"), (BeanDefinition) anyObject());
ordered.verify(beanFactory).registerBeanDefinition(eq("c"), any(BeanDefinition.class)); ordered.verify(beanFactory).registerBeanDefinition(eq("c"), (BeanDefinition) anyObject());
}
@Test
public void invokeAwareMethodsInImportSelector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
context.getBean(MessageSource.class);
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
}
@Configuration
@Import(SampleImportSelector.class)
static class AwareConfig {
}
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware {
static ClassLoader classLoader;
static ResourceLoader resourceLoader;
static BeanFactory beanFactory;
static Environment environment;
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
SampleRegistrar.classLoader = classLoader;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
SampleRegistrar.beanFactory = beanFactory;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
SampleRegistrar.resourceLoader = resourceLoader;
}
@Override
public void setEnvironment(Environment environment) {
SampleRegistrar.environment = environment;
}
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] {};
}
} }
@Sample @Sample
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册