编程知识 cdmana.com

Spring configuration is best not to configure XSD version number!

author : Hengyun duanling https://blog.csdn.net/hengyunabc/article/details/22295749

Why? dubbo There's no problem starting ?

This article blog From a question :

Our company made Ali's dubbo, It's been months since I hung up , Why is there no problem starting our app ? Our applications are Spring There is a similar configuration in the configuration file :

<?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 Spring At startup, it is necessary to verify XML Of documents . Or why Eclipse in xml No error prompt ?

Like this one Spring To configure :

<?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">

We can also add the version number after it :

xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

XML Some of the concepts of

So first of all xml Some of the concepts of :

xml Of schema Are there in namespace, You can give it an alias . For example, the common spring Of namespace:

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

Usually ,namespace Corresponding URI It's a storage XSD The address of , Although there is no such requirement in the specification .

If not provided schemaLocation, that Spring Of xml The parser will start from namespace Of URI Load in XSD file . We can change the configuration file to look like this , It can also work normally :

<?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 xml namespace To the corresponding XSD A mapping of files , So we can see , stay xsi:schemaLocation The strings configured later are paired , The one in front is namespace Of URI, And then xsd Of documents URI. such as :

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"

**Spring How to check XML Of ? **

Spring The default is to load XSD File to verify xml Of documents , So if you're disconnected sometimes , Or some open source software switches domain names , So it's easy to run into applications that can't be started . I remember that time Oracle Acquisition Sun Company time , I've been in this situation .

To prevent this ,Spring Provides a mechanism , Load locally by default XSD file . open spring-context-3.2.0.RELEASE.jar, You can see that there are two special files inside :

spring.handlers

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

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
...

And on again jar In the bag org/springframework/context/config/ Catalog , You can see below

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

Obviously , You can think of Spring It's a XSD The files have been put locally , And then spring.schemas There's a mapping in , Loading from local is preferred XSD file .

also Spring Very intimate. , Put the old version of XSD The papers are all out . This will prevent upgrades Spring edition , And the configuration file is still the old version of XSD file , And then it was disconnected , The app won't start .

We can still see that , When no version number is configured , It's the current version of XSD file :

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

Again , We turn on dubbo Of jar package , It can be in its spring.schemas You can see this configuration in the file :

http://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd

therefore ,Spring In the load dubbo when , From dubbo Of jar Load in dubbo.xsd.

How to skip Spring Of XML check ?

You can skip validation in this way :

GenericXmlApplicationContext context = new GenericXmlApplicationContext();context.setValidating(false);

How to write your own spring xml namespace Expand ?

You can refer to Spring Documents , It's actually quite simple . Just realize your own NamespaceHandler, Configure it again spring.handlers and spring.schemas That's all right. .

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

Other things

prevent XSD One idea that failed to load

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

Complete Spring Of namespace A list of

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

summary :

Why not in Spring In the configuration of , Configuration XSD Version number of ?

Because if you don't configure the version number , Take the current jar Inside XSD file , It reduces all kinds of risks . And it's elegant to have more conventions than configuration .

Recent hot article recommends :

1.Java 15 Official release , 14 A new feature , Refresh your mind !!

2. Finally, I got it through open source projects IntelliJ IDEA Activation code , It's delicious !

3. I use Java 8 Wrote a piece of logic , I can't understand it , You try ..

4. To hang up Tomcat ,Undertow It's very powerful !!

5.《Java Development Manual ( Song Mountain version )》 The latest release , Download it quickly !

I think it's good , Don't forget to like it + Forward !

版权声明
本文为[Java technology stack]所创,转载请带上原文链接,感谢

Scroll to Top