首页 > 编程语言 > 详细

Spring中的p标签(转)

时间:2015-05-14 20:09:25      阅读:212      评论:0      收藏:0      [点我收藏+]

Spring的p标签是基于XML Schema的配置方式,目的是为了简化配置方式。

在XML文件头部添加xmlns:p="http://www.springframework.org/schema/p"即可使用。

例如:

类Person

  1. public class Person  
  2. {  
  3.   private int age;  
  4.   private Tool tool;  
  5.   public void setAge(int age)  
  6.   {  
  7.      this.age=age;  
  8.   }  
  9.   public void setTool(Tool tool)  
  10.   {  
  11.      this.tool=tool;  
  12.   }  
  13. 其余代码省略  
  14. ......  
  15. }  

原本的bean配置为

  1. <?xml version="1.0" encoding="GBK"?>  
  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-3.0.xsd">  
  6.     <bean id="person" class="com.myclass.Person">  
  7.         <property name="age" value="21"/>  
  8.         <property name="tool" ref="tool"/>  
  9.     </bean>  
  10. </beans>  


使用P标签的配置为

  1. <?xml version="1.0" encoding="GBK"?>  
  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-3.0.xsd">  
  7.     <bean id="person" class="com.myclass.Person" p:age="21" p:tool-ref="tool"/>  
  8. </beans>  

tool之后添加"-ref"后缀表示是对另外一个bean的引用。

Spring中的p标签(转)

原文:http://www.cnblogs.com/cornucopia/p/4504009.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!