Null and Empty String Values
空字符串值
Spring treats empty arguments for properties and the like as empty Strings
. The following XML-based configuration metadata snippet sets the email
property to the empty String
value ("").
Spring将属性等的空参数视为空字符串。以下基于XML的配置元数据片段将email属性设置为空字符串值(“”)。
<bean class="ExampleBean">
<property name="email" value=""/>
</bean>
The preceding example is equivalent to the following Java code:
前面的示例等效于以下Java代码:
exampleBean.setEmail("");
The <null/>
element handles null
values. The following listing shows an example:
<null/>元素处理空值。下面的列表显示了一个示例:
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
The preceding configuration is equivalent to the following Java code:
前面的配置相当于以下Java代码:
exampleBean.setEmail(null);
XML Shortcut with the p-namespace
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-namespace允许您使用bean
元素的属性(而不是嵌套的<property/>
元素)来描述协作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模式定义。本章中讨论的bean配置格式是在XML模式文档中定义的。但是,p-namespace不是在XSD文件中定义的,它只存在于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.
该示例显示了p名称空间中bean定义中名为email
的属性。这告诉Spring包含一个属性声明。如前所述,p-namespace没有模式定义,因此可以将属性的名称设置为属性名称。
This next example includes two more bean definitions that both have a reference to another bean:
下一个示例包括另外两个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命名空间的属性值,而且还使用一种特殊的格式来声明属性引用。第一个bean定义使用<property name=“spose”ref=“jane”/>
来创建一个从bean john到bean jane的引用,而第二个bean定义使用p:spoise-ref=“jane”
作为一个属性来执行相同的操作。在本例中,spoose是属性名,而-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-namespace不如标准XML格式灵活。例如,声明属性引用的格式与以Ref结尾的属性冲突,而标准XML格式则不冲突。我们建议您谨慎地选择您的方法,并与您的团队成员进行沟通,以避免生成同时使用这三种方法的XML文档。 |
XML Shortcut with the c-namespace
带有c命名空间的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.
与p-namespace的XML快捷方式类似,spring3.1中引入的c-namespace允许内联属性来配置构造函数参数,而不是嵌套的构造函数arg元素。
The following example uses the c:
namespace to do the same thing as the from Constructor-based Dependency Injection:
以下示例使用c:命名空间执行与基于from构造函数的依赖项注入相同的操作:
<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).
命名空间使用与p:
one(bean引用的尾部-ref)相同的约定,用于按名称设置构造函数参数。类似地,它需要在XML文件中声明,即使它没有在XSD模式中定义(它存在于Spring内核中)。
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:
对于构造函数参数名不可用的少数情况(通常,如果编译字节码时没有调试信息),可以使用回退到参数索引,如下所示:
<!-- 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 _
, as XML attribute names cannot start with a number (even though some IDEs allow it). A corresponding index notation is also available for <constructor-arg>
elements but not commonly used since the plain order of declaration is usually sufficient there.
由于XML语法的原因,索引表示法要求出现前导的U,因为XML属性名不能以数字开头(即使某些IDE允许这样做)。相应的索引表示法也可用于<constructor-arg>
元素,但不常用,因为声明的简单顺序通常就足够了。
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.
实际上,构造函数解析机制在匹配参数方面非常有效,因此除非您确实需要,否则我们建议您在整个配置中使用名称表示法。
Compound Property Names
复合属性名称
You can use compound or nested property names when you set bean properties, as long as all components of the path except the final property name are not null
. Consider the following bean definition:
在设置bean属性时,可以使用复合属性名或嵌套属性名,只要路径中除最终属性名之外的所有组件都不为空。考虑下面的bean定义:
<bean id="something" class="things.ThingOne">
<property name="fred.bob.sammy" value="123" />
</bean>
The something
bean has a fred
property, which has a bob
property, which has a sammy
property, and that final sammy
property is being set to a value of 123
. In order for this to work, the fred
property of something
and the bob
property of fred
must not be null
after the bean is constructed. Otherwise, a NullPointerException
is thrown.
something bean有一个fred属性,它有一个bob属性,还有一个sammy属性,最后一个sammy属性被设置为123。为了使其工作,在构建bean之后,something的fred属性和fred的bob属性不能为null。否则,将引发NullPointerException。
下一节:Using
depends-on
标签