Spring中的p标签(转)

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的引用。