加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:06:16 所属栏目:百科 来源:网络整理
导读:p-命名空间 p-命名空间使用bean元素属性替代内嵌 property 元素,用来描述属性值或者协作类。 p命名空间并不是在XSD文件中,而是存在于Spring核心中。 下面XML片段解释了:1使用了标准XML,第2个使用p-命名空间 beans xmlns = "http://www.springframework.or

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匹配方式配置。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读