1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
import org .springframework .core .env .Environment ;
36
36
import org .springframework .core .io .Resource ;
37
37
import org .springframework .core .io .support .ResourcePatternUtils ;
38
- import org .springframework .util .Assert ;
39
38
import org .springframework .util .ResourceUtils ;
40
39
import org .springframework .util .StringUtils ;
41
40
@@ -84,19 +83,13 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
84
83
private BeanDefinitionParserDelegate delegate ;
85
84
86
85
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
93
87
public void setEnvironment (Environment environment ) {
94
88
this .environment = environment ;
95
89
}
96
90
97
91
/**
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
100
93
* (or DTD, historically).
101
94
* <p>Opens a DOM Document; then initializes the default settings
102
95
* specified at the {@code <beans/>} level; then parses the contained bean definitions.
@@ -108,20 +101,35 @@ public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext
108
101
doRegisterBeanDefinitions (root );
109
102
}
110
103
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
+
111
123
112
124
/**
113
125
* 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
117
126
*/
118
127
protected void doRegisterBeanDefinitions (Element root ) {
119
128
String profileSpec = root .getAttribute (PROFILE_ATTRIBUTE );
120
129
if (StringUtils .hasText (profileSpec )) {
121
- Assert .state (this .environment != null , "Environment must be set for evaluating profiles" );
122
130
String [] specifiedProfiles = StringUtils .tokenizeToStringArray (
123
131
profileSpec , BeanDefinitionParserDelegate .MULTI_VALUE_ATTRIBUTE_DELIMITERS );
124
- if (!this . environment .acceptsProfiles (specifiedProfiles )) {
132
+ if (!getEnvironment () .acceptsProfiles (specifiedProfiles )) {
125
133
return ;
126
134
}
127
135
}
@@ -147,7 +155,7 @@ protected BeanDefinitionParserDelegate createDelegate(
147
155
148
156
BeanDefinitionParserDelegate delegate = createHelper (readerContext , root , parentDelegate );
149
157
if (delegate == null ) {
150
- delegate = new BeanDefinitionParserDelegate (readerContext , this . environment );
158
+ delegate = new BeanDefinitionParserDelegate (readerContext , getEnvironment () );
151
159
delegate .initDefaults (root , parentDelegate );
152
160
}
153
161
return delegate ;
@@ -160,22 +168,6 @@ protected BeanDefinitionParserDelegate createHelper(
160
168
return null ;
161
169
}
162
170
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
-
179
171
/**
180
172
* Parse the elements at the root level in the document:
181
173
* "import", "alias", "bean".
@@ -230,7 +222,7 @@ protected void importBeanDefinitionResource(Element ele) {
230
222
}
231
223
232
224
// Resolve system properties: e.g. "${user.dir}"
233
- location = environment .resolveRequiredPlaceholders (location );
225
+ location = getEnvironment () .resolveRequiredPlaceholders (location );
234
226
235
227
Set <Resource > actualResources = new LinkedHashSet <Resource >(4 );
236
228
0 commit comments