spring-p命名空间 c命名空间

p命名空间
1. 需要引入命名空间p 并不真实存在 是在spring内部中实现的
xmlns:p="http://www.springframework.org/schema/p"
2. 如果是引用注入需要加-ref后缀 p和c命名空间 并不适用于集合参数

<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" <!-- 1 -->
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean name="john-classic" class="com.example.Person">
      <property name="name" value="John Doe"/>
      <property name="spouse" ref="jane"/>
  </bean>

  <bean name="john-modern"
      class="com.example.Person"
      p:name="John Doe" <!-- 2 -->
      p:spouse-ref="jane"/> <!-- 3 -->

  <bean name="jane" class="com.example.Person">
      <property name="name" value="Jane Doe"/>
  </bean>
</beans>

c命名空间
1. 需要引入命名空间c 并不真实存在 是在spring内部中实现的
xmlns:c="http://www.springframework.org/schema/c"
2.可以使用c:参数名 或c:_参数index 两种方式
因为xml命名空间中属性名不能以数字开头 所以加了_
3. 如果是引用注入需要加-ref后缀

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:c="http://www.springframework.org/schema/c" <!-- 1 -->
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="bar" class="x.y.Bar"/>
  <bean id="baz" class="x.y.Baz"/>

  <-- 'traditional' declaration -->
  <bean id="foo" class="x.y.Foo">
      <constructor-arg ref="bar"/>
      <constructor-arg ref="baz"/>
      <constructor-arg value="foo@bar.com"/>
  </bean>

  <-- 'c-namespace' declaration --> <!-- 2 -->
  <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com">

  <-- 'c-namespace' index declaration --> <!-- 3 -->
  <bean id="foo2" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz">

</beans>

 

转载于:https://my.oschina.net/dajianguo/blog/1093914

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值