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
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans xmlns="Http://www.springframework.org/schema/beans"
- xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 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:
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans 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:
[HTML]View PlainCopy
- 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
- 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:
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans 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:
[HTML]View PlainCopy
- 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
[Plain]View PlainCopy
- Http\://www.springframework.org/schema/context=org.springframework.context.config.contextnamespacehandler
- Http\://www.springframework.org/schema/jee=org.springframework.ejb.config.jeenamespacehandler
- Http\://www.springframework.org/schema/lang=org.springframework.scripting.config.langnamespacehandler
- Http\://www.springframework.org/schema/task=org.springframework.scheduling.config.tasknamespacehandler
- Http\://www.springframework.org/schema/cache=org.springframework.cache.config.cachenamespacehandler
Spring.schemas
[Plain]View PlainCopy
- http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/ Spring-context-2.5.xsd
- http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/ Spring-context-3.0.xsd
- http\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/ Spring-context-3.1.xsd
- http\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/ Spring-context-3.2.xsd
- http\://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:
[HTML]View PlainCopy
- 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
- 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
- Genericxmlapplicationcontext context = 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
Ext.: http://blog.csdn.net/hengyunabc/article/details/22295749
The spring namespace does not require a version number