【Spring学习12】XML简写p-namespace及c-namespace

p-命名空间

p-命名空间使用bean元素属性替代内嵌<property>元素,用来描述属性值或者协作类。
p命名空间并不是在XSD文件中,而是存在于Spring核心中。

下面XML片段解释了:1使用了标准XML,第2个使用p-命名空间

<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/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 1、普通青年 -->
    <bean id="order" class="twm.spring.start.Order">
        <property name="customer.name" value="陈先生" />
        <property name="customer.address" value="深圳南山区" />
        <property name="orderno" value="201799777799"></property>
        <property name="notifyservice" ref="notify2"></property>
    </bean>
    <!-- 2、文艺青年 -->
    <bean id="order2" class="twm.spring.start.Order" p:customer.name="陈先生123"
        p:customer.address="深圳南山区123" p:orderno="201799777799"
        p:notifyservice-ref="notify" />

    <bean id="notify" class="twm.spring.start.NotifyServiceByCellPhoneImpl" />
</beans> 

上例中解释了,在bean定义中使用p-namespace设置email 属性。它告诉Spring这里有一个property声明。前面提到过,p-namespace 并不存在schema定义,所以p可以修改为其他名字。这里改成ppp也行

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:ppp="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 1、普通青年 -->
    <bean id="order" class="twm.spring.start.Order">
        <property name="customer.name" value="陈先生" />
        <property name="customer.address" value="深圳南山区" />
        <property name="orderno" value="201799777799"></property>
        <property name="notifyservice" ref="notify2"></property>
    </bean>
    <!-- 2、文艺青年 -->
    <bean id="order2" class="twm.spring.start.Order" ppp:customer.name="陈先生123"
        ppp:customer.address="深圳南山区123" ppp:orderno="201799777799"
        ppp:notifyservice-ref="notify" />

    <bean id="notify" class="twm.spring.start.NotifyServiceByCellPhoneImpl" />
</beans> 

c-命名空间

p-namespace配合set注入,简写<property>元素的。
同样,配合构造注入,spring推出了c-namespace,允许行内配置构造参数,而不需使用内嵌的<constructor-arg/>元素

beans加入 xmlns:c="http://www.springframework.org/schema/c"

c:namespace重构构造注入

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

    <bean id="notify" class="twm.spring.start.NotifyServiceByCellPhoneImpl" />
    <bean id="customer" class="twm.spring.start.Customer">
        <property name="name" value="陈先生"></property>
        <property name="address" value="深圳南山区"></property>
    </bean>
    <!-- 普通青年 -->
    <bean id="order" class="twm.spring.start.Order">
        <constructor-arg name="customer" ref="customer" />
        <constructor-arg name="orderno" value="201799777799" />
        <constructor-arg name="notifyservice" ref="notify" />
    </bean>
    <!-- 文艺青年 -->
    <bean id="order2" class="twm.spring.start.Order" c:customer-ref="customer" c:orderno="201799777799" c:notifyservice-ref="notify" />
</beans> 

在不知道构造函数的参数名称(比如无源码且编译时无调试信息),怎么办呢?
此时可以用参数索引:

<bean id="order2" class="twm.spring.start.Order" c:_0-ref="customer" c:_1="201799777799" c:_2-ref="notify" />

注意:

由于XML语法,索引标记需要下划线_开头作为XML属性名称,而不能使用数字开头(尽管某些ID支持)

在实际中,构造注入(name匹配/类型匹配/索引匹配)是非常高效的,一般情况下,推荐使用 name匹配方式配置。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值