Spring 配置文件中 Bean 的 property 属性使用示例

在 Spring 配置文件中,beans 元素是 spring-beans 内容模型的根结点,bean 元素是 beans 元素的子节点,通常 bean 元素用于定义 JavaBean。而 bean 元素包含以下几种子元素,它们分别是:

  • constructor-arg 元素
  • property 元素
  • lookup-method 元素
  • replace-method 元素

在 Spring 配置文件中,用户可以通过 Bean 的属性 property 进行参数注入。使用 property 属性不但可以将 String、int 等字面值注入到 Bean 中,还可以将集合、Map 等类型注入到 Bean 中,此外还可以注入其他的 Bean。

(1)字面值:一般是指可用字符串表示的值,这些值可通过 <value> 元素标签进行注入。在默认情况下,基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式。


(2)引用其他的 Bean:Spring IoC 容器中定义的 Bean 可以互相引用,IoC 容器则充当了介绍人的角色。<ref> 元素可以通过 bean、local、parent 三个属性引用其他 Bean 的属性,其中 bean 可以引用统一配置文件中或者父容器中的 Bean,local 只能引用同一配置文件中的 Bean,parent 只能引用父容器中的 Bean。


 
 
  1. public class Boss {
  2. private Car car;
  3. public void setCar(Car car) {
  4. this.car = car;
  5. }
  6. }
  • 1

 
 
  1. <bean id= "car" class= "***" />
  2. <bean id="boss" class="***">
  3. <property name="car">
  4. <ref bean="car"> </ref>
  5. </property>
  6. </bean
  • 1

(3)内部 Bean:当 Spring IoC 容器中的 bean1 只会被 bean2 引用,而不会被容器中任何其他 Bean 引用的时候,则可以将这个 bean1 以内部 Bean 的方式注入到 bean2 中。跟 Java 中的内部类是相似的。


 
 
  1. <bean id= "boss" class= "***">
  2. <property name="car">
  3. <bean class="***">
  4. <property name="price" value="200">
  5. </bean>
  6. </property>
  7. </bean>
  • 1


(4)null 值:有些时候,需要为某个 bean 的属性注入一个 null 值,在这里我们需要使用专用的 <null/> 元素标签,通过它可以为其他对象的属性注入 null 值。

 
 
  1. <bean id= "car" class= "***">
  2. <property name="brand">
  3. <null/>
  4. </property>
  5. < /bean>
  • 1


(5)级联属性:跟 Struts、Hibernate 等框架一样,Spring 支持级联属性的配置,例如当我们希望在定义 bean1 时直接为 bean2 的属性提供注入值,则可以采取以下的配置方式:(boss.getCar().setBrand())


 
 
  1. public class Boss {
  2. private Car car;
  3. public void setCar(Car car) {
  4. this.car = car;
  5. }
  6. }
  • 1

 
 
  1. <bean id= "boss" class= "***">
  2. <property name="car.brand">
  3. <value>奔驰E级 </value>
  4. </property>
  5. < /bean>
  • 1


(6)集合类型属性:java.util 包中的集合类是最常用的数据结构类型,主要包括 List、Set、Map 以及 Properties,Spring 为这些集合类型属性提供了专门的配置元素标签:
①  当属性为 java.util.List 的时候,


 
 
  1. public class Boss {
  2. private List favorites = new ArrayList();
  3. public List getFavorites() {
  4. return favrites;
  5. }
  6. public void setFavorites(List favrites) {
  7. this.favrites = favorites;
  8. }
  9. }
  • 1

 
 
  1. <bean id= "boss" class= "***">
  2. <property name="favorites">
  3. <list>
  4. <value>唱歌 </value>
  5. <value>运动 </value>
  6. <value>读书 </value>
  7. </list>
  8. </property>
  9. < /bean>
  • 1
② 当属性为 java.util.Set 的时候,

 
 
  1. public class Boss {
  2. private Set favorites = new ArrayList();
  3. public Set getFavorites() {
  4. return favrites;
  5. }
  6. public void setFavorites(Set favrites) {
  7. this.favrites = favorites;
  8. }
  9. }
  • 1

 
 
  1. <bean id= "boss" class= "***">
  2. <property name="favorites">
  3. <set>
  4. <value>唱歌 </value>
  5. <value>运动 </value>
  6. <value>读书 </value>
  7. </set>
  8. </property>
  9. < /bean>
  • 1
③ 当属性为 java.util.Map 的时候,

 
 
  1. public class Boss {
  2. private Map favorites;
  3. public Map getFavorites() {
  4. return favrites;
  5. }
  6. public void setFavorites(Map favrites) {
  7. this.favrites = favorites;
  8. }
  9. }
  • 1

 
 
  1. <bean id= "boss" class= "***">
  2. <property name="favorites">
  3. <map>
  4. <entry>
  5. <key> <value>key01 </value> </key>
  6. <value>唱歌 </value>
  7. </entry>
  8. <entry>
  9. <key> <value>key02 </value> </key>
  10. <value>运动 </value>
  11. </entry>
  12. <entry>
  13. <key> <ref bean="keyBean" /> </key>
  14. <ref bean="valueBean" />
  15. </entry>
  16. </map>
  17. </property>
  18. </bean>
  • 1
④ 当属性为 java.util.Properties 的时候,可以看做是属性为 Map 的一个特例,Properties 属性的键值只能是字符串,

 
 
  1. public class Boss {
  2. private Properties favorites;
  3. public Properties getFavorites() {
  4. return favrites;
  5. }
  6. public void setFavorites(Properties favrites) {
  7. this.favrites = favorites;
  8. }
  9. }
  • 1

 
 
  1. <bean id="boss" class="***">
  2. <property name="favorites">
  3. <props>
  4. <prop key="p01">唱歌 </prop>
  5. <prop key="p02">运动 </prop>
  6. <prop key="p03">读书 </prop>
  7. </props>
  8. </properties>
  9. </property>
  10. </bean>
  • 1
⑤ 强类型结合:根据 JDK5.0 提供的强类型集合功能,在配置文件中,允许为集合元素指定类型:

 
 
  1. public class Boss {
  2. private Map<Integer, String> favorites;
  3. public Map getFavorites() {
  4. return favrites;
  5. }
  6. public void setFavorites(Map favrites) {
  7. this.favrites = favorites;
  8. }
  9. }
  • 1

 
 
  1. <bean id= "boss" class= "***">
  2. <property name="favorites">
  3. <map>
  4. <entry>
  5. <key> <value>101 </value> </key>
  6. <value>唱歌 </value>
  7. </entry>
  8. </map>
  9. </property>
  10. < /bean>
  • 1

⑥集合合并:配置文件中的集合合并的功能,允许子 <bean> 集成父 <bean> 的同名属性集合元素,并将子 <bean> 中配置的集合属性值和父 <bean> 中配置的同名属性值合并,作为最终 <bean> 的属性值,


 
 
  1. <bean id="parentBoss" abstract="true" class="***">
  2. <property name="favorites">
  3. <set>
  4. <value>唱歌 </value>
  5. <value>运动 </value>
  6. <value>读书 </value>
  7. </set>
  8. </property>
  9. </bean>
  10. <bean id="childBoss" parent="parentBoss">
  11. <property name="favorites">
  12. <set merge="true">
  13. <value>旅游 </value>
  14. <value>睡觉 </value>
  15. </set>
  16. </property>
  17. </bean>
  • 1


(7)简化配置方式:Spring 为字面值、引用 Bean 和集合都提供了相对简化的配置方式。

(8)自动装配:就是不再使用 ref 进行手工装配 Bean,这种方式可以减少配置文件的代码量,但是在大型项目中,不推荐使用,容易混乱。

①autowire="byName"

②autowire="byType"

③autowire="constructor"

④autowire="autodetect"

<beans> 元素标签中的 default-autowire 属性可以配置全局自动装配,其属性的默认值为 no,标志不启用自动装配;在 <beans> 中定义的自动装配策略可以被 <bean> 的自动装配策略覆盖。




                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
                </div>
</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值