Spring P標(biāo)簽的使用詳解
Spring P標(biāo)簽的使用
Spring的p標(biāo)簽是基于XML Schema的配置方式,目的是為了簡(jiǎn)化配置方式。由于Spring的p標(biāo)簽是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開(kāi)始,Spring支持使用名稱(chēng)空間的可擴(kuò)展配置格式。這些名稱(chēng)空間都是基于一種XML Schema定義。事實(shí)上,我們所看到的所有bean的配置格式都是基于一個(gè) XML Schema文檔。
特定的名稱(chēng)空間并不需要定義在一個(gè)XSD文件中,它只在Spring內(nèi)核中存在。我們所說(shuō)的p名稱(chēng)空間就是這樣,它不需要一個(gè)schema定義,與我們前面采用<property/>元素定義bean的屬性不同的是,當(dāng)我們采用了p名稱(chēng)空間,我們就可以在bean元素中使用屬性(attribute)來(lái)描述bean的property值。具體操作請(qǐng)看以下示例。
本例設(shè)計(jì)對(duì)象Topic、Speech和Speaker
具體實(shí)現(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; } /** * 有參的構(gòu)造函數(shù) ,可選 * @param context */ public Topic(String context) { this.context = context; } /** * 無(wú)參數(shù)的構(gòu)造函數(shù) , 必須提供一個(gè)無(wú)參的構(gòu)造函數(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ù)以上對(duì)象代碼,在不使用Spring的p標(biāo)簽時(shí),相關(guān)的配置如下。
<?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標(biāo)簽的出現(xiàn),旨在簡(jiǎn)化配置,以下是使用P標(biāo)簽的配置文件,對(duì)比之后就會(huì)顯而易見(jiàn)。
<?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名稱(chēng)空間的方式包含了叫name、timeHour的屬性,而Spring會(huì)知道我們的bean包含了一個(gè)屬性(property)定義。
我們前面說(shuō)了,p名稱(chēng)空間是不需要schema定義的,因此屬性(attribute)的名字就是你bean的property的名字。而第二個(gè)bean定義則采用p:topic-ref="highTopic"屬性(attribute)的方式達(dá)到了同樣的目的。
在這個(gè)例子中,"topic"是屬性(property)名,而"-ref“則用來(lái)說(shuō)明該屬性不是一個(gè)具體的值而是對(duì)另外一個(gè)bean的引用。
spring配置p標(biāo)簽問(wèn)題
今天學(xué)習(xí)spring遇到這樣的一個(gè)問(wèn)題
spring中使用p標(biāo)簽代替<property> 以用來(lái)達(dá)到簡(jiǎn)化的目的
但是如果在
<bean id="test" class="User" p:name="wangxiaoer" p:list-ref="phone1"> <property name="list.brand" value="Huawei"/> </bean>
這樣直接修改list中的屬性 是會(huì)報(bào)錯(cuò)的 因?yàn)槭褂昧藀標(biāo)簽的bean中 他的執(zhí)行順序是先執(zhí)行property標(biāo)簽中的內(nèi)容 而這里 因?yàn)橄葓?zhí)行了list.brand 這個(gè)屬性 對(duì)其修改 而這個(gè)對(duì)象還沒(méi)有引用 即get出來(lái) 所以直接修改是會(huì)報(bào)錯(cuò)的 主要原因是spring并不會(huì)幫我們自動(dòng)創(chuàng)建所需要的對(duì)象 在這里使用即為null
解決方法如下
依然使用property 的方法 先進(jìn)行引用 再對(duì)其中的值進(jìn)行修改
... <property name="phone" ref="phone1"/> <property name="phone.brand" value="Huawei"/>
即可~
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot啟動(dòng)類(lèi)@SpringBootApplication注解背后的秘密
這篇文章主要介紹了SpringBoot啟動(dòng)類(lèi)@SpringBootApplication注解背后的秘密,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12mybatis實(shí)現(xiàn)mapper代理模式的方式
本文向大家講解mybatis的mapper代理模式,以根據(jù)ide值查詢(xún)單條數(shù)據(jù)為例編寫(xiě)xml文件,通過(guò)mapper代理的方式進(jìn)行講解增刪改查,分步驟給大家講解的很詳細(xì),對(duì)mybatis mapper代理模式相關(guān)知識(shí)感興趣的朋友一起看看吧2021-06-06spring @Validated 注解開(kāi)發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了spring @Validated 注解開(kāi)發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05SpringBoot自定義定時(shí)任務(wù)的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot自定義定時(shí)任務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解
這篇文章主要介紹了Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Java使用String類(lèi)格式化當(dāng)前日期實(shí)現(xiàn)代碼
這篇文章主要介紹了Java使用String類(lèi)格式化當(dāng)前日期實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02SpringBoot項(xiàng)目的漏洞修復(fù)經(jīng)驗(yàn)分享
在局域網(wǎng)環(huán)境下,由于無(wú)法連接外網(wǎng)下載Maven包,常見(jiàn)解決方案是在外網(wǎng)環(huán)境搭建相同的開(kāi)發(fā)環(huán)境以便更新Maven包,本次漏洞掃描包括Tomcat、jackson-databind、fastjson、logback等組件,通常解決方法是升級(jí)到更高版本2024-10-10