Consistent Environment access in XML bean definition parsing code
Issue: SPR-12248
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -41,7 +41,9 @@ public interface BeanDefinitionDocumentReader {
|
||||
* Set the Environment to use when reading bean definitions.
|
||||
* <p>Used for evaluating profile information to determine whether a
|
||||
* {@code <beans/>} document/element should be included or ignored.
|
||||
* @deprecated in favor of {@link XmlReaderContext#getEnvironment()}
|
||||
*/
|
||||
@Deprecated
|
||||
void setEnvironment(Environment environment);
|
||||
|
||||
/**
|
||||
|
||||
@@ -59,7 +59,6 @@ import org.springframework.beans.factory.support.ManagedSet;
|
||||
import org.springframework.beans.factory.support.MethodOverrides;
|
||||
import org.springframework.beans.factory.support.ReplaceOverride;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -246,8 +245,6 @@ public class BeanDefinitionParserDelegate {
|
||||
|
||||
private final XmlReaderContext readerContext;
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();
|
||||
|
||||
private final ParseState parseState = new ParseState();
|
||||
@@ -261,25 +258,23 @@ public class BeanDefinitionParserDelegate {
|
||||
|
||||
|
||||
/**
|
||||
* Create a new BeanDefinitionParserDelegate associated with the
|
||||
* supplied {@link XmlReaderContext} and {@link Environment}.
|
||||
* Create a new BeanDefinitionParserDelegate associated with the supplied
|
||||
* {@link XmlReaderContext}.
|
||||
*/
|
||||
public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) {
|
||||
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
|
||||
Assert.notNull(readerContext, "XmlReaderContext must not be null");
|
||||
Assert.notNull(environment, "Environment must not be null");
|
||||
this.readerContext = readerContext;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new BeanDefinitionParserDelegate associated with the
|
||||
* supplied {@link XmlReaderContext} and a new {@link StandardEnvironment}.
|
||||
* @deprecated since Spring 3.1 in favor of
|
||||
* {@link #BeanDefinitionParserDelegate(XmlReaderContext, Environment)}
|
||||
* Create a new BeanDefinitionParserDelegate associated with the supplied
|
||||
* {@link XmlReaderContext}.
|
||||
* @deprecated since the given {@link Environment} parameter is effectively
|
||||
* ignored in favor of {@link XmlReaderContext#getEnvironment()}
|
||||
*/
|
||||
@Deprecated
|
||||
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
|
||||
this(readerContext, new StandardEnvironment());
|
||||
public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) {
|
||||
this(readerContext);
|
||||
}
|
||||
|
||||
|
||||
@@ -292,9 +287,11 @@ public class BeanDefinitionParserDelegate {
|
||||
|
||||
/**
|
||||
* Get the {@link Environment} associated with this helper instance.
|
||||
* @deprecated in favor of {@link XmlReaderContext#getEnvironment()}
|
||||
*/
|
||||
@Deprecated
|
||||
public final Environment getEnvironment() {
|
||||
return this.environment;
|
||||
return this.readerContext.getEnvironment();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -35,7 +35,6 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -77,27 +76,18 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private Environment environment;
|
||||
|
||||
private XmlReaderContext readerContext;
|
||||
|
||||
private BeanDefinitionParserDelegate delegate;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>Default value is {@code null}; property is required for parsing any
|
||||
* {@code <beans/>} element with a {@code profile} attribute present.
|
||||
* @see #doRegisterBeanDefinitions
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>This implementation parses bean definitions according to the "spring-beans" XSD
|
||||
* This implementation parses bean definitions according to the "spring-beans" XSD
|
||||
* (or DTD, historically).
|
||||
* <p>Opens a DOM Document; then initializes the default settings
|
||||
* specified at the {@code <beans/>} level; then parses the contained bean definitions.
|
||||
@@ -110,12 +100,24 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
doRegisterBeanDefinitions(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the descriptor for the XML resource that this parser works on.
|
||||
*/
|
||||
protected final XmlReaderContext getReaderContext() {
|
||||
return this.readerContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
|
||||
* source metadata from the supplied {@link Element}.
|
||||
*/
|
||||
protected Object extractSource(Element ele) {
|
||||
return getReaderContext().extractSource(ele);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register each bean definition within the given root {@code <beans/>} element.
|
||||
* @throws IllegalStateException if {@code <beans profile="..."} attribute is present
|
||||
* and Environment property has not been set
|
||||
* @see #setEnvironment
|
||||
*/
|
||||
protected void doRegisterBeanDefinitions(Element root) {
|
||||
// Any nested <beans> elements will cause recursion in this method. In
|
||||
@@ -125,15 +127,14 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
// then ultimately reset this.delegate back to its original (parent) reference.
|
||||
// this behavior emulates a stack of delegates without actually necessitating one.
|
||||
BeanDefinitionParserDelegate parent = this.delegate;
|
||||
this.delegate = createDelegate(this.readerContext, root, parent);
|
||||
this.delegate = createDelegate(getReaderContext(), root, parent);
|
||||
|
||||
if (this.delegate.isDefaultNamespace(root)) {
|
||||
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
|
||||
if (StringUtils.hasText(profileSpec)) {
|
||||
Assert.state(this.environment != null, "Environment must be set for evaluating profiles");
|
||||
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
|
||||
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
|
||||
if (!this.environment.acceptsProfiles(specifiedProfiles)) {
|
||||
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -149,27 +150,11 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
protected BeanDefinitionParserDelegate createDelegate(
|
||||
XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
|
||||
|
||||
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, this.environment);
|
||||
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
|
||||
delegate.initDefaults(root, parentDelegate);
|
||||
return delegate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the descriptor for the XML resource that this parser works on.
|
||||
*/
|
||||
protected final XmlReaderContext getReaderContext() {
|
||||
return this.readerContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
|
||||
* source metadata from the supplied {@link Element}.
|
||||
*/
|
||||
protected Object extractSource(Element ele) {
|
||||
return this.readerContext.extractSource(ele);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse the elements at the root level in the document:
|
||||
* "import", "alias", "bean".
|
||||
@@ -224,7 +209,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
}
|
||||
|
||||
// Resolve system properties: e.g. "${user.dir}"
|
||||
location = environment.resolveRequiredPlaceholders(location);
|
||||
location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);
|
||||
|
||||
Set<Resource> actualResources = new LinkedHashSet<Resource>(4);
|
||||
|
||||
|
||||
@@ -135,6 +135,7 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
|
||||
super(registry);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set whether to use XML validation. Default is {@code true}.
|
||||
* <p>This method switches namespace awareness on if validation is turned off,
|
||||
@@ -501,9 +502,10 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
|
||||
* @see #setDocumentReaderClass
|
||||
* @see BeanDefinitionDocumentReader#registerBeanDefinitions
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
|
||||
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
|
||||
documentReader.setEnvironment(this.getEnvironment());
|
||||
documentReader.setEnvironment(getEnvironment());
|
||||
int countBefore = getRegistry().getBeanDefinitionCount();
|
||||
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
|
||||
return getRegistry().getBeanDefinitionCount() - countBefore;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -28,6 +28,7 @@ import org.springframework.beans.factory.parsing.ReaderContext;
|
||||
import org.springframework.beans.factory.parsing.ReaderEventListener;
|
||||
import org.springframework.beans.factory.parsing.SourceExtractor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
@@ -74,6 +75,10 @@ public class XmlReaderContext extends ReaderContext {
|
||||
return this.reader.getBeanClassLoader();
|
||||
}
|
||||
|
||||
public final Environment getEnvironment() {
|
||||
return this.reader.getEnvironment();
|
||||
}
|
||||
|
||||
public final NamespaceHandlerResolver getNamespaceHandlerResolver() {
|
||||
return this.namespaceHandlerResolver;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user