Spring P标签的使用

来源:互联网 发布:监控视频显示无网络 编辑:程序博客网 时间:2024/05/22 15:02

       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/beanshttp://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值。具体操作请看以下示例。
【转载使用,请注明出处:http://blog.csdn.net/mahoking

       本例设计对象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 {@Overridepublic 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());}@Overridepublic 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/beanshttp://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/beanshttp://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的引用。

【转载使用,请注明出处:http://blog.csdn.net/mahoking




 

 

1 0
原创粉丝点击