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

前端之家收集整理的这篇文章主要介绍了【Spring学习12】XML简写p-namespace及c-namespace前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

p-命名空间

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

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

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

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

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

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

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

c-命名空间

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

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

c:namespace@H_502_4@重构构造注入

@H_502_10@<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">

    @H_502_10@<bean id="notify" class="twm.spring.start.NotifyServiceByCellPhoneImpl" />
    @H_502_10@<bean id="customer" class="twm.spring.start.Customer">
        @H_502_10@<property name="name" value="陈先生">@H_502_10@</property>
        @H_502_10@<property name="address" value="深圳南山区">@H_502_10@</property>
    @H_502_10@</bean>
    <!-- 普通青年 -->
    @H_502_10@<bean id="order" class="twm.spring.start.Order">
        @H_502_10@<constructor-arg name="customer" ref="customer" />
        @H_502_10@<constructor-arg name="orderno" value="201799777799" />
        @H_502_10@<constructor-arg name="notifyservice" ref="notify" />
    @H_502_10@</bean>
    <!-- 文艺青年 -->
    @H_502_10@<bean id="order2" class="twm.spring.start.Order" c:customer-ref="customer" c:orderno="201799777799" c:notifyservice-ref="notify" />
@H_502_10@</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匹配方式配置。

猜你在找的XML相关文章