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]