Use of Spring P labels, springp labels

Source: Internet
Author: User

Use of Spring P labels, springp labels

Spring p labels are configured based on XML Schema to simplify the configuration method. Since the p tag of Spring is built in spring, it can be called as long as it is declared in the xml header. As follows:

<?xml version="1.0" encoding="UTF-8"?><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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"></beans>


Starting from 2.0, Spring supports the namespace scalable configuration format. These namespaces are all defined based on an XML Schema. In fact, the configuration format of all beans we see is based on an XML Schema document. A specific namespace does not need to be defined in an XSD file. It only exists in the Spring kernel. The p namespace we call is like this. It does not need a schema definition. What is different from the attribute defined by bean using the <property/> element above, when p is used, we can use attribute in bean elements to describe bean property values. For more information, see the following example.
[For more information, see http://blog.csdn.net/mahoking]

In this example, the design objects are Topic, Speech, and Speaker. The specific implementation is as follows:
Topic

Public class Topic {/** content must provide the getter and setter Methods */public String context; public String getContext () {return context;} public void setContext (String context) {this. context = context;}/*** constructor with parameters. Optional * @ param context */public Topic (String context) {this. context = context;}/*** a constructor without parameters. A constructor without parameters must be provided */public Topic (){}}

Speech

public class Speech extends Topic {@Overridepublic void setContext(String context) {super.context = context;}}


Speaker

Public class Speaker {/* must provide the getter and setter Methods */private String name; private Topic highTopic; private Speech speech; private int timeHour; public Speech getSpeech () {return speech ;} public void setSpeech (Speech speech) {this. speech = speech;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Topic getTopic () {return highTopic;} public void setTopic (Topic highTopic) {this. highTopic = highTopic;} public int getTimeHour () {return timeHour;} public void setTimeHour (int timeHour) {this. timeHour = timeHour;}/*** speech */public void speech () {System. out. println (toString () ;}@ Overridepublic String toString () {return "Speaker [name =" + name + ", highTopic =" + highTopic. getContext () + ", speech =" + speech. getContext () + ", timeHour =" + timeHour + "]" ;}}

According to the above object code, when the p label of Spring is not used, the related configuration is as follows.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="highSpeaker01" class="com.mahaochen.spring.high.learn01.Speaker"><property name="name" value="lily" /><property name="highTopic" ref="highTopic" /><property name="speech" ref="highSpeech" /><property name="timeHour" value="2" /></bean><bean id="highTopic" class="com.mahaochen.spring.high.learn01.Topic"p:context="heppy new year 2015!" /><bean id="highSpeech" class="com.mahaochen.spring.high.learn01.Speech"p:context="Welcome to 2015!" /></beans>

The P tag is designed to simplify the configuration. The following is the configuration file using the P tag, which will be obvious after comparison.

<?xml version="1.0" encoding="UTF-8"?><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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="highSpeaker01" class="com.mahaochen.spring.high.learn01.Speaker"p:name="lily" p:topic-ref="highTopic" p:speech-ref="highSpeech"p:timeHour="2" /><bean id="highTopic" class="com.mahaochen.spring.high.learn01.Topic"p:context="heppy new year 2015!" /><bean id="highSpeech" class="com.mahaochen.spring.high.learn01.Speech"p:context="Welcome to 2015!" /></beans>

From the bean definition above, we use the p namespace method to include the attributes named name and timeHour, and Spring will know that our bean contains a property definition. As we mentioned earlier, the p namespace does not need schema definition. Therefore, the attribute name is the name of your bean property. The second bean definition adopts the p: topic-ref = "highTopic" attribute method to achieve the same purpose. In this example, "topic" is the property name, and "-ref" is used to indicate that this property is not a specific value but a reference to another bean.

[For more information, see http://blog.csdn.net/mahoking]




 

 

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.