Spring 依赖和配置的细节-06 Null and 空字符串

Null and 空字符串

Strings将属性的空参数视为空字符串。下面基于XML的元数据配置就会将email 属性配置为String的值。

<bean class="ExampleBean">
    <property name="email" value=""/>
</bean>

上面的示例等效于以下Java代码:

exampleBean.setEmail("");

<null/>将被处理为null值。以下清单显示了一个示例:

<bean class="ExampleBean">
    <property name="email">
        <null/>
    </property>
</bean>

上述配置等同于以下Java代码:

exampleBean.setEmail(null);

使用p命名空间简化XML配置

p命名空间让开发者可以使用bean的属性,而不必使用嵌套的元素。

Spring是支持基于XML的格式化命名空间扩展的。本节讨论的beans 配置都是基于XML的,p命名空间是定义在Spring Core中的(不是在XSD文件)。

以下示例显示了两个XML片段(第一个使用标准XML格式,第二个使用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">

    <bean name="classic" class="com.example.ExampleBean">
        <property name="email" value="someone@somewhere.com"/>
    </bean>

    <bean name="p-namespace" class="com.example.ExampleBean"
        p:email="someone@somewhere.com"/>
</beans>

上面的例子在bean中定义了email的属性。这种定义告知Spring这是一个属性声明。如前面所描述的,p命名空间并没有标准的定义模式,所以开发者可以将属性的名称配置为依赖名称。

下一个示例包括另外两个bean定义,它们都引用了另一个bean:

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

    <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"
        p:spouse-ref="jane"/>

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

此示例不仅包含使用p命名空间的属性值,还使用特殊格式来声明属性引用。第一个bean定义使用来创建从bean john到bean jane的引用, 而第二个bean定义使用<property p:spouse-ref="jane">来作为指向bean的引用。在这个例子中 spouse是属性的名字,而-ref部分表名这个依赖不是直接的类型,而是引用另一个bean。

p命名空间并不如标准XML格式灵活。例如,声明属性的引用可能和一些以Ref结尾的属性相冲突,而标准的XML格式就不会。Spring团队推荐开发者能够和团队商量一下,协商使用哪一种方式,而不要同时使用三种方法。

使用c命名空间简化XML

与 p命名空间类似,c命名空间是在Spring 3.1首次引入的,c命名空间允许使用内联的属性来配置构造参数而不必使用constructor-arg 。

以下示例使用c:命名空间的例子来执行与基于Constructor的依赖注入相同的操作:

<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="thingOne" class="x.y.ThingTwo"/>
    <bean id="thingTwo" class="x.y.ThingThree"/>

    <!-- traditional declaration -->
    <bean id="thingOne" class="x.y.ThingOne">
        <constructor-arg ref="thingTwo"/>
        <constructor-arg ref="thingThree"/>
        <constructor-arg value="something@somewhere.com"/>
    </bean>

    <!-- c-namespace declaration -->
    <bean id="thingOne" class="x.y.ThingOne" c:thingTwo-ref="thingTwo" c:thingThree-ref="thingThree" c:email="something@somewhere.com"/>

</beans>

c::命名空间使用了和p: :命名空间相类似的方式(使用了-ref 来配置引用).而且,同样的,c命名空间也是定义在Spring Core中的(不是XSD模式).

在少数的例子之中,构造函数的参数名字并不可用(通常,如果字节码没有debug信息的编译),你可以使用回调参数的索引,如下面的例子:

<!-- c-namespace index declaration -->
<bean id="thingOne" class="x.y.ThingOne" c:_0-ref="thingTwo" c:_1-ref="thingThree"/>

由于XML语法,索引表示法需要使用_作为属性名字的前缀,因为XML属性名称不能以数字开头(即使某些IDE允许它)。

实际上,构造函数解析机制在匹配参数方面非常有效,因此除非您确实需要,否则我们建议在整个配置中使用名称表示法。

组合属性名

开发者可以配置混合的属性,只需所有的组件路径(除了最后一个属性名字)不能为null即可。参考如下定义:

<bean id="something" class="things.ThingOne">
    <property name="fred.bob.sammy" value="123" />
</bean>

something有 fred的属性,而其中fred属性有bob属性,而bob属性之中有sammy属性,那么最后这个sammy属性会配置为123。 想要上述的配置能够生效,fred属性需要有bob属性而且在fred构造之后不为null即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值