Spring P標簽的使用詳解
Spring P標簽的使用
Spring的p標簽是基于XML Schema的配置方式,目的是為了簡化配置方式。由于Spring的p標簽是spring內(nèi)置的,只要在xml頭部申明下就可以調(diào)用。如下:
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
從 2.0開始,Spring支持使用名稱空間的可擴展配置格式。這些名稱空間都是基于一種XML Schema定義。事實上,我們所看到的所有bean的配置格式都是基于一個 XML Schema文檔。
特定的名稱空間并不需要定義在一個XSD文件中,它只在Spring內(nèi)核中存在。我們所說的p名稱空間就是這樣,它不需要一個schema定義,與我們前面采用<property/>元素定義bean的屬性不同的是,當我們采用了p名稱空間,我們就可以在bean元素中使用屬性(attribute)來描述bean的property值。具體操作請看以下示例。
本例設計對象Topic、Speech和Speaker
具體實現(xiàn)如下:
Topic
public class Topic {
/**內(nèi)容 必須提供 getter 與 setter 方法*/
public String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
/**
* 有參的構造函數(shù) ,可選
* @param context
*/
public Topic(String context) {
this.context = context;
}
/**
* 無參數(shù)的構造函數(shù) , 必須提供一個無參的構造函數(shù)
*/
public Topic() {
}
}
Speech
public class Speech extends Topic {
@Override
public 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());
}
@Override
public String toString() {
return "Speaker [name=" + name + ", highTopic="
+ highTopic.getContext() + ", speech=" + speech.getContext()
+ ", timeHour=" + timeHour + "]";
}
}
根據(jù)以上對象代碼,在不使用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/beans http://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標簽的出現(xiàn),旨在簡化配置,以下是使用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/beans http://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的引用。
spring配置p標簽問題
今天學習spring遇到這樣的一個問題
spring中使用p標簽代替<property> 以用來達到簡化的目的
但是如果在
<bean id="test" class="User" p:name="wangxiaoer" p:list-ref="phone1"> <property name="list.brand" value="Huawei"/> </bean>
這樣直接修改list中的屬性 是會報錯的 因為使用了p標簽的bean中 他的執(zhí)行順序是先執(zhí)行property標簽中的內(nèi)容 而這里 因為先執(zhí)行了list.brand 這個屬性 對其修改 而這個對象還沒有引用 即get出來 所以直接修改是會報錯的 主要原因是spring并不會幫我們自動創(chuàng)建所需要的對象 在這里使用即為null
解決方法如下
依然使用property 的方法 先進行引用 再對其中的值進行修改
... <property name="phone" ref="phone1"/> <property name="phone.brand" value="Huawei"/>
即可~
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot啟動類@SpringBootApplication注解背后的秘密
這篇文章主要介紹了SpringBoot啟動類@SpringBootApplication注解背后的秘密,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn)
這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
Java使用String類格式化當前日期實現(xiàn)代碼
這篇文章主要介紹了Java使用String類格式化當前日期實現(xiàn)代碼,需要的朋友可以參考下2014-02-02

