目录
  • spring p标签的使用
    • 本例设计对象topic、speech和speaker

spring p标签的使用

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/beans
http://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值。具体操作请看以下示例。

本例设计对象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 {
 
 @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标签时,相关的配置如下。

<?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标签的出现,旨在简化配置,以下是使用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中 他的执行顺序是先执行property标签中的内容 而这里 因为先执行了list.brand 这个属性 对其修改 而这个对象还没有引用 即get出来 所以直接修改是会报错的 主要原因是spring并不会帮我们自动创建所需要的对象 在这里使用即为null

解决方法如下

依然使用property 的方法 先进行引用 再对其中的值进行修改

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

即可~

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。