Spring P標籤的使用,springp標籤

來源:互聯網
上載者:User

Spring P標籤的使用,springp標籤

       Spring的p標籤是基於XML Schema的配置方式,目的是為了簡化配置方式。由於Spring的p標籤是spring內建的,只要在xml頭部申明下就可以調用。如下:

<?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>


       從 2.0開始,Spring支援使用名稱空間的可擴充配置格式。這些名稱空間都是基於一種XML Schema定義。事實上,我們所看到的所有bean的配置格式都是基於一個 XML Schema文檔。特定的名稱空間並不需要定義在一個XSD檔案中,它只在Spring核心中存在。我們所說的p名稱空間就是這樣,它不需要一個schema定義,與我們前面採用<property/>元素定義bean的屬性不同的是,當我們採用了p名稱空間,我們就可以在bean元素中使用屬性(attribute)來描述bean的property值。具體操作請看以下樣本。
【轉載使用,請註明出處:http://blog.csdn.net/mahoking】

       本例設計對象Topic、Speech和Speaker。具體實現如下:
Topic

public class Topic {/**內容   必須提供     getter 與 setter 方法*/public String context;public String getContext() {return context;}public void setContext(String context) {this.context = context;}/** * 有參的建構函式 ,可選 * @param context */public Topic(String context) {this.context = context;}/** * 無參數的建構函式  , 必須提供一個無參的建構函式 */public Topic() {}}

Speech

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


Speaker

public class Speaker {/* 必須提供 getter 與 setter 方法 */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;}/** * 演講 */public void speech() {System.out.println(toString());}@Overridepublic String toString() {return "Speaker [name=" + name + ", highTopic="+ highTopic.getContext() + ", speech=" + speech.getContext()+ ", timeHour=" + timeHour + "]";}}

       根據以上對象代碼,在不使用Spring的p標籤時,相關的配置如下。

<?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>

      P標籤的出現,旨在簡化配置,以下是使用P標籤的設定檔,對比之後就會顯而易見。

<?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>

       從上面的bean定義中,我們採用p名稱空間的方式包含了叫name、timeHour的屬性,而Spring會知道我們的bean包含了一個屬性(property)定義。我們前面說了,p名稱空間是不需要schema定義的,因此屬性(attribute)的名字就是你bean的property的名字。而第二個bean定義則採用p:topic-ref="highTopic"屬性(attribute)的方式達到了同樣的目的。在這個例子中,"topic"是屬性(property)名,而"-ref“則用來說明該屬性不是一個具體的值而是對另外一個bean的引用。

【轉載使用,請註明出處:http://blog.csdn.net/mahoking】




 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.