欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > 《Spring Framework实战》11:4.1.4.2.详细的依赖和配置2

《Spring Framework实战》11:4.1.4.2.详细的依赖和配置2

2025/10/2 4:13:36 来源:https://blog.csdn.net/daqiang012/article/details/145064872  浏览:    关键词:《Spring Framework实战》11:4.1.4.2.详细的依赖和配置2
欢迎观看《Spring Framework实战》视频教程

          1. 空字符串值

Spring将属性等的空参数视为空字符串。以下基于XML的配置元数据片段将电子邮件属性设置为空字符串值(“”)。

<bean class="ExampleBean">

<property name="email" value=""/>

</bean>

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

Java

exampleBean.setEmail("");

<null/>元素处理空值。以下列表显示了一个示例:

<bean class="ExampleBean">

<property name="email">

<null/>

</property>

</bean>

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

Java

exampleBean.setEmail(null);

          1. 带有p-namespace的XML快捷方式

p-namespace允许您使用bean元素的属性(而不是嵌套的<property/>元素)来描述协作bean的属性值,或两者兼而有之。

Spring支持基于XML模式定义的命名空间的可扩展配置格式。本章讨论的bean配置格式在XML模式文档中定义。但是,p-namespace没有在XSD文件中定义,只存在于Spring的核心中。

以下示例显示了解析为相同结果的两个XML片段(第一个使用标准XML格式,第二个使用p-namespace):

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

该示例显示了bean定义中p-namespace中名为email的属性。这个命令告诉Spring包含一个属性声明。如前所述,p-namespace没有模式定义,因此您可以将属性的名称设置为属性名称。

下一个例子包括另外两个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>

此示例不仅包括使用p-namespace的属性值,还使用特殊格式声明属性引用。第一个bean定义使用<property name=“spouse”ref=“jane”/>创建从bean john到bean jane的引用,而第二个bean定义则使用p:spouse ref=“jane”作为属性来执行完全相同的操作。在这种情况下,配偶是属性名,而-ref部分表示这不是一个直接的值,而是对另一个bean的引用。

p-namespace不如标准XML格式灵活。例如,声明属性引用的格式与以Ref结尾的属性冲突,而标准XML格式则不冲突。我们建议您仔细选择您的方法,并将其传达给您的团队成员,以避免同时生成使用所有三种方法的XML文档。

          1. 带有c-namespace的XML快捷方式

与具有p-namespace的XML快捷方式类似,Spring 3.1中引入的c-namespace允许内联属性来配置构造函数参数,而不是嵌套构造函数参数元素。

以下示例使用c:namespace执行与基于构造函数的依赖注入相同的操作:

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

c:namespace使用与p:one(bean引用的尾随-ref)相同的约定,按名称设置构造函数参数。同样,它需要在XML文件中声明,即使它没有在XSD模式中定义(它存在于Spring核心中)。

对于构造函数参数名称不可用的罕见情况(通常是在编译字节码时没有-parameters标志的情况下),您可以回退到参数索引,如下所示:

<!-- c-namespace index declaration -->

<bean id="beanOne" class="x.y.ThingOne" c:_0-ref="beanTwo" c:_1-ref="beanThree"

c:_2="something@somewhere.com"/>

由于XML语法,索引表示法需要前导_的存在,因为XML属性名称不能以数字开头(即使某些IDE允许这样做)。相应的索引符号也可用于<constructor arg>元素,但并不常用,因为声明的简单顺序通常就足够了。

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

          1. 复合属性名称

设置bean属性时,可以使用复合或嵌套属性名,只要路径中除最终属性名之外的所有组件都不为空。考虑以下bean定义:

<bean id="something" class="things.ThingOne">

<property name="fred.bob.sammy" value="123" />

</bean>

something bean有一个fred属性,这个属性有一个bob属性,还有一个sammy属性,最后的sammy属性被设置为值123。为了实现这一点,在构造bean后,something的fred属性和fred的bob属性不能为null。否则,将抛出NullPointerException。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词