Skip to content

Commit de3ea5d

Browse files
committed
Deprecated BeanDefinitionDocumentReader's setEnvironment method in favor of access via XmlReaderContext
Issue: SPR-12248
1 parent 1341fd4 commit de3ea5d

File tree

4 files changed

+53
-63
lines changed

4 files changed

+53
-63
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,9 @@ public interface BeanDefinitionDocumentReader {
4141
* Set the Environment to use when reading bean definitions.
4242
* <p>Used for evaluating profile information to determine whether a
4343
* {@code <beans/>} document/element should be included or ignored.
44+
* @deprecated in favor of Environment access via XmlReaderContext
4445
*/
46+
@Deprecated
4547
void setEnvironment(Environment environment);
4648

4749
/**

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,6 @@
5959
import org.springframework.beans.factory.support.MethodOverrides;
6060
import org.springframework.beans.factory.support.ReplaceOverride;
6161
import org.springframework.core.env.Environment;
62-
import org.springframework.core.env.StandardEnvironment;
6362
import org.springframework.util.Assert;
6463
import org.springframework.util.ClassUtils;
6564
import org.springframework.util.CollectionUtils;
@@ -246,12 +245,12 @@ public class BeanDefinitionParserDelegate {
246245

247246
private final XmlReaderContext readerContext;
248247

248+
private final Environment environment;
249+
249250
private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();
250251

251252
private final ParseState parseState = new ParseState();
252253

253-
private Environment environment;
254-
255254
/**
256255
* Stores all used bean names so we can enforce uniqueness on a per
257256
* beans-element basis. Duplicate bean ids/names may not exist within the
@@ -261,26 +260,24 @@ public class BeanDefinitionParserDelegate {
261260

262261

263262
/**
264-
* Create a new BeanDefinitionParserDelegate associated with the
265-
* supplied {@link XmlReaderContext} and {@link Environment}.
263+
* Create a new BeanDefinitionParserDelegate associated with the supplied
264+
* {@link XmlReaderContext}.
265+
*/
266+
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
267+
this(readerContext, readerContext.getReader().getEnvironment());
268+
}
269+
270+
/**
271+
* Create a new BeanDefinitionParserDelegate associated with the supplied
272+
* {@link XmlReaderContext}.
266273
*/
267274
public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) {
268275
Assert.notNull(readerContext, "XmlReaderContext must not be null");
269-
Assert.notNull(readerContext, "Environment must not be null");
276+
Assert.notNull(environment, "Environment must not be null");
270277
this.readerContext = readerContext;
271278
this.environment = environment;
272279
}
273280

274-
/**
275-
* Create a new BeanDefinitionParserDelegate associated with the
276-
* supplied {@link XmlReaderContext} and a new {@link StandardEnvironment}.
277-
* @deprecated since Spring 3.1 in favor of
278-
* {@link #BeanDefinitionParserDelegate(XmlReaderContext, Environment)}
279-
*/
280-
@Deprecated
281-
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
282-
this(readerContext, new StandardEnvironment());
283-
}
284281

285282
/**
286283
* Get the {@link XmlReaderContext} associated with this helper instance.
@@ -326,6 +323,13 @@ protected void error(String message, Element source, Throwable cause) {
326323
}
327324

328325

326+
/**
327+
* Initialize the default settings assuming a {@code null} parent delegate.
328+
*/
329+
public void initDefaults(Element root) {
330+
initDefaults(root, null);
331+
}
332+
329333
/**
330334
* Initialize the default lazy-init, autowire, dependency check settings,
331335
* init-method, destroy-method and merge settings. Support nested 'beans'
@@ -339,16 +343,6 @@ public void initDefaults(Element root, BeanDefinitionParserDelegate parent) {
339343
this.readerContext.fireDefaultsRegistered(this.defaults);
340344
}
341345

342-
/**
343-
* Initialize the default settings assuming a {@code null} parent delegate.
344-
* @deprecated in Spring 3.1 in favor of
345-
* {@link #initDefaults(Element, BeanDefinitionParserDelegate)}
346-
*/
347-
@Deprecated
348-
public void initDefaults(Element root) {
349-
initDefaults(root, null);
350-
}
351-
352346
/**
353347
* Populate the given DocumentDefaultsDefinition instance with the default lazy-init,
354348
* autowire, dependency check settings, init-method, destroy-method and merge settings.

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

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,6 @@
3535
import org.springframework.core.env.Environment;
3636
import org.springframework.core.io.Resource;
3737
import org.springframework.core.io.support.ResourcePatternUtils;
38-
import org.springframework.util.Assert;
3938
import org.springframework.util.ResourceUtils;
4039
import org.springframework.util.StringUtils;
4140

@@ -84,19 +83,13 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
8483
private BeanDefinitionParserDelegate delegate;
8584

8685

87-
/**
88-
* {@inheritDoc}
89-
* <p>Default value is {@code null}; property is required for parsing any
90-
* {@code <beans/>} element with a {@code profile} attribute present.
91-
* @see #doRegisterBeanDefinitions
92-
*/
86+
@Deprecated
9387
public void setEnvironment(Environment environment) {
9488
this.environment = environment;
9589
}
9690

9791
/**
98-
* {@inheritDoc}
99-
* <p>This implementation parses bean definitions according to the "spring-beans" XSD
92+
* This implementation parses bean definitions according to the "spring-beans" XSD
10093
* (or DTD, historically).
10194
* <p>Opens a DOM Document; then initializes the default settings
10295
* specified at the {@code <beans/>} level; then parses the contained bean definitions.
@@ -108,20 +101,35 @@ public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext
108101
doRegisterBeanDefinitions(root);
109102
}
110103

104+
/**
105+
* Return the descriptor for the XML resource that this parser works on.
106+
*/
107+
protected final XmlReaderContext getReaderContext() {
108+
return this.readerContext;
109+
}
110+
111+
/**
112+
* Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
113+
* source metadata from the supplied {@link Element}.
114+
*/
115+
protected Object extractSource(Element ele) {
116+
return getReaderContext().extractSource(ele);
117+
}
118+
119+
private Environment getEnvironment() {
120+
return (this.environment != null ? this.environment : getReaderContext().getReader().getEnvironment());
121+
}
122+
111123

112124
/**
113125
* Register each bean definition within the given root {@code <beans/>} element.
114-
* @throws IllegalStateException if {@code <beans profile="..."} attribute is present
115-
* and Environment property has not been set
116-
* @see #setEnvironment
117126
*/
118127
protected void doRegisterBeanDefinitions(Element root) {
119128
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
120129
if (StringUtils.hasText(profileSpec)) {
121-
Assert.state(this.environment != null, "Environment must be set for evaluating profiles");
122130
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
123131
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
124-
if (!this.environment.acceptsProfiles(specifiedProfiles)) {
132+
if (!getEnvironment().acceptsProfiles(specifiedProfiles)) {
125133
return;
126134
}
127135
}
@@ -147,7 +155,7 @@ protected BeanDefinitionParserDelegate createDelegate(
147155

148156
BeanDefinitionParserDelegate delegate = createHelper(readerContext, root, parentDelegate);
149157
if (delegate == null) {
150-
delegate = new BeanDefinitionParserDelegate(readerContext, this.environment);
158+
delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment());
151159
delegate.initDefaults(root, parentDelegate);
152160
}
153161
return delegate;
@@ -160,22 +168,6 @@ protected BeanDefinitionParserDelegate createHelper(
160168
return null;
161169
}
162170

163-
/**
164-
* Return the descriptor for the XML resource that this parser works on.
165-
*/
166-
protected final XmlReaderContext getReaderContext() {
167-
return this.readerContext;
168-
}
169-
170-
/**
171-
* Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
172-
* source metadata from the supplied {@link Element}.
173-
*/
174-
protected Object extractSource(Element ele) {
175-
return this.readerContext.extractSource(ele);
176-
}
177-
178-
179171
/**
180172
* Parse the elements at the root level in the document:
181173
* "import", "alias", "bean".
@@ -230,7 +222,7 @@ protected void importBeanDefinitionResource(Element ele) {
230222
}
231223

232224
// Resolve system properties: e.g. "${user.dir}"
233-
location = environment.resolveRequiredPlaceholders(location);
225+
location = getEnvironment().resolveRequiredPlaceholders(location);
234226

235227
Set<Resource> actualResources = new LinkedHashSet<Resource>(4);
236228

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -135,6 +135,7 @@ public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
135135
super(registry);
136136
}
137137

138+
138139
/**
139140
* Set whether to use XML validation. Default is {@code true}.
140141
* <p>This method switches namespace awareness on if validation is turned off,
@@ -486,9 +487,10 @@ protected int detectValidationMode(Resource resource) {
486487
* @see #setDocumentReaderClass
487488
* @see BeanDefinitionDocumentReader#registerBeanDefinitions
488489
*/
490+
@SuppressWarnings("deprecation")
489491
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
490492
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
491-
documentReader.setEnvironment(this.getEnvironment());
493+
documentReader.setEnvironment(getEnvironment());
492494
int countBefore = getRegistry().getBeanDefinitionCount();
493495
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
494496
return getRegistry().getBeanDefinitionCount() - countBefore;

0 commit comments

Comments
 (0)