How does spring load the XSD file (Org. xml. Sax. saxparseexception: failed to read schema document error solution)

Source: Internet
Author: User
Tags file url

The original Article connection: http://blog.csdn.net/bluishglc/article/details/7596118, reproduced please indicate the source!

Sometimes you will find that the system has been running normally in the past, and the following error will be reported when you start the system one day:

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans-2.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

Apparently, the XSD file specified in the spring xml configuration file cannot be read because of network disconnection or the failure to connect to the spring official website. You can confirm by entering the XSD File URL in your browser, such as the http://www.springframework.org/schema/beans/spring-beans-2.0.xsd.

There are two common solutions on the Internet for this problem. The first one is simple and effective, but the workload is large, namely: convert the URL-based XSD path in all spring configuration files to the classpath-based path pointing to the local XSD file, for example: classpath: ORG/springframework/beans/Factory/XML/spring-beans-2.5.xsd, another method is to build a web server on the local machine, create a corresponding folder by URL, put the corresponding XSD file, and add "127.0.0.1 www.springframework.org" to the hosts file on the local machine ". in fact, both methods belong to the "squatting" method. To understand this problem, we still need to talk about the XSD file loading mechanism of spring.

First, you must know one thing:When loading the XSD file, Spring always tries to find the XSD file locally (the spring jar package contains all versions of the XSD file). If not, to go to the path specified by the URL for download.This is a very reasonable practice, not as it seems, every time it is downloaded from the site. In fact, if all your configurations are correct, your project can be fully started when the network is disconnected without reporting the preceding errors. The class for spring to load the XSD file is pluggableschemaresolver. You can check its source code to verify the above statement. In addition, you can add the following to the log4j. xml file:

<logger name="org.springframework.beans.factory.xml"><level value="all" /></logger>

Learn how spring loads the XSD file through logs.

Next, the question is why spring does not find the desired file locally and has to switch to the website for download. This is actually very simple. In many spring jar packages, there is one under the META-INF directorySpring. Schemas, This is a property file whose content is similar to the following:

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

In fact, this file is the XSD ing between spring and the path where the XSD file is stored locally. Spring searches for the XSD file locally (that is, in spring jar. Otherwise, the declared XSD file version does not exist locally. In general, the new version of spring jar package will pack all the XSD versions of the past (should be later than 2.0), and in spring. the corresponding items are added to the schemas file. When a problem occurs, it is often declared that a higher-version XSD file, such as 3.0, is used, but the jar package of spring dependent is a version earlier than 2.5, because version 2.5 naturally cannot contain an XSD file of version 3.0, it will cause spring to download the target XSD file from the site. In case of network disconnection or unavailability of the target site, the above problem will occur.

However, during implementation and development, the probability of the above errors is not high. The most common cause of this problem is actually the use of a"Assembly. Many projects need to package the project together with all the jar packages it depends on into a jar package. The Maven Assembly plug-in is used to complete this task. However, because the project often depends on many jar packages, and the dependent jar will depend on other jar packages, so that when the project depends on different versions of spring, when using assembly for packaging, you can only use the spring. the schemas file is placed in the final jar package, which may omit the local XSD ing of some versions of XSD, and the error mentioned at the beginning of the article appears. If your project is packaged into a single jar, you can check the spring. schemas file in the generated jar to check whether this is the case. In this case, we recommend that you use another packaging plug-in.Shade, It is indeed a better tool than assembly, in the spring. in schemas File Processing, shade can. the schemas file is merged and spring. schemas contains a set of all versions that have appeared!

The above is the mechanism for loading XSD files by spring and the cause of the problem. In fact, we should let our project always load the local XSD file at startup, instead of going to the site to download each time, to do this, you need to check your project based on the situations mentioned above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.