Spring uses the P namespace to configure properties

Source: Internet
Author: User
Tags format definition

Another option to "lose weight" to the XML profile is to use the P namespace, which, starting with 2.0, supports extensible configuration format using namespaces. These namespaces are based on an XML schema definition. In fact, all bean of the configuration formats we see are based on an XML schema document.

A specific namespace does not need to be defined in an XSD file, it exists only in the spring kernel. What we call the P-namespace is this, it doesn't need a schema definition, unlike the attributes we used to define the bean in front of it, <property/> when we adopt the P-namespace, we can bean element to describe the property value of the bean using the attribute (attribute).

The following two segments of the XML configuration file are used to define the same bean: one in the standard XML format, and one in the P namespace.

<beans xmlns= "Http://www.springframework.org/schema/beans"    xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "    xmlns:p=" http://www.springframework.org/schema/p "    xsi:schemalocation="/http/ Www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">         <bean name= "classic" class= "Com.example.ExampleBean" >        <property name= "Email" value= "[email protected]/>    </bean>        <bean name= "P-namespace" class= "Com.example.ExampleBean" p:email= "          [ Email protected] "/></beans>

From the bean definition above, we include a property called email in the form of the P namespace, and spring will know that our bean contains a property definition. As we said earlier, the P namespace does not need schema definitions, so the name of the attribute (attribute) is the name of your Bean's property.

This next example includes the more beans definitions that both has a reference to another bean:

The following example contains two bean definitions, all of which refer to another bean

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi:schemalocation = "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" > <bean name= "john-classic" class= "Com.example.Person" > <property name= "name" value= "John Doe"/&        Gt <property name= "spouse" ref= "Jane"/> </bean> <bean name= "John-modern" class= "com.example.Pers        On "p:name=" John Doe "p:spouse-ref=" Jane "/> <bean name=" Jane "class=" Com.example.Person "> <property name= "name" value= "Jane Doe"/> </bean></beans> 

As can see, this example doesn ' t only include a property value using the P-namespace, but also uses a special forma T to declare property references. Whereas the first bean definition Uses <property name= "spouse" ref= "Jane"/>  to Create a reference from Bean john  to bean  jane , the second bean definition Uses p:spouse-ref= "Jane"  as an attribute to do the exact same thing. In this case ' spouse ' was the property name whereas the ' -ref ' p Art indicates that this is not a straight value but rather a reference to another bean.

The above example not only uses the P namespace to contain a property value, but also declares a property reference using a special format. The first bean definition <property name="spouse" ref="jane"/> is used to establish a bean john jane -to-bean reference, and the second bean definition takes the p:spouse-ref="jane" same purpose as the attribute (attribute). In this example, " spouse " is the property name, and " -ref " is used to indicate that the property is not a specific value but a reference to another bean.

Attention

It is important to note that the P namespace is not as flexible as the standard XML format definition, for example, if the Bean's property name is Ref terminated, then the use of the P namespace definition will result in a conflict, and the standard XML format definition will not occur. Here we remind you to decide how to do that in a project or a careful tradeoff, and also to choose three definitions in your project, based on how your team members understand different definitions.

Ext.: http://blog.csdn.net/liaomin416100569/article/details/4924899

Spring uses the P namespace to configure properties

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.