Skip to content

Commit 5ecdd8c

Browse files
committed
Consistent Environment access in XML bean definition parsing code
Issue: SPR-12248
1 parent b0e6091 commit 5ecdd8c

File tree

8 files changed

+53
-62
lines changed

8 files changed

+53
-62
lines changed

spring-beans-groovy/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ else if (this.currentBeanDefinition != null) {
671671

672672
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
673673
XmlReaderContext readerContext = this.xmlBeanDefinitionReader.createReaderContext(new DescriptiveResource("Groovy"));
674-
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment());
674+
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
675675
boolean decorating = (this.currentBeanDefinition != null);
676676
if (!decorating) {
677677
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);

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 {@link XmlReaderContext#getEnvironment()}
4445
*/
46+
@Deprecated
4547
void setEnvironment(Environment environment);
4648

4749
/**

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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,8 +245,6 @@ public class BeanDefinitionParserDelegate {
246245

247246
private final XmlReaderContext readerContext;
248247

249-
private final Environment environment;
250-
251248
private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();
252249

253250
private final ParseState parseState = new ParseState();
@@ -261,25 +258,23 @@ public class BeanDefinitionParserDelegate {
261258

262259

263260
/**
264-
* Create a new BeanDefinitionParserDelegate associated with the
265-
* supplied {@link XmlReaderContext} and {@link Environment}.
261+
* Create a new BeanDefinitionParserDelegate associated with the supplied
262+
* {@link XmlReaderContext}.
266263
*/
267-
public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) {
264+
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
268265
Assert.notNull(readerContext, "XmlReaderContext must not be null");
269-
Assert.notNull(environment, "Environment must not be null");
270266
this.readerContext = readerContext;
271-
this.environment = environment;
272267
}
273268

274269
/**
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)}
270+
* Create a new BeanDefinitionParserDelegate associated with the supplied
271+
* {@link XmlReaderContext}.
272+
* @deprecated since the given {@link Environment} parameter is effectively
273+
* ignored in favor of {@link XmlReaderContext#getEnvironment()}
279274
*/
280275
@Deprecated
281-
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
282-
this(readerContext, new StandardEnvironment());
276+
public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) {
277+
this(readerContext);
283278
}
284279

285280

@@ -292,9 +287,11 @@ public final XmlReaderContext getReaderContext() {
292287

293288
/**
294289
* Get the {@link Environment} associated with this helper instance.
290+
* @deprecated in favor of {@link XmlReaderContext#getEnvironment()}
295291
*/
292+
@Deprecated
296293
public final Environment getEnvironment() {
297-
return this.environment;
294+
return this.readerContext.getEnvironment();
298295
}
299296

300297
/**

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

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -77,27 +76,18 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
7776

7877
protected final Log logger = LogFactory.getLog(getClass());
7978

80-
private Environment environment;
81-
8279
private XmlReaderContext readerContext;
8380

8481
private BeanDefinitionParserDelegate delegate;
8582

8683

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-
*/
84+
@Deprecated
9385
@Override
9486
public void setEnvironment(Environment environment) {
95-
this.environment = environment;
9687
}
9788

9889
/**
99-
* {@inheritDoc}
100-
* <p>This implementation parses bean definitions according to the "spring-beans" XSD
90+
* This implementation parses bean definitions according to the "spring-beans" XSD
10191
* (or DTD, historically).
10292
* <p>Opens a DOM Document; then initializes the default settings
10393
* specified at the {@code <beans/>} level; then parses the contained bean definitions.
@@ -110,12 +100,24 @@ public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext
110100
doRegisterBeanDefinitions(root);
111101
}
112102

103+
/**
104+
* Return the descriptor for the XML resource that this parser works on.
105+
*/
106+
protected final XmlReaderContext getReaderContext() {
107+
return this.readerContext;
108+
}
109+
110+
/**
111+
* Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
112+
* source metadata from the supplied {@link Element}.
113+
*/
114+
protected Object extractSource(Element ele) {
115+
return getReaderContext().extractSource(ele);
116+
}
117+
113118

114119
/**
115120
* Register each bean definition within the given root {@code <beans/>} element.
116-
* @throws IllegalStateException if {@code <beans profile="..."} attribute is present
117-
* and Environment property has not been set
118-
* @see #setEnvironment
119121
*/
120122
protected void doRegisterBeanDefinitions(Element root) {
121123
// Any nested <beans> elements will cause recursion in this method. In
@@ -125,15 +127,14 @@ protected void doRegisterBeanDefinitions(Element root) {
125127
// then ultimately reset this.delegate back to its original (parent) reference.
126128
// this behavior emulates a stack of delegates without actually necessitating one.
127129
BeanDefinitionParserDelegate parent = this.delegate;
128-
this.delegate = createDelegate(this.readerContext, root, parent);
130+
this.delegate = createDelegate(getReaderContext(), root, parent);
129131

130132
if (this.delegate.isDefaultNamespace(root)) {
131133
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
132134
if (StringUtils.hasText(profileSpec)) {
133-
Assert.state(this.environment != null, "Environment must be set for evaluating profiles");
134135
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
135136
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
136-
if (!this.environment.acceptsProfiles(specifiedProfiles)) {
137+
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
137138
return;
138139
}
139140
}
@@ -149,27 +150,11 @@ protected void doRegisterBeanDefinitions(Element root) {
149150
protected BeanDefinitionParserDelegate createDelegate(
150151
XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
151152

152-
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, this.environment);
153+
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
153154
delegate.initDefaults(root, parentDelegate);
154155
return delegate;
155156
}
156157

157-
/**
158-
* Return the descriptor for the XML resource that this parser works on.
159-
*/
160-
protected final XmlReaderContext getReaderContext() {
161-
return this.readerContext;
162-
}
163-
164-
/**
165-
* Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
166-
* source metadata from the supplied {@link Element}.
167-
*/
168-
protected Object extractSource(Element ele) {
169-
return this.readerContext.extractSource(ele);
170-
}
171-
172-
173158
/**
174159
* Parse the elements at the root level in the document:
175160
* "import", "alias", "bean".
@@ -224,7 +209,7 @@ protected void importBeanDefinitionResource(Element ele) {
224209
}
225210

226211
// Resolve system properties: e.g. "${user.dir}"
227-
location = environment.resolveRequiredPlaceholders(location);
212+
location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);
228213

229214
Set<Resource> actualResources = new LinkedHashSet<Resource>(4);
230215

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -501,9 +502,10 @@ protected int detectValidationMode(Resource resource) {
501502
* @see #setDocumentReaderClass
502503
* @see BeanDefinitionDocumentReader#registerBeanDefinitions
503504
*/
505+
@SuppressWarnings("deprecation")
504506
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
505507
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
506-
documentReader.setEnvironment(this.getEnvironment());
508+
documentReader.setEnvironment(getEnvironment());
507509
int countBefore = getRegistry().getBeanDefinitionCount();
508510
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
509511
return getRegistry().getBeanDefinitionCount() - countBefore;

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

Lines changed: 6 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.factory.parsing.ReaderEventListener;
2929
import org.springframework.beans.factory.parsing.SourceExtractor;
3030
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31+
import org.springframework.core.env.Environment;
3132
import org.springframework.core.io.Resource;
3233
import org.springframework.core.io.ResourceLoader;
3334

@@ -74,6 +75,10 @@ public final ClassLoader getBeanClassLoader() {
7475
return this.reader.getBeanClassLoader();
7576
}
7677

78+
public final Environment getEnvironment() {
79+
return this.reader.getEnvironment();
80+
}
81+
7782
public final NamespaceHandlerResolver getNamespaceHandlerResolver() {
7883
return this.namespaceHandlerResolver;
7984
}

spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
103103
* {@link org.springframework.stereotype.Component @Component},
104104
* {@link org.springframework.stereotype.Repository @Repository},
105105
* {@link org.springframework.stereotype.Service @Service}, and
106-
* {@link org.springframework.stereotype.Controller @Controller} stereotype
107-
* annotations.
106+
* {@link org.springframework.stereotype.Controller @Controller} stereotype annotations
108107
* @see #setResourceLoader
109108
* @see #setEnvironment
110109
*/
@@ -124,13 +123,12 @@ public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean u
124123
* @param registry the {@code BeanFactory} to load bean definitions into, in the form
125124
* of a {@code BeanDefinitionRegistry}
126125
* @param useDefaultFilters whether to include the default filters for the
127-
* @param environment the Spring {@link Environment} to use when evaluating bean
128-
* definition profile metadata.
129126
* {@link org.springframework.stereotype.Component @Component},
130127
* {@link org.springframework.stereotype.Repository @Repository},
131128
* {@link org.springframework.stereotype.Service @Service}, and
132-
* {@link org.springframework.stereotype.Controller @Controller} stereotype
133-
* annotations.
129+
* {@link org.springframework.stereotype.Controller @Controller} stereotype annotations
130+
* @param environment the Spring {@link Environment} to use when evaluating bean
131+
* definition profile metadata
134132
* @since 3.1
135133
* @see #setResourceLoader
136134
*/

spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, En
115115
if (useDefaultFilters) {
116116
registerDefaultFilters();
117117
}
118+
Assert.notNull(environment, "Environment must not be null");
118119
this.environment = environment;
119120
}
120121

@@ -161,10 +162,11 @@ public final MetadataReaderFactory getMetadataReaderFactory() {
161162
/**
162163
* Set the Environment to use when resolving placeholders and evaluating
163164
* {@link Conditional @Conditional}-annotated component classes.
164-
* <p>The default is a {@link StandardEnvironment}
165+
* <p>The default is a {@link StandardEnvironment}.
165166
* @param environment the Environment to use
166167
*/
167168
public void setEnvironment(Environment environment) {
169+
Assert.notNull(environment, "Environment must not be null");
168170
this.environment = environment;
169171
this.conditionEvaluator = null;
170172
}

0 commit comments

Comments
 (0)