国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Spring中P標簽的使用方法

發(fā)布時(shí)間:2021-09-04 11:55 來(lái)源:億速云 閱讀:0 作者:chen 欄目: 開(kāi)發(fā)技術(shù)

這篇文章主要講解了“Spring中P標簽的使用方法”,文中的講解內容簡(jiǎn)單清晰,易于學(xué)習與理解,下面請大家跟著(zhù)小編的思路慢慢深入,一起來(lái)研究和學(xué)習“Spring中P標簽的使用方法”吧!

目錄
  • Spring P標簽的使用

    • 本例設計對象Topic、Speech和Speaker

  • spring配置p標簽問(wèn)題

    • 今天學(xué)習spring遇到這樣的一個(gè)問(wèn)題

    • 解決方法如下

Spring P標簽的使用

Spring的p標簽是基于XML Schema的配置方式,目的是為了簡(jiǎn)化配置方式。由于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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

從 2.0開(kāi)始,Spring支持使用名稱(chēng)空間的可擴展配置格式。這些名稱(chēng)空間都是基于一種XML Schema定義。事實(shí)上,我們所看到的所有bean的配置格式都是基于一個(gè) XML Schema文檔。

特定的名稱(chēng)空間并不需要定義在一個(gè)XSD文件中,它只在Spring內核中存在。我們所說(shuō)的p名稱(chēng)空間就是這樣,它不需要一個(gè)schema定義,與我們前面采用<property/>元素定義bean的屬性不同的是,當我們采用了p名稱(chēng)空間,我們就可以在bean元素中使用屬性(attribute)來(lái)描述bean的property值。具體操作請看以下示例。

本例設計對象Topic、Speech和Speaker

具體實(shí)現如下:

  • 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;
 }
 
 /**
  * 無(wú)參數的構造函數  , 必須提供一個(gè)無(wú)參的構造函數
  */
 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 + "]";
 }
}

根據以上對象代碼,在不使用Spring的p標簽時(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標簽的出現,旨在簡(jiǎn)化配置,以下是使用P標簽的配置文件,對比之后就會(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)的方式達到了同樣的目的。

在這個(gè)例子中,"topic"是屬性(property)名,而"-ref“則用來(lái)說(shuō)明該屬性不是一個(gè)具體的值而是對另外一個(gè)bean的引用。

spring配置p標簽問(wèn)題

今天學(xué)習spring遇到這樣的一個(gè)問(wèn)題

spring中使用p標簽代替<property> 以用來(lái)達到簡(jiǎn)化的目的

但是如果在

<bean id="test" class="User" p:name="wangxiaoer"  p:list-ref="phone1">
<property name="list.brand" value="Huawei"/>
</bean>

這樣直接修改list中的屬性 是會(huì )報錯的 因為使用了p標簽的bean中 他的執行順序是先執行property標簽中的內容 而這里 因為先執行了list.brand 這個(gè)屬性 對其修改 而這個(gè)對象還沒(méi)有引用 即get出來(lái) 所以直接修改是會(huì )報錯的 主要原因是spring并不會(huì )幫我們自動(dòng)創(chuàng )建所需要的對象 在這里使用即為null

解決方法如下

依然使用property 的方法 先進(jìn)行引用 再對其中的值進(jìn)行修改

...
<property name="phone" ref="phone1"/>
<property name="phone.brand" value="Huawei"/>

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

免费吃奶摸下激烈视频| 超H公用妓女精便器系列小说| 男人放进女人阳道入口| 人妻无码一区二区不卡无码AV| 国产亚洲成AV人片在线观看导航| 中文字幕美人妻亅U乚一596|