经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Spring P标签的使用详解
来源:jb51  时间:2021/8/16 18:51:30  对本文有异议

Spring P标签的使用

Spring的p标签是基于XML Schema的配置方式,目的是为了简化配置方式。由于Spring的p标签是spring内置的,只要在xml头部申明下就可以调用。如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd">
  7. </beans>

从 2.0开始,Spring支持使用名称空间的可扩展配置格式。这些名称空间都是基于一种XML Schema定义。事实上,我们所看到的所有bean的配置格式都是基于一个 XML Schema文档。

特定的名称空间并不需要定义在一个XSD文件中,它只在Spring内核中存在。我们所说的p名称空间就是这样,它不需要一个schema定义,与我们前面采用<property/>元素定义bean的属性不同的是,当我们采用了p名称空间,我们就可以在bean元素中使用属性(attribute)来描述bean的property值。具体操作请看以下示例。

本例设计对象Topic、Speech和Speaker

具体实现如下:

  • Topic
  1. public class Topic {
  2. /**内容 必须提供 getter 与 setter 方法*/
  3. public String context;
  4. public String getContext() {
  5. return context;
  6. }
  7. public void setContext(String context) {
  8. this.context = context;
  9. }
  10. /**
  11. * 有参的构造函数 ,可选
  12. * @param context
  13. */
  14. public Topic(String context) {
  15. this.context = context;
  16. }
  17. /**
  18. * 无参数的构造函数 , 必须提供一个无参的构造函数
  19. */
  20. public Topic() {
  21. }
  22. }
  • Speech
  1. public class Speech extends Topic {
  2. @Override
  3. public void setContext(String context) {
  4. super.context = context;
  5. }
  6. }
  • Speaker
  1. public class Speaker {
  2. /* 必须提供 getter 与 setter 方法 */
  3. private String name;
  4. private Topic highTopic;
  5. private Speech speech;
  6. private int timeHour;
  7. public Speech getSpeech() {
  8. return speech;
  9. }
  10. public void setSpeech(Speech speech) {
  11. this.speech = speech;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Topic getTopic() {
  20. return highTopic;
  21. }
  22. public void setTopic(Topic highTopic) {
  23. this.highTopic = highTopic;
  24. }
  25. public int getTimeHour() {
  26. return timeHour;
  27. }
  28. public void setTimeHour(int timeHour) {
  29. this.timeHour = timeHour;
  30. }
  31. /**
  32. * 演讲
  33. */
  34. public void speech() {
  35. System.out.println(toString());
  36. }
  37. @Override
  38. public String toString() {
  39. return "Speaker [name=" + name + ", highTopic="
  40. + highTopic.getContext() + ", speech=" + speech.getContext()
  41. + ", timeHour=" + timeHour + "]";
  42. }
  43. }

根据以上对象代码,在不使用Spring的p标签时,相关的配置如下。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="highSpeaker01" class="com.mahaochen.spring.high.learn01.Speaker">
  7. <property name="name" value="lily" />
  8. <property name="highTopic" ref="highTopic" />
  9. <property name="speech" ref="highSpeech" />
  10. <property name="timeHour" value="2" />
  11. </bean>
  12. <bean id="highTopic" class="com.mahaochen.spring.high.learn01.Topic"
  13. p:context="heppy new year 2015!" />
  14. <bean id="highSpeech" class="com.mahaochen.spring.high.learn01.Speech"
  15. p:context="Welcome to 2015!" />
  16. </beans>

P标签的出现,旨在简化配置,以下是使用P标签的配置文件,对比之后就会显而易见。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd">
  7. <bean id="highSpeaker01" class="com.mahaochen.spring.high.learn01.Speaker"
  8. p:name="lily" p:topic-ref="highTopic" p:speech-ref="highSpeech"
  9. p:timeHour="2" />
  10. <bean id="highTopic" class="com.mahaochen.spring.high.learn01.Topic"
  11. p:context="heppy new year 2015!" />
  12. <bean id="highSpeech" class="com.mahaochen.spring.high.learn01.Speech"
  13. p:context="Welcome to 2015!" />
  14. </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> 以用来达到简化的目的

但是如果在

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

这样直接修改list中的属性 是会报错的 因为使用了p标签的bean中 他的执行顺序是先执行property标签中的内容 而这里 因为先执行了list.brand 这个属性 对其修改 而这个对象还没有引用 即get出来 所以直接修改是会报错的 主要原因是spring并不会帮我们自动创建所需要的对象 在这里使用即为null

解决方法如下

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

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

即可~

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号