17
17
package org .springframework .beans .factory .xml ;
18
18
19
19
import java .io .IOException ;
20
+ import java .net .URISyntaxException ;
20
21
import java .util .LinkedHashSet ;
21
22
import java .util .Set ;
22
23
23
24
import org .apache .commons .logging .Log ;
24
25
import org .apache .commons .logging .LogFactory ;
26
+ import org .w3c .dom .Document ;
27
+ import org .w3c .dom .Element ;
28
+ import org .w3c .dom .Node ;
29
+ import org .w3c .dom .NodeList ;
30
+
25
31
import org .springframework .beans .factory .BeanDefinitionStoreException ;
26
32
import org .springframework .beans .factory .config .BeanDefinitionHolder ;
27
33
import org .springframework .beans .factory .parsing .BeanComponentDefinition ;
31
37
import org .springframework .util .ResourceUtils ;
32
38
import org .springframework .util .StringUtils ;
33
39
import org .springframework .util .SystemPropertyUtils ;
34
- import org .w3c .dom .Document ;
35
- import org .w3c .dom .Element ;
36
- import org .w3c .dom .Node ;
37
- import org .w3c .dom .NodeList ;
38
40
39
41
/**
40
42
* Default implementation of the {@link BeanDefinitionDocumentReader} interface.
@@ -171,15 +173,15 @@ protected void importBeanDefinitionResource(Element ele) {
171
173
172
174
// Discover whether the location is an absolute or relative URI
173
175
boolean absoluteLocation = false ;
174
-
175
176
try {
176
177
absoluteLocation = ResourcePatternUtils .isUrl (location ) || ResourceUtils .toURI (location ).isAbsolute ();
177
- } catch (Exception ex ) {
178
+ }
179
+ catch (URISyntaxException ex ) {
178
180
// cannot convert to an URI, considering the location relative
179
- // unless it is the well-known Spring prefix classpath*:
181
+ // unless it is the well-known Spring prefix " classpath*:"
180
182
}
181
-
182
- // check the
183
+
184
+ // Absolute or relative?
183
185
if (absoluteLocation ) {
184
186
try {
185
187
int importCount = getReaderContext ().getReader ().loadBeanDefinitions (location , actualResources );
@@ -195,9 +197,17 @@ protected void importBeanDefinitionResource(Element ele) {
195
197
else {
196
198
// No URL -> considering resource location as relative to the current file.
197
199
try {
198
- String baseLocation = getReaderContext ().getResource ().getURL ().toString ();
199
- int importCount = getReaderContext ().getReader ().loadBeanDefinitions (
200
- StringUtils .applyRelativePath (baseLocation , location ), actualResources );
200
+ int importCount ;
201
+ Resource relativeResource = getReaderContext ().getResource ().createRelative (location );
202
+ if (relativeResource .exists ()) {
203
+ importCount = getReaderContext ().getReader ().loadBeanDefinitions (relativeResource );
204
+ actualResources .add (relativeResource );
205
+ }
206
+ else {
207
+ String baseLocation = getReaderContext ().getResource ().getURL ().toString ();
208
+ importCount = getReaderContext ().getReader ().loadBeanDefinitions (
209
+ StringUtils .applyRelativePath (baseLocation , location ), actualResources );
210
+ }
201
211
if (logger .isDebugEnabled ()) {
202
212
logger .debug ("Imported " + importCount + " bean definitions from relative location [" + location + "]" );
203
213
}
0 commit comments