Not exposing ApplicationContext on ConditionContext anymore

This commit is contained in:
Juergen Hoeller
2013-08-28 00:00:54 +02:00
parent 1e64eed6b2
commit 5efe894ee4
9 changed files with 78 additions and 129 deletions

View File

@@ -79,8 +79,7 @@ public class AnnotatedBeanDefinitionReader {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
Assert.notNull(environment, "Environment must not be null");
this.registry = registry;
this.conditionEvaluator = new ConditionEvaluator(registry, environment,
null, null, null);
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
@@ -98,8 +97,7 @@ public class AnnotatedBeanDefinitionReader {
* @see #registerBean(Class, String, Class...)
*/
public void setEnvironment(Environment environment) {
this.conditionEvaluator = new ConditionEvaluator(this.registry, environment,
null, null, null);
this.conditionEvaluator = new ConditionEvaluator(this.registry, environment, null);
}
/**
@@ -135,9 +133,10 @@ public class AnnotatedBeanDefinitionReader {
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
if (conditionEvaluator.shouldSkip(abd.getMetadata())) {
if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
return;
}
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
abd.setScope(scopeMetadata.getScopeName());
String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
@@ -155,6 +154,7 @@ public class AnnotatedBeanDefinitionReader {
}
}
}
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);

View File

@@ -25,6 +25,7 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
@@ -174,7 +175,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
}
/**
* Returns the {@link BeanDefinitionRegistry} used by this scanner or {@code null}.
* Returns the {@link BeanDefinitionRegistry} used by this scanner, if any.
*/
protected BeanDefinitionRegistry getRegistry() {
return null;
@@ -355,8 +356,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
*/
private boolean isConditionMatch(MetadataReader metadataReader) {
if (this.conditionEvaluator == null) {
this.conditionEvaluator = new ConditionEvaluator(getRegistry(),
getEnvironment(), null, null, getResourceLoader());
this.conditionEvaluator = new ConditionEvaluator(getRegistry(), getEnvironment(), getResourceLoader());
}
return !conditionEvaluator.shouldSkip(metadataReader.getAnnotationMetadata());
}

View File

@@ -18,7 +18,6 @@ package org.springframework.context.annotation;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
@@ -31,41 +30,39 @@ import org.springframework.core.io.ResourceLoader;
public interface ConditionContext {
/**
* Returns the {@link BeanDefinitionRegistry} that will hold the bean definition
* Return the {@link BeanDefinitionRegistry} that will hold the bean definition
* should the condition match or {@code null} if the registry is not available.
* @return the registry or {@code null}
*/
BeanDefinitionRegistry getRegistry();
/**
* Return the {@link Environment} for which the current application is running or
* {@code null} if no environment is available.
* @return the environment or {@code null}
*/
Environment getEnvironment();
/**
* Returns the {@link ConfigurableListableBeanFactory} that will hold the bean
* definition should the condition match or {@code null} if the bean factory is
* not available.
* Return the {@link ConfigurableListableBeanFactory} that will hold the bean
* definition should the condition match or {@code null} if the bean factory
* is not available.
* @return the bean factory or {@code null}
*/
ConfigurableListableBeanFactory getBeanFactory();
/**
* Returns the {@link ResourceLoader} currently being used or {@code null} if the
* resource loader cannot be obtained.
* Return the {@link Environment} for which the current application is running
* or {@code null} if no environment is available.
* @return the environment or {@code null}
*/
Environment getEnvironment();
/**
* Return the {@link ResourceLoader} currently being used or {@code null}
* if the resource loader cannot be obtained.
* @return a resource loader or {@code null}
*/
ResourceLoader getResourceLoader();
/**
* Returns the {@link ClassLoader} that should be used to load additional classes
* or {@code null} if the default classloader should be used.
* @return the classloader or {@code null}
* Return the {@link ClassLoader} that should be used to load additional
* classes or {@code null} if the default classloader should be used.
* @return the class loader or {@code null}
*/
ClassLoader getClassLoader();
ApplicationContext getApplicationContext();
}

View File

@@ -22,7 +22,6 @@ import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
import org.springframework.core.env.Environment;
@@ -48,10 +47,8 @@ class ConditionEvaluator {
/**
* Create a new {@link ConditionEvaluator} instance.
*/
public ConditionEvaluator(BeanDefinitionRegistry registry, Environment environment,
ApplicationContext applicationContext, ClassLoader classLoader, ResourceLoader resourceLoader) {
this.context = new ConditionContextImpl(registry, environment, applicationContext, classLoader, resourceLoader);
public ConditionEvaluator(BeanDefinitionRegistry registry, Environment environment, ResourceLoader resourceLoader) {
this.context = new ConditionContextImpl(registry, environment, resourceLoader);
}
@@ -99,6 +96,7 @@ class ConditionEvaluator {
}
}
}
return false;
}
@@ -120,99 +118,72 @@ class ConditionEvaluator {
*/
private static class ConditionContextImpl implements ConditionContext {
private BeanDefinitionRegistry registry;
private final BeanDefinitionRegistry registry;
private ConfigurableListableBeanFactory beanFactory;
private final ConfigurableListableBeanFactory beanFactory;
private Environment environment;
private final Environment environment;
private ApplicationContext applicationContext;
private final ResourceLoader resourceLoader;
private ClassLoader classLoader;
private ResourceLoader resourceLoader;
public ConditionContextImpl(BeanDefinitionRegistry registry,
Environment environment, ApplicationContext applicationContext,
ClassLoader classLoader, ResourceLoader resourceLoader) {
public ConditionContextImpl(BeanDefinitionRegistry registry, Environment environment, ResourceLoader resourceLoader) {
this.registry = registry;
this.beanFactory = deduceBeanFactory(registry);
this.environment = environment;
this.applicationContext = applicationContext;
this.classLoader = classLoader;
this.resourceLoader = resourceLoader;
this.environment = (environment != null ? environment : deduceEnvironment(registry));
this.resourceLoader = (resourceLoader != null ? resourceLoader : deduceResourceLoader(registry));
}
private ConfigurableListableBeanFactory deduceBeanFactory(Object source) {
if (source == null) {
return null;
}
private ConfigurableListableBeanFactory deduceBeanFactory(BeanDefinitionRegistry source) {
if (source instanceof ConfigurableListableBeanFactory) {
return (ConfigurableListableBeanFactory) source;
}
else if (source instanceof ConfigurableApplicationContext) {
return deduceBeanFactory(((ConfigurableApplicationContext) source).getBeanFactory());
if (source instanceof ConfigurableApplicationContext) {
return (((ConfigurableApplicationContext) source).getBeanFactory());
}
return null;
}
private Environment deduceEnvironment(BeanDefinitionRegistry source) {
if (source instanceof EnvironmentCapable) {
return ((EnvironmentCapable) source).getEnvironment();
}
return null;
}
private ResourceLoader deduceResourceLoader(BeanDefinitionRegistry source) {
if (source instanceof ResourceLoader) {
return (ResourceLoader) source;
}
return null;
}
@Override
public BeanDefinitionRegistry getRegistry() {
if (this.registry != null) {
return this.registry;
}
if (getBeanFactory() instanceof BeanDefinitionRegistry) {
return (BeanDefinitionRegistry) getBeanFactory();
}
return null;
}
@Override
public Environment getEnvironment() {
if (this.environment != null) {
return this.environment;
}
if (getRegistry() instanceof EnvironmentCapable) {
return ((EnvironmentCapable) getRegistry()).getEnvironment();
}
return null;
return this.registry;
}
@Override
public ConfigurableListableBeanFactory getBeanFactory() {
Assert.state(this.beanFactory != null, "Unable to locate the BeanFactory");
return this.beanFactory;
}
@Override
public Environment getEnvironment() {
return this.environment;
}
@Override
public ResourceLoader getResourceLoader() {
if (this.resourceLoader != null) {
return this.resourceLoader;
}
if (this.registry instanceof ResourceLoader) {
return (ResourceLoader) registry;
}
return null;
return this.resourceLoader;
}
@Override
public ClassLoader getClassLoader() {
if (this.classLoader != null) {
return this.classLoader;
if (this.resourceLoader != null) {
return this.resourceLoader.getClassLoader();
}
if (getResourceLoader() != null) {
return getResourceLoader().getClassLoader();
}
return null;
}
@Override
public ApplicationContext getApplicationContext() {
if (this.applicationContext != null) {
return this.applicationContext;
}
if (getRegistry() instanceof ApplicationContext) {
return (ApplicationContext) getRegistry();
if (this.beanFactory != null) {
return this.beanFactory.getBeanClassLoader();
}
return null;
}

View File

@@ -26,6 +26,7 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
@@ -42,7 +43,6 @@ import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
@@ -87,15 +87,14 @@ class ConfigurationClassBeanDefinitionReader {
private final ConditionEvaluator conditionEvaluator;
/**
* Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used
* to populate the given {@link BeanDefinitionRegistry}.
*/
public ConfigurationClassBeanDefinitionReader(
BeanDefinitionRegistry registry, ApplicationContext applicationContext,
SourceExtractor sourceExtractor, ProblemReporter problemReporter,
MetadataReaderFactory metadataReaderFactory, ResourceLoader resourceLoader,
Environment environment, BeanNameGenerator importBeanNameGenerator) {
public ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor,
ProblemReporter problemReporter, MetadataReaderFactory metadataReaderFactory,
ResourceLoader resourceLoader, Environment environment, BeanNameGenerator importBeanNameGenerator) {
this.registry = registry;
this.sourceExtractor = sourceExtractor;
@@ -104,8 +103,7 @@ class ConfigurationClassBeanDefinitionReader {
this.resourceLoader = resourceLoader;
this.environment = environment;
this.importBeanNameGenerator = importBeanNameGenerator;
this.conditionEvaluator = new ConditionEvaluator(registry, environment,
applicationContext, null, resourceLoader);
this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader);
}

View File

@@ -46,7 +46,6 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition;
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.ApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
@@ -128,8 +127,7 @@ class ConfigurationClassParser {
*/
public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory,
ProblemReporter problemReporter, Environment environment, ResourceLoader resourceLoader,
BeanNameGenerator componentScanBeanNameGenerator, BeanDefinitionRegistry registry,
ApplicationContext applicationContext) {
BeanNameGenerator componentScanBeanNameGenerator, BeanDefinitionRegistry registry) {
this.metadataReaderFactory = metadataReaderFactory;
this.problemReporter = problemReporter;
@@ -138,8 +136,7 @@ class ConfigurationClassParser {
this.registry = registry;
this.componentScanParser = new ComponentScanAnnotationParser(
resourceLoader, environment, componentScanBeanNameGenerator, registry);
this.conditionEvaluator = new ConditionEvaluator(registry, environment,
applicationContext, null, resourceLoader);
this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader);
}

View File

@@ -27,6 +27,7 @@ import java.util.Stack;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -50,8 +51,6 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ConfigurationClassEnhancer.EnhancedConfiguration;
@@ -92,8 +91,7 @@ import static org.springframework.context.annotation.AnnotationConfigUtils.*;
* @since 3.0
*/
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware, ApplicationContextAware,
Ordered {
ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware, Ordered {
private static final String IMPORT_AWARE_PROCESSOR_BEAN_NAME =
ConfigurationClassPostProcessor.class.getName() + ".importAwareProcessor";
@@ -113,8 +111,6 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
private Environment environment;
private ApplicationContext applicationContext;
private ResourceLoader resourceLoader = new DefaultResourceLoader();
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
@@ -203,12 +199,6 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
this.environment = environment;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
@@ -303,8 +293,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
// Parse each @Configuration class
ConfigurationClassParser parser = new ConfigurationClassParser(
this.metadataReaderFactory, this.problemReporter, this.environment,
this.resourceLoader, this.componentScanBeanNameGenerator, registry,
this.applicationContext);
this.resourceLoader, this.componentScanBeanNameGenerator, registry);
parser.parse(configCandidates);
parser.validate();
@@ -325,10 +314,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
// Read the model and create bean definitions based on its content
if (this.reader == null) {
this.reader = new ConfigurationClassBeanDefinitionReader(
registry, this.applicationContext, this.sourceExtractor,
this.problemReporter, this.metadataReaderFactory,
this.resourceLoader, this.environment, this.importBeanNameGenerator);
this.reader = new ConfigurationClassBeanDefinitionReader(registry, this.sourceExtractor,
this.problemReporter, this.metadataReaderFactory, this.resourceLoader, this.environment,
this.importBeanNameGenerator);
}
reader.loadBeanDefinitions(parser.getConfigurationClasses());