Schema constraints for XML documents in spring

Source: Internet
Author: User
Tags aliases aop validate xml document

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.
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:mvc="Http://www.springframework.org/schema/mvc"
  4. xmlns:tx="Http://www.springframework.org/schema/tx"
  5. xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
  6. xmlns:context="Http://www.springframework.org/schema/context"
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xsi:schemalocation= "
  9. Http://www.springframework.org/schema/beans
  10. Http://www.springframework.org/schema/beans/spring-beans.xsd
  11. Http://www.springframework.org/schema/context
  12. Http://www.springframework.org/schema/context/spring-context.xsd
  13. Http://www.springframework.org/schema/mvc
  14. Http://www.springframework.org/schema/mvc/spring-mvc.xsd
  15. Http://www.springframework.org/schema/tx
  16. Http://www.springframework.org/schema/tx/spring-tx.xsd
  17. Http://www.springframework.org/schema/aop
  18. Http://www.springframework.org/schema/aop/spring-aop.xsd "
  19. default-autowire="byname">
  20. </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
    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 as follows: [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-4.0.xsd=org/springframework/context/config/ Spring-context-4.0.xsd
  6. http\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/ Spring-context-4.1.xsd
  7. http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/ Spring-context-4.1.xsd
  8. Http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd
  9. Http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
  10. Http\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd
  11. Http\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd
  12. Http\://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd
  13. Http\://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd
  14. Http\://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
  15. Http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
  16. Http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
  17. Http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd
  18. Http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd
  19. Http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd
  20. Http\://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd
  21. Http\://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd
  22. Http\://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
  23. Http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
  24. Http\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd
  25. Http\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd
  26. Http\://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd
  27. Http\://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd
  28. Http\://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
  29. Http\://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
  30. Http\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd
  31. Http\://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd
  32. Http\://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd
  33. Http\://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd
  34. 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.
    1. 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

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.