XML configuration file namespace with headers in the spring configuration file

Source: Internet
Author: User
Tags aop xml parser

All along, write the spring configuration file, the other configuration files are copied to the head, up to change the version number, it is not clear what is needed, what the Hell. Tidy up today and refuse to have no brain copy.

one, Spring configuration file Common configuration header

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "http://www.springframework.org/schema/beans"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "http://www.springframework.org/schema/context"XMLNS:AOP= "http://www.springframework.org/schema/aop"Xmlns:tx= "http://www.springframework.org/schema/tx"xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spri Ng-aop-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.x SD ">            <!--the middle is the configuration file section -        <BeanID= "txmanager"class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">        < propertyname= "dataSource"ref= "dataSource" />    </Bean>        <Tx:adviceID= "txadvice"Transaction-manager= "txmanager">        <tx:attributes>            <Tx:methodname= "find*"Propagation= "not_supported" />        </tx:attributes>    </Tx:advice>    <Aop:config>        <Aop:aspectID="***"ref="***"/>        <Aop:pointcutID="***"expression="****" />    </Aop:config>        <!--the middle is the configuration file section -        </Beans>

Focus on the header section of the configuration file, which is written in the <beans > elements Section.   Do you feel familiar and unfamiliar? After careful understanding, will not be so mysterious again.

Ii. what is an XML namespace

An XML namespace is a collection of XML elements and attributes that are identified by an internationalized resource identifier (IRI), which is often referred to as an XML "vocabulary." In xml, the name of the element is defined by the developer, and a naming conflict occurs when two different documents use the same element Name. For a simple chestnut, the namespace is much like a package in Java, where the same class name can be stored under different packages, as long as the Class's package is preceded by the introduction of the class to avoid collisions with the same name class.

Iii. Declaration and use of XML namespaces

The namespace is declared as an attribute of the Element. The namespace is not necessarily declared only in the root element, but can be declared in any element in the XML Document. The scope of the declared namespace begins with the element that declares the namespace and applies to all content of the element until it is overwritten by another namespace declaration with the same prefix name, where the element content refers to the Element's <opening-tag> and </closing-tag The content between >.

For example: the namespace above is declared in the <beans> </beans> element, and all namespaces declared in it are valid in both tags.

xmlns:aop= "http://www.springframework.org/schema/aop"

Xmlns: similar to a reserved word, it is used only to declare namespaces. In other words, xmlns is used to bind a namespace, but it is not itself bound to any namespace.

Aop: This is actually a binding of the prefix "aop" to the namespace "http://www.springframework.org/schema/aop" (the URI contains information about the namespace). Usually we use a shorter or more conventional name as the prefix for the namespace (for example, AOP here), But the exact prefix depends entirely on the individual. the prefix of the custom namespace is Legal. The clarity of XML files is enhanced with meaningful namespace Prefixes. So you can see that we usually configure the spring configuration file, the prefix name is AOP (slice), TX (transaction) and other naming methods.

after a prefix has been configured , we use the namespace prefix as follows:

< Aop:config > <  id= "* * *"  ref= "* * *"/><ID  = "* * *"  expression= "* * *"/></aop:config  >

Here we use the AOP prefix when we configure content for aspect-oriented programming, representing the following elements (config,aspect, Etc.) as defined in Http://www.springframework.org/schema/aop. Note that the prefix is used only as a placeholder and must be interpreted through an XML parser that can recognize the namespace to use the actual namespace bound to the Prefix.

A single default namespace

We see that in the config file, we don't use namespace prefixes for elements such as Beans,bean. It can be cumbersome to repeatedly qualify an element or attribute to be used in a namespace.
In this case, you can declare a default namespace. there can be only one default namespace at any time .
Declaring a default namespace means that if any element in the scope of the default namespace declaration is not explicitly qualified with a prefix, the element is implicitly Qualified. As with a prefixed namespace,
The default namespace can also be Overridden.
The default namespace is declared in the following way:

xmlns= "http://www.springframework.org/schema/beans"

That is, in the namespace scope, not prefixed elements are in this namespace, for example, here <beans> <bean>, etc., because it is more commonly used so let
Declare their namespaces as the default, without having to prefix each Write.

Iv. Spring configuration file Configuration namespace

Spring integrates tools, and spring provides a way to configure XML scheme for a variety of tools, simplifying development. We should also have a clearer idea of the introduction of XML namespaces for various Tools. Spring is to validate the XML file at Startup. If there are elements in the XML space that are not in the namespace, it is an Error. typically, the URI for 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.

For example we can write:

xmlns= "http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"

The default namespace is to load the specified XSD file.

SchemaLocation provides a mapping of an XML namespace to a corresponding XSD (xml Schema Definition) file whose value consists of one or more URI reference Pairs.
Two URIs are separated by a space character (spaces and line breaks are available). The first URI is the value of the defined XML namespace, and the second URI gives the actual location of the schema document,
The schema processor will read the schema document from this location, and the Document's targetnamespace must match the first URI (the value of the XML namespace).

The note here contrasts with the location of the following AOP namespace Uri.

The strings that are configured after xsi:schemalocation are paired, preceded by the URI of the namespace, followed by the XSD file uri;
For example, let's say that we have a naming prefix: xsi, this xsi is declared in 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-3.0.xsd           http://www.springframework.org/schema/context/  http Www.springframework.org/schema/context/spring-context-3.0.xsd           http://www.springframework.org/schema/ AOP  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/ Schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "    

The value of the namespace AOP here is "http://www.springframework.org/schema/aop", which corresponds to the location of the XSD file "http://www.springframework.org/schema/ Aop/spring-aop-3.0.xsd "

We open http://www.springframework.org/schema/aop/spring-aop-3.0.xsd and we can see the values of targetnamespace in the XSD file as well as the values of the Namespaces. As follows:

V. Spring finds XSD file for validating XML

Spring defaults to validating the XML file by loading the XSD file from the location of the configured namespace at startup, so it is easy to run out of applications if there are some times when the network is broken, or if some open source software switches the domain Name.

To prevent this, Spring provides a mechanism to load an XSD file locally by default, which is obtained from the network based on the actual URI when it is not locally available.

We open Spring-aop-4.1.6release.jar (this is my local version), and this package has a Meta_inf folder with two files: spring.handlers and Spring.schemas.




http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/ Spring-aop-2.0.xsdhttp\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config /spring-aop-2.5.xsdhttp\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/ Config/spring-aop-3.0.xsdhttp\://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop /config/spring-aop-3.1.xsdhttp\://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/ Aop/config/spring-aop-3.2.xsdhttp\://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework /aop/config/spring-aop-4.0.xsdhttp\://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/ Springframework/aop/config/spring-aop-4.1.xsdhttp\://www.springframework.org/schema/aop/spring-aop.xsd=org /springframework/aop/config/spring-aop-4.1.xsd

We see an XSD file corresponding to a local path, we open Org/springframework/aop/config/can see:

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 put all the old versions of the XSD file in Spring. 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.

Note the last line that I won the red in spring.schemas, that we can write the XSD file location for the namespace value, without having to write the version number, which defaults to the corresponding XSD version of the local spring related version, which I am 4.1.

Write in Xsi:schemalocation: HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd

V. Summary

There is a clear understanding of the head of the spring configuration File. In the future to write the configuration file will not be a full copy of the Brain. First look at what features are needed, and then import the appropriate Namespaces.

It is best to write the XSD file where the namespace value corresponds, without adding the version number, so that spring loads the XSD file corresponding to the local Version.

of course, You can also let spring skip the validation of xml, specifically: in eclipse, Windows right-click, remove the red arrow tick, you can cancel the validation of the XML file for this workspace:

or, for a separate XML file right-click, validate is Ok.

For files that have been verified, and no modifications have been made. We can cancel the automatic validation of it. For the new configuration file, I think it is best not to cancel the verification of good, after all, we are in the configuration file, there is a validation will let us find the error this morning.

--always feel that they are not very clear, but also need to practice slowly, write more blog, exercise the ability to express writing, there are unclear or wrong in the text of the place welcome message to Discuss.



XML configuration file namespace with headers in the spring configuration file

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.