Skip to content

Commit 09a55c8

Browse files
committed
import works with relative resources in other classpath roots again (SPR-6493)
1 parent 231c833 commit 09a55c8

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

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

+22-12
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
package org.springframework.beans.factory.xml;
1818

1919
import java.io.IOException;
20+
import java.net.URISyntaxException;
2021
import java.util.LinkedHashSet;
2122
import java.util.Set;
2223

2324
import org.apache.commons.logging.Log;
2425
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+
2531
import org.springframework.beans.factory.BeanDefinitionStoreException;
2632
import org.springframework.beans.factory.config.BeanDefinitionHolder;
2733
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
@@ -31,10 +37,6 @@
3137
import org.springframework.util.ResourceUtils;
3238
import org.springframework.util.StringUtils;
3339
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;
3840

3941
/**
4042
* Default implementation of the {@link BeanDefinitionDocumentReader} interface.
@@ -171,15 +173,15 @@ protected void importBeanDefinitionResource(Element ele) {
171173

172174
// Discover whether the location is an absolute or relative URI
173175
boolean absoluteLocation = false;
174-
175176
try {
176177
absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
177-
} catch (Exception ex) {
178+
}
179+
catch (URISyntaxException ex) {
178180
// 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*:"
180182
}
181-
182-
// check the
183+
184+
// Absolute or relative?
183185
if (absoluteLocation) {
184186
try {
185187
int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);
@@ -195,9 +197,17 @@ protected void importBeanDefinitionResource(Element ele) {
195197
else {
196198
// No URL -> considering resource location as relative to the current file.
197199
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+
}
201211
if (logger.isDebugEnabled()) {
202212
logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
203213
}

0 commit comments

Comments
 (0)