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.7. XML Shortcut with the c-namespace(c-namespace的XML快捷方式)
Similar to the XML Shortcut with the p-namespace, the c-namespace, introduced in Spring 3.1, allows inlined attributes for configuring the constructor arguments rather then nested constructor-arg
elements.
The following example uses the c:
namespace to do the same thing as the from Constructor-based Dependency Injection:
与具有p-namespace的XML快捷方式(XML Shortcut with the p-namespace)类似,Spring 3.1中引入的c-namespace允许通过内联属性来配置构造函数参数,而不是嵌套的constructor-arg
元素。
以下示例使用c:命名空间执行与基于构造函数的依赖注入(Constructor-based Dependency Injection)相同的操作:
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>
The c:
namespace uses the same conventions as the p:
one (a trailing -ref
for bean references) for setting the constructor arguments by their names. Similarly, it needs to be declared in the XML file even though it is not defined in an XSD schema (it exists inside the Spring core).
c:
命名空间使用与p:one(bean引用具有-ref尾部)相同的约定,通过名称设置构造函数参数。类似地,它需要在XML文件中声明,即使它没有在XSD schema中定义(它存在于Spring核心(Spring core)中)。
For the rare cases where the constructor argument names are not available (usually if the bytecode was compiled without debugging information), you can use fallback to the argument indexes, as follows:
罕见情况下,构造函数参数名不可用(通常是在字节码编译时没有调试信息),可以使用参数索引的回退(fallback),如下所示:
<!-- c-namespace index declaration -->
<bean id="beanOne" class="x.y.ThingOne" c:_0-ref="beanTwo" c:_1-ref="beanThree"
c:_2="something@somewhere.com"/>
| Due to the XML grammar, the index notation requires the presence of the leading |
| 由于XML语法的原因,索引表示法(index notation)要求出现前导‘_’,因为XML属性名称不能以数字开头(即使某些IDE允许)。对于< |
In practice, the constructor resolution mechanism is quite efficient in matching arguments, so unless you really need to, we recommend using the name notation through-out your configuration.
实际上,构造函数解析机制在匹配参数方面非常有效,因此除非您真的需要,否则我们建议在整个配置中使用名称表示法(name notation)。