Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Spring finds the XSD file from classpath

Tags:

java

spring

xml

xsd

In our spring configuration we put the beans tag like this:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

Now spring has to find out from my calsspath for the location of the files spring-beans.xsd & spring-context.xsd.

I have found some xsd files at this path:

spring-context-4.1.5.RELEASE.jar/org/springframework/context/config

spring-context-2.5.xsd
spring-context-3.0.xsd
spring-context-3.1.xsd
spring-context-3.2.xsd
spring-context-4.0.xsd
spring-context-4.1.xsd

When searching for spring-beans.xsd file I found it some files in this path:

spring-beans-4.1.5.RELEASE.jar/org/springframework/beans/factory/xml

spring-beans-2.5.xsd
spring-beans-3.0.xsd
spring-beans-3.1.xsd
spring-beans-3.2.xsd
spring-beans-4.0.xsd
spring-beans-4.1.xsd

How spring will know where to look for this file as there is no link between the schema location in my beans tag and the actual location of this file. Also I am able to find out files like this spring-beans-<version>.xsd then how spring will know what is spring-beans.xsd even when we do not specify any version in <beans> tag.

like image 223
learner Avatar asked Aug 10 '15 18:08

learner


People also ask

How do I find the XSD schema?

xsd file. To see the schema set in the XML Schema Explorer, right-click an XML node in an XML literal or an XML namespace import and select the Show in Schema Explorer command. For more information, see Integration of XML literals with XML Schema Explorer.


1 Answers

Some spring libraries contain some file like META-INF/spring.schemas

The properties file called 'spring.schemas' contains a mapping of XML Schema locations (referred to along with the schema declaration in XML files that use the schema as part of the 'xsi:schemaLocation' attribute) to classpath resources. This file is needed to prevent Spring from absolutely having to use a default EntityResolver that requires Internet access to retrieve the schema file. If you specify the mapping in this properties file, Spring will search for the schema on the classpath

e.g.

spring.schemas of spring-beans-x.x.x-RELEASE.jar

http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd

In few words, above properties allows it to map XSD resource to schemaLocation attribute.

For more details, also see Need understanding of spring.handlers and spring.schemas .

like image 191
Xstian Avatar answered Sep 21 '22 08:09

Xstian