Skip to content

XML bean definition "import" directive doesn't work with custom resource loader [SPR-5762] #10432

@spring-projects-issues

Description

@spring-projects-issues

Stefan Podkowinski opened SPR-5762 and commented

I've written a custom resource loader that is supposed to handle protocol "hdfs://" and registered the loader with the XmlBeanDefinitionReader as follows:

XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
reader.setResourceLoader(resourceLoader);

Within my application, resolving resources with the "hdfs://" prefix is working perfectly fine. So I was rather surprised to see the following import directive fail:

<beans>
<import resource="hdfs:///meta/schemes/common.xml"/>
</beans>

The following exception will occur:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [hdfs:///meta/schemes/common.xml]
Offending resource: file [/home/test/workspace/test/application-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/home/test/workspace/test/hdfs:/meta/schemes/common.xml]; nested exception is java.io.FileNotFoundException: /home/test/workspace/test/hdfs:/meta/schemes/common.xml (No such file or directory)
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:201)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:147)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:132)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:475)

The problem here is that the DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource() method will use ResourcePatternUtils.isUrl() to determine if the path is relative or not. Finally if the path is a valid url or not will be based on the outcome of valuating new URL(resourceLocation). In case a MalformedUrlException is thrown, it will return false and a relative path is build in importBeanDefinitionResource().

The thing to note here is that a MalformedUrlException thrown in the java.lang.net.URL constructor will not indicate a syntactically incorrect url, but only an unkown protocol.


Affects: 3.0 M3

Attachments:

Issue Links:

Referenced from: commits bcc6413

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions