Spring设置属性 <property> && p命名空间 、 util命名空间

本文介绍了Spring中如何使用<property>元素设置属性,以及更简洁的p命名空间替代方法。通过启用p命名空间,可以更方便地装配bean引用和字面量值。此外,还讲解了利用util命名空间中的<util:list>等元素简化集合装配的技巧,如创建list、map和set类型的bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<bean  id= "cdPlay"  class = "soundsystem.CDPlay">

  <property name="compactDisc"  red="compactDisc" />

</bean>

<property>元素是为属性的Setter方法所提供的功能,Spring提供了简洁的p命名空间来替代<property>元素。

为启用p命名空间,需要在xml头部声明

<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd>
...
</beans>

<bean id = "cdPlayer"  class="soundsystem.CDPlay" 

          p:compactDisc-ref = "compactDisc" />

p-命名空间中属性所遵循的命名规则和c-命名空间属性类型:

p:属性名-ref ="所注入的beanId"   //-ref:注入bean引用,告诉spring这里装配的是引用,而不是字面量值

将字面量注入到属性中

<bean id = "cdPlayer"  class="soundsystem.CDPlay" >

   <property  name = "title"  value = "this is title name value">

</bean>

p命名空间

<bean id = "cdPlayer"  class="soundsystem.CDPlay" 

         p:title = "this is title name value"

        p:artist  =  "this is artist value">

   <property  name ="属性名">

       <list>

               <value>this  first value </value>

               <value>this second value</value>

      </list>

    <property>

</bean>

与c命名空间一样,装配bean引用与装配字面量的唯一区别在于是否带有"-ref"后缀,如果没有"-ref"后缀,所装配的就是字面量。

虽然不能使用p-命名空间来装配集合,但是可以使用Spring util-命名空间中的一些功能来简化。

首先,在XML中声明util-命名空间及其模式

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util             
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd>
...
</beans>

util-命名空间所提供的功能之一就是<util:list>元素,它会创建一个列表的bean:

<util:list id="listId">

   <value> this is one value</value>

   <value> this is two value</value>

</util:list>

<bean id = "compactDisc" class="soundsystem.BlankDisc"

            p:title="this is title name"

            p:artist = "this is a vale"

            p:tracks-ref="listId" />    //该属性名为tracks,依赖上面util:list所创建的list bean .

util-命名空间的其他元素:
<util:constant>                    引用某个类型的public  static域,并将其暴露为bean

<util:list>                              创建一个java.util.list的bean,其中包含值和引用

<util:map>                           创建一个java.util.map类型的bean,其中包含值和引用

<util:properties>                  创建一个java.util.properties类型的bean

<util:property-path>            创建一个ben的属性(或内嵌属性),并将其暴露

<util:set>                             创建一个java.util.Set类型的bean,其中包含值和引用

第二部分代码:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- bean配置:Car类型,id为car1,car1的brand属性值为"大众",price属性值为210000,speed属性值为60 --> <!-- ===============begin=============== --> <bean id="car1" class="step1.Car"> <property name="brand" value="大众"></property> <property name="price" value="210000"></property> <property name="speed" value="60"></property> </bean> <!-- ================end================ --> <!-- bean配置:Car类型,id为car2,car2的brand属性值为"HAVAL",price属性值为130000,speed属性值为100 --> <!-- ===============begin=============== --> <!-- ================end================ --> <!-- bean配置:Address类型,id为address1,address1的city属性值为"成都",street属性值为"三岔街道" --> <!-- ===============begin=============== --> <bean id="address1" class="step1.Address"> <property name="city" value="成都"></property> <property name="street" value="三岔街道"></property> </bean> <!-- ================end================ --> <!-- bean配置:Map类型,id为addressMap,添加一个元素,key为1,value-ref为address1 --> <!-- ===============begin=============== --> <!-- ================end================ --> <!-- bean配置:Person类型,id为person,person的name属性值为"鲁班七号", cars的列表中包含car1car2两个元素, addresses属性值为上面的Map对象addressMap--> <!-- ===============begin=============== --> <!-- ================end================ --> </beans>根据第一第二部分代码补充完整
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三丶竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值