Browse Source

Deprecated BeanDefinitionDocumentReader's setEnvironment method in favor of access via XmlReaderContext

Issue: SPR-12248
pull/689/head
Juergen Hoeller 11 years ago
parent
commit
de3ea5dad6
  1. 4
      spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionDocumentReader.java
  2. 48
      spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java
  3. 58
      spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java
  4. 6
      spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java

4
spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionDocumentReader.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 Environment access via XmlReaderContext
*/
@Deprecated
void setEnvironment(Environment environment);
/**

48
spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@ -59,7 +59,6 @@ import org.springframework.beans.factory.support.ManagedSet; @@ -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,12 +245,12 @@ public class BeanDefinitionParserDelegate { @@ -246,12 +245,12 @@ public class BeanDefinitionParserDelegate {
private final XmlReaderContext readerContext;
private final Environment environment;
private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();
private final ParseState parseState = new ParseState();
private Environment environment;
/**
* Stores all used bean names so we can enforce uniqueness on a per
* beans-element basis. Duplicate bean ids/names may not exist within the
@ -261,26 +260,24 @@ public class BeanDefinitionParserDelegate { @@ -261,26 +260,24 @@ 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) {
this(readerContext, readerContext.getReader().getEnvironment());
}
/**
* Create a new BeanDefinitionParserDelegate associated with the supplied
* {@link XmlReaderContext}.
*/
public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) {
Assert.notNull(readerContext, "XmlReaderContext must not be null");
Assert.notNull(readerContext, "Environment 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)}
*/
@Deprecated
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
this(readerContext, new StandardEnvironment());
}
/**
* Get the {@link XmlReaderContext} associated with this helper instance.
@ -326,6 +323,13 @@ public class BeanDefinitionParserDelegate { @@ -326,6 +323,13 @@ public class BeanDefinitionParserDelegate {
}
/**
* Initialize the default settings assuming a {@code null} parent delegate.
*/
public void initDefaults(Element root) {
initDefaults(root, null);
}
/**
* Initialize the default lazy-init, autowire, dependency check settings,
* init-method, destroy-method and merge settings. Support nested 'beans'
@ -339,16 +343,6 @@ public class BeanDefinitionParserDelegate { @@ -339,16 +343,6 @@ public class BeanDefinitionParserDelegate {
this.readerContext.fireDefaultsRegistered(this.defaults);
}
/**
* Initialize the default settings assuming a {@code null} parent delegate.
* @deprecated in Spring 3.1 in favor of
* {@link #initDefaults(Element, BeanDefinitionParserDelegate)}
*/
@Deprecated
public void initDefaults(Element root) {
initDefaults(root, null);
}
/**
* Populate the given DocumentDefaultsDefinition instance with the default lazy-init,
* autowire, dependency check settings, init-method, destroy-method and merge settings.

58
spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java

@ -1,5 +1,5 @@ @@ -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.
@ -35,7 +35,6 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; @@ -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;
@ -84,19 +83,13 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume @@ -84,19 +83,13 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
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
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.
@ -108,20 +101,35 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume @@ -108,20 +101,35 @@ 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);
}
private Environment getEnvironment() {
return (this.environment != null ? this.environment : getReaderContext().getReader().getEnvironment());
}
/**
* 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) {
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 (!getEnvironment().acceptsProfiles(specifiedProfiles)) {
return;
}
}
@ -147,7 +155,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume @@ -147,7 +155,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
BeanDefinitionParserDelegate delegate = createHelper(readerContext, root, parentDelegate);
if (delegate == null) {
delegate = new BeanDefinitionParserDelegate(readerContext, this.environment);
delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment());
delegate.initDefaults(root, parentDelegate);
}
return delegate;
@ -160,22 +168,6 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume @@ -160,22 +168,6 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
return null;
}
/**
* 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".
@ -230,7 +222,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume @@ -230,7 +222,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
}
// Resolve system properties: e.g. "${user.dir}"
location = environment.resolveRequiredPlaceholders(location);
location = getEnvironment().resolveRequiredPlaceholders(location);
Set<Resource> actualResources = new LinkedHashSet<Resource>(4);

6
spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -135,6 +135,7 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { @@ -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,
@ -486,9 +487,10 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { @@ -486,9 +487,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;

Loading…
Cancel
Save