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。
public class Boss {    private Car car;    public void setCar(Car car) {        this.car = car;    }}<bean id="car" class="***" /><bean id="boss" class="***">  <property name="car">    <ref bean="car"></ref>  </property></bean

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

<bean id="boss" class="***">  <property name="car">    <bean class="***">      <property name="price" value="200">    </bean>  </property></bean>

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

(5)级联属性:跟 Struts、Hibernate 等框架一样,Spring 支持级联属性的配置,例如当我们希望在定义 bean1 时直接为 bean2 的属性提供注入值,则可以采取以下的配置方式:(boss.getCar().setBrand())
public class Boss {    private Car car;    public void setCar(Car car) {        this.car = car;    }}<bean id="boss" class="***">  <property name="car.brand">    <value>奔驰E级</value>  </property></bean>

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

② 当属性为 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. <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>

③ 当属性为 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. <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>

④ 当属性为 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. <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>

⑤ 强类型结合:根据 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. <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>

⑥集合合并:配置文件中的集合合并的功能,允许子 <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.  
  11. <bean id="childBoss" parent="parentBoss">

  12. <property name="favorites">

  13. <set merge="true">

  14. <value>旅游</value>

  15. <value>睡觉</value>

  16. </set>

  17. </property>

  18.  

--------------------- 作者:Hin_CSDN 来源:CSDN 原文:https://blog.csdn.net/qq_21396469/article/details/63684769?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!

(7)简化配置方式:Spring 为字面值、引用 Bean 和集合都提供了相对简化的配置方式。
(8)自动装配:就是不再使用 ref 进行手工装配 Bean,这种方式可以减少配置文件的代码量,但是在大型项目中,不推荐使用,容易混乱。
①autowire="byName"
②autowire="byType"
③autowire="constructor"
④autowire="autodetect"
<beans> 元素标签中的 default-autowire 属性可以配置全局自动装配,其属性的默认值为 no,标志不启用自动装配;在 <beans> 中定义的自动装配策略可以被 <bean> 的自动装配策略覆盖。

---------------------
作者:Hin_CSDN 
来源:CSDN 
原文:https://blog.csdn.net/qq_21396469/article/details/63684769?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/bingguang1993/article/details/83012423