Why not configure the version number of the XSD file in spring configuration

Source: Internet
Author: User
Tags xml parser

Why Dubbo boot no problem?

Original link: Http://www.tuicool.com/articles/YRn67zM

This blog comes from a question:

Our company makes Ali's Dubbo, but Ali's Open source website http://code.alibabatech.com, hangs for several months, why our application launches no problem?

The spring configuration file for our app has a similar configuration:

<?xml version= "1.0" encoding= "UTF-8"?><xmlns="Http://www.springframework.org/schema/beans" xmlns:xsi=xmlns:dubbo="Http://code.alibabatech.com/schema/dubbo"xsi:schemalocation="http ://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  Http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> 

We all know that spring is going to validate the XML file at startup. Or why is there no error in XML in eclipse?

For example, a spring configuration like this:

<?xml version= "1.0" encoding= "UTF-8"?><xmlns="Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">< /beans>        

We can also add the version number to the following:

xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd ">

What's the difference between having this version number and not having it?

Some concepts of XML

First look at some of the concepts of XML:

There is a namespace in the XML schema that can give it an individual name. For example, the common spring namespace:

Xmlns:mvc= "Http://www.springframework.org/schema/mvc"  xmlns:context= "http://www.springframework.org/ Schema/context "

Typically, the URI of the namespace is an address that holds the XSD, although the specification is not required. If schemalocation is not provided, the spring XML parser will load the XSD file from the URI of the namespace. We can change the configuration file to look like this, also can work normally:

<?xml version= "1.0" encoding= "UTF-8"?><xmlns="http://www.springframework.org/schema/beans/ Spring-beans.xsd "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance ">    

SchemaLocation provides a mapping of XML namespace to the corresponding XSD file, so we can see that the strings configured after xsi:schemalocation are paired, preceded by the namespace URI, followed by the URI of the XSD file. Like what:

xsi:schemalocation= "Http://www.springframework.org/schema/beans  http://www.springframework.org/schema/ Beans/spring-beans.xsd  http://www.springframework.org/schema/security/  http Www.springframework.org/schema/security/spring-security.xsd "  
How spring validates the XML

Spring defaults to loading an XSD file to validate the XML file at startup, so if there is a time when the network is broken, or if some open source software switches the domain name, it is easy to run into the application. I remember the time when Oracle acquired Sun company.

To prevent this, spring provides a mechanism to load XSD files locally by default. Open Spring-context-3.2.0.release.jar, you can see there are two special files:

Spring.handlers

Http\://www.springframework.org/schema/context=org.springframework.context.config.  Contextnamespacehandlerhttp\://www.springframework.org/schema/jee=org.springframework.ejb.config.  Jeenamespacehandlerhttp\://www.springframework.org/schema/lang=org.springframework.scripting.config.  Langnamespacehandlerhttp\://www.springframework.org/schema/task=org.springframework.scheduling.config.  Tasknamespacehandlerhttp\://www.springframework.org/schema/cache=org.springframework.cache.config.  Cachenamespacehandler         

Spring.schemas

Http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5. Xsdhttp\://www.springframework.org/schema/context/spring-context- 3.0.xsd=org/springframework/context/config/spring-context-3.0.xsdhttp\ ://www.springframework.org/schema/context/spring-context-3.1.xsd=org /springframework/context/config/spring-context-3.1.xsdhttp\:// Www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context /config/spring-context-3.2.xsdhttp\://www.springframework.org/schema /context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd ... 

Open the org/springframework/context/config/directory in the jar package and you can see the following

Spring-context-2.5.xsd
Spring-context-3.0.xsd
Spring-context-3.1.xsd
Spring-context-3.2.xsd

It is obvious that spring is putting the XSD file locally, and then doing a mapping in spring.schemas, giving preference to the XSD file from the local Riga.

And spring is very sweet, the old version of the XSD file is also released. This prevents the upgrade of the spring version, and the configuration file is used in the old version of the XSD file, and then broken network, the application can not start.

We can also see that the current version of the XSD file is used when the version number is not configured:

Http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/ spring-context-3.2.xsd 

Again, we open the Dubbo jar package, and can see the configuration in its Spring.schemas file:

Http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=meta-inf/dubbo.xsd  

Therefore, when spring loads the Dubbo, it loads the dubbo.xsd from the Dubbo jar.

How do I skip Spring's XML checksum?

You can skip the checksum in this way:

New Genericxmlapplicationcontext (); context.setvalidating (false);
How to write a spring XML namespace extension of your own

You can refer to the spring documentation, which is actually quite simple. As long as the implementation of their own namespacehandler, and then configure the Spring.handlers and Spring.schemas can be.

Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/extensible-xml.html

Some of the other things to prevent XSD loading is not a successful idea

http://hellojava.info/?p=135

A complete list of spring's namespace

Http://stackoverflow.com/questions/11174286/spring-xml-namespaces-how-do-i-find-what-are-the-implementing-classes-behind-t

Spring Core
  • aop-AopNamespaceHandler
  • c-SimpleConstructorNamespaceHandler
  • cache-CacheNamespaceHandler
  • context-ContextNamespaceHandler
  • jdbc-JdbcNamespaceHandler
  • jee-JeeNamespaceHandler
  • jms-JmsNamespaceHandler
  • lang-LangNamespaceHandler
  • mvc-MvcNamespaceHandler
  • oxm-OxmNamespaceHandler
  • p-SimplePropertyNamespaceHandler
  • task-TaskNamespaceHandler
  • tx-TxNamespaceHandler
  • util-UtilNamespaceHandler
Spring Security
    • security-SecurityNamespaceHandler
    • oauth-OAuthSecurityNamespaceHandler
Spring Integration
  • int-IntegrationNamespaceHandler
  • amqp-AmqpNamespaceHandler
  • event-EventNamespaceHandler
  • feed-FeedNamespaceHandler
  • file-FileNamespaceHandler
  • ftp-FtpNamespaceHandler
  • gemfire-GemfireIntegrationNamespaceHandler
  • groovy-GroovyNamespaceHandler
  • http-HttpNamespaceHandler
  • ip-IpNamespaceHandler
  • jdbc-JdbcNamespaceHandler
  • jms-JmsNamespaceHandler
  • jmx-JmxNamespaceHandler
  • mail-MailNamespaceHandler
  • redis-RedisNamespaceHandler
  • rmi-RmiNamespaceHandler
  • script-ScriptNamespaceHandler
  • security-IntegrationSecurityNamespaceHandler
  • sftp-SftpNamespaceHandler
  • stream-StreamNamespaceHandler
  • twitter-TwitterNamespaceHandler
  • ws-WsNamespaceHandler
  • xml-IntegrationXmlNamespaceHandler
  • xmpp-XmppNamespaceHandler
Summarize:

Why not configure the version number on the XSD in the spring configuration?
Because if the version number is not configured, the XSD file in the current jar is taken, which reduces the risk.
And so the convention is more elegant than the way it is configured.

Reference:

Http://stackoverflow.com/questions/10768873/spring-di-applicationcontext-xml-how-exactly-is-xsischemalocation-used

Http://stackoverflow.com/questions/11174286/spring-xml-namespaces-how-do-i-find-what-are-the-implementing-classes-behind-t

Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/extensible-xml.html

Why not configure the version number of the XSD file in spring configuration

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.