When I first started using the spring framework, for its profile XML, it just learned how to use it on the web, and it doesn't know what it means. Recently thinking about the root of the inquiry into the bottom. The following are the main contents of this article:
1. Examples of configuration files.
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans xmlns="Http://www.springframework.org/schema/beans"
- xmlns:mvc="Http://www.springframework.org/schema/mvc"
- xmlns:tx="Http://www.springframework.org/schema/tx"
- xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
- xmlns:context="Http://www.springframework.org/schema/context"
- 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
- Http://www.springframework.org/schema/context
- Http://www.springframework.org/schema/context/spring-context.xsd
- Http://www.springframework.org/schema/mvc
- Http://www.springframework.org/schema/mvc/spring-mvc.xsd
- Http://www.springframework.org/schema/tx
- Http://www.springframework.org/schema/tx/spring-tx.xsd
- Http://www.springframework.org/schema/aop
- Http://www.springframework.org/schema/aop/spring-aop.xsd "
- default-autowire="byname">
- </Beans>
The above is the first part of our most commonly used applicationcontext.xml configuration file, and many times we just know that these must be added, but why, it is not very clear what to add. We know that spring will validate the XML document when it is launched, which is used to verify the correctness of the XML document syntax of the configuration file. Let's take a look at how to validate XML document validation.
2. XML Schema constraint
First, the XML document schema constraint, which defines the structure of the XML document, content and syntax, including elements and attributes, the structure of the relationship and data types, and so on. Here are a few things to follow:
1), all tags and attributes need to be defined by schema. (the schema itself is defined by the consortium).
2), all schema files need to be an ID, here we call namespace, its value is a URL, usually the address of this XML XSD file.
3), the namespace value is specified by the TargetNamespace property
4), introduce a schema constraint, using the attribute xmlns, the attribute value is the corresponding schema file namespace NameSpace.
5), if the introduction of the schema is defined by the non-organization, you must specify the location of the schema file, the location of the schema file is specified by schemalocation.
6), the introduction of multiple schema files requires the use of aliases. The alias form is as follows: Xmlns:alias.
3, detailed interpretation of the above configuration file
1), Declare the default namespace (xmlns= "Http://www.springframework.org/schema/beans")
2), declare the XML Schema instance namespace (http://www.w3.org/2001/XMLSchema-instance), and bind the xsi prefix to the namespace so that the pattern processor can identify the xsi: The SchemaLocation property. The prefix of the XML schema instance namespace is typically used XSI
3), use the Xsi:schemalocation property to specify the namespace (Http://www.springframework.org/schema/beans) and the mode location (HTTP/ WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPRING-BEANS.XSD) related. The XML Schema recommendation standard indicates that the Xsi:schemalocation property can be used on any element in the instance, not necessarily the root element, but the Xsi:schemalocation property must appear before any elements and attributes it wants to validate.
4), using aliases to introduce multiple schema files. such as the above Xmlns:mvc, XMLNS:TX, Xmlns:context and so on.
4. What are the drawbacks of using an XSD file with a version number? Let's start by looking at how spring validates XML documents. Spring defaults to loading an XSD file to validate the XML file at startup, so if there is some time off the net, or some open source software to switch the domain name, then it is very easy to touch the application can not start, in order to prevent this situation, Spring provides a mechanism to load the XSD file from the local default. Open Spring-context-4.1.2.release.jar, you can see there are two special files: Spring.handlers as follows:
[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 as follows:
[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-4.0.xsd=org/springframework/context/config/ Spring-context-4.0.xsd
- http\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/ Spring-context-4.1.xsd
- http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/ Spring-context-4.1.xsd
- Http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd
- Http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
- Http\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd
- Http\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd
- Http\://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd
- Http\://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd
- Http\://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
- Http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
- Http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
- Http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd
- Http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd
- Http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd
- Http\://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd
- Http\://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd
- Http\://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
- Http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
- Http\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd
- Http\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd
- Http\://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd
- Http\://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd
- Http\://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
- Http\://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
- Http\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd
- Http\://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd
- Http\://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd
- Http\://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd
- Http\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.1.xsd
Then open the jar in the org/springframework/context/config/directory, you can see the following is very obvious, you can think of spring is to put the XSD file locally, Again in the Spring.schemas to do a mapping, priority to download XSD files from the local. 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. As you can see from the XML document above, when the version number is not configured, the current version of the XML Schema document is used.
- http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/ Spring-context-4.1.xsd
This is part of a description of the validation of XML documents in the spring configuration document, which I ignored during my learning process. There are some video tutorials and a blog post why in spring configuration it is best not to configure the version number of the XSD file. This article is reproduced from the Quartzhao column
RSS Subscription
Schema constraints for XML documents in spring