Spring Framework Documentation (5.3.10)
Core | IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP. |
1. The IoC Container
1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)
1.2. Container Overview (容器概览)
1.4.1. Dependency Injection(依赖注入)
1.4.2. Dependencies and Configuration in Detail(依赖与配置详细介绍)
1.4.2.1. Straight Values (Primitives, Strings, and so on)(直接值(原始类型、字符串等))
1.4.2.2. References to Other Beans (Collaborators) (引用其他bean(协作者))
1.4.2.5. Null and Empty String Values(Null和空字符串值)
1.4.2.6. XML Shortcut with the p-namespace(p命名空间的XML快捷方式)
1.4.2.7. XML Shortcut with the c-namespace(c-namespace的XML快捷方式)
1.4.2.8. Compound Property Names(复合属性名)
1.4.3. Using depends-on(使用depends-on)
1.4.4. Lazy-initialized Beans(延迟初始化Bean)
1.4.5. Autowiring Collaborators(自动装配协作者)
下载此文档精编完整版
No. | 内容 | 下载地址 | 文档内容目录 |
1 | 中英双语精编版 第一部分 | PDF下载 | 内容目录 |
2 | 中英双语精编版 第二部分 | PDF下载 | 内容目录 |
3 | 中文精编版 第一部分 | PDF下载 | 内容目录 |
4 | 中文精编版 第二部分 | PDF下载 | 内容目录 |
更多章节内容,请点击查看: Core Technologies
1.4.2.6. XML Shortcut with the p-namespace(p命名空间的XML快捷方式)
The p-namespace lets you use the bean
element’s attributes (instead of nested <property/>
elements) to describe your property values collaborating beans, or both.
p命名空间允许您使用bean元素的属性(而不是嵌套的<property/>元素)来描述您的属性值协作bean(property values collaborating bean),或者二者。
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 Schema定义。本章讨论的beans
配置格式(configuration format)是在XML Schema文档中定义的。但是,p命名空间没有在XSD文件中定义,只存在于Spring的核心(core of Spring)中。
The following example shows two XML snippets (the first uses standard XML format and the second uses the p-namespace) that resolve to the same result:
以下示例展示了两个解析为相同结果的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
https://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>
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.
This next example includes two more bean definitions that both have a reference to another bean:
该示例展示了bean定义中名为email的p命名空间(p-namespace)中的一个属性。它告诉Spring包含一个属性声明。如前所述,p-namespace没有schema定义,因此可以将属性名(name of the attribute)设置为属性名(property name)。
下一个示例包括另外两个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
https://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>
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-namespace的属性值,还使用特殊格式声明属性引用(property reference)。第一个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 |
| p命名空间(p-namespace)不如标准XML格式灵活。例如,用于声明属性引用的格式与以Ref结尾的属性冲突,而标准XML格式则不冲突。我们建议您仔细选择您的方法,并将此告知您的团队成员,以避免生成同时使用这三种方法的XML文档。 |