7.4.2 XML shortcut with the p-namespace

The p-namespace enables you to use the bean element’s attributes, instead of nested <property/> elements, to describe your property values and/or collaborating beans.

p名称空间能够让你使用 bean 元素的属性而不是嵌套的 <property/> 元素去描述你的属性值和/或合作的beans.

Spring supports extensible configuration formats with namespaces, which are based on an XML Schema definition. The beans configuration format discussed in this chapter is defined in an XML Schema document. However, the p-namespace is not defined in an XSD file and exists only in the core of Spring.

Spring通过名称空间来支持可扩展的配置格式,它基于一个XML约束定义.在这章讨论的 beans 配置格式被定义在一个XML约束文档中.然而,p名称空间没有被定义在一个XSD文件中,它只存在于Spirng的核心中.

The following example shows two XML snippets that resolve to the same result: The first uses standard XML format and the second uses the p-namespace.

下面的例子展示了两个解析出相同结果的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="[email protected]"/>
    </bean>

    <bean name="p-namespace" class="com.example.ExampleBean"
        p:email="[email protected]"/>
</beans>

The example shows an attribute in the p-namespace called email in the bean definition. This tells Spring to include a property declaration. As previously mentioned, the p-namespace does not have a schema definition, so you can set the name of the attribute to the property name.

这个例子展示了一个在p名称空间中被叫做email的属性在bean定义中.这里告诉了Spring去包含一个属性声明.综上所述,p名称空间没有约束定义,所以你可以设置属性(attribute)的名称到属性(property)名称.

This next example includes two more bean definitions that both have a reference to another bean:

下面这个例子包含了2个多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>

As you can see, this example includes not only a property value using the p-namespace, but also uses a special format to declare property references. Whereas the first bean definition uses <property name="spouse" ref="jane"/> to create a reference from bean john to bean jane, the second bean definition uses p:spouse-ref="jane" as an attribute to do the exact same thing. In this case spouse is the property name, whereas the -ref part indicates that this is not a straight value but rather a reference to another bean.

作为你看到的,这个例子不仅仅包含了属性值使用p名称空间,还使用了一个特殊格式去声明属性引用.第一个bean定义使用 <property name="spouse" ref="jane"/> 去创建一个引用从bean john 到bean jane,第二个bean定义使用 p:spouse-ref="jane" 作为一个属性去做相同的事情.在这个情况中 spouse 是属性名,反之 -ref 这部分表明这不是一个直接值,而是另一个bean的引用.

The p-namespace is not as flexible as the standard XML format. For example, the format for declaring property references clashes with properties that end in Ref, whereas the standard XML format does not. We recommend that you choose your approach carefully and communicate this to your team members, to avoid producing XML documents that use all three approaches at the same time.

p名称空间并不像标准XML格式一样灵活.例如,这个格式为了声明属性引用冲突通过属性结尾的 Ref,反之标准XML格式不需要.我们建议你小心地使用并和你团队成员进行交流去避免在同一时间通过3中不同的方式去编写XML文档.

猜你喜欢

转载自blog.csdn.net/weixin_41648566/article/details/80852382