The spring namespace does not require a version number

Source: Internet
Author: User
Tags xml parser

Why Dubbo boot no problem?

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:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= "http://code.alibabatech.com/ Schema/dubbo "
  4. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans.xsd
  6. Http://code.alibabatech.com/schema/dubbo
  7. 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:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans.xsd ">
  5. </Beans>

We can also add the version number to the following:

[HTML]View PlainCopy
    1. 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:

[HTML]View PlainCopy
    1. xmlns:mvc="Http://www.springframework.org/schema/mvc"
    2. 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:

[HTML]View PlainCopy
    1. <? XML version= "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans/spring-beans.xsd"
    3. 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:

[HTML]View PlainCopy
    1. Xsi:schemalocation= "Http://www.springframework.org/schema/beans
    2. Http://www.springframework.org/schema/beans/spring-beans.xsd
    3. Http://www.springframework.org/schema/security
    4. 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

[Plain]View PlainCopy
    1. Http\://www.springframework.org/schema/context=org.springframework.context.config.contextnamespacehandler
    2. Http\://www.springframework.org/schema/jee=org.springframework.ejb.config.jeenamespacehandler
    3. Http\://www.springframework.org/schema/lang=org.springframework.scripting.config.langnamespacehandler
    4. Http\://www.springframework.org/schema/task=org.springframework.scheduling.config.tasknamespacehandler
    5. Http\://www.springframework.org/schema/cache=org.springframework.cache.config.cachenamespacehandler

Spring.schemas

[Plain]View PlainCopy
    1. http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/ Spring-context-2.5.xsd
    2. http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/ Spring-context-3.0.xsd
    3. http\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/ Spring-context-3.1.xsd
    4. http\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/ Spring-context-3.2.xsd
    5. http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/ Spring-context-3.2.xsd
    6. ...

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:

[HTML]View PlainCopy
    1. 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:

[HTML]View PlainCopy
    1. 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:

[HTML]View PlainCopy
    1. Genericxmlapplicationcontext context = new Genericxmlapplicationcontext ();
    2. 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

Ext.: http://blog.csdn.net/hengyunabc/article/details/22295749

The spring namespace does not require a version number

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.