首页 > 编程技术 > java

Spring P标签的使用详解

发布时间:2021-8-17 00:00

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

具体实现如下:

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() {
 } 
}

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

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"/>

即可~

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

标签:[!--infotagslink--]

您可能感兴趣的文章: