Using Spring's namespace p assembly properties-excerpt from the Spring Combat (3rd edition)

Source: Internet
Author: User

Assembling values and references for bean properties using the <property> element is not too complex. However, Spring's namespace P provides a way to assemble another bean property without the need to configure so many angle brackets.

The schema URL for the namespace P is http://www.springframework.org/schema/p. If you want to use the namespace p, simply add the following statement to the XML configuration of spring:

<? XML version= "1.0" encoding= "UTF-8" ?> <  xmlns= "Http://www.springframework.org/schema/beans"            xmlns:p= "http ://www.springframework.org/schema/p "            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 " >

(The above link may be wrong for hand, please check when copying)

With this declaration, we can now use P: as the prefix for all attributes of the <bean> element to assemble the bean's properties.

To demonstrate, I used the property to declare a bean and use the namespace p to declare a bean to compare it.

Property :<BeanID= "Kenny"class= "Com.springinaction.springidol.Instrumentalist">    < Propertyname= "Song"value= "Jingle Bells"/>    < Propertyname= "instrument"ref= "Piano"/></Bean>P Namespace:<BeanID= "Kenny"class= "Com.springinaction.springidol.Instrumentalist"P:song= "Jingle Bells"P:instrument-ref= "Piano"/>

The value of the P:song property is set to "Jingle Bells", which will be used to assemble the song property. Similarly, the value of the P:instrument-ref property is set to "Piano", a bean with ID piano is used to assemble the instrument attribute, and the-ref suffix is used as an identity to tell spring to assemble a reference rather than a literal.

Select <property> or namespace p depending on you, they are equivalent. The main advantage of the namespace P is that it is more concise.

Using Spring's namespace p assembly properties-excerpt from the Spring Combat (3rd edition)

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.