欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Java的Spring框架中bean的注入集合

程序员文章站 2024-03-07 14:58:03
使用value属性和使用标签的ref属性在你的bean配置文件中的对象引用,这两种情况下可以处理单值到一个bean,如果你想通过多元值,如j...

使用value属性和使用<property>标签的ref属性在你的bean配置文件中的对象引用,这两种情况下可以处理单值到一个bean,如果你想通过多元值,如java collection类型list, set, map 及 properties。要处理这种情况,spring提供了四种类型的如下集合的配置元素:

详解Java的Spring框架中bean的注入集合

可以使用<list> 或<set> 来连接任何实现java.util.collection或数组。

会遇到两种情况(a)将收集的直接的值及(b)传递一个bean的引用作为集合的元素之一。

例子:
我们使用eclipse ide,然后按照下面的步骤来创建一个spring应用程序:

详解Java的Spring框架中bean的注入集合

这里是javacollection.java文件的内容:

package com.yiibai;
import java.util.*;

public class javacollection {
  list addresslist;
  set addressset;
  map addressmap;
  properties addressprop;

  // a setter method to set list
  public void setaddresslist(list addresslist) {
   this.addresslist = addresslist;
  }
  // prints and returns all the elements of the list.
  public list getaddresslist() {
   system.out.println("list elements :" + addresslist);
   return addresslist;
  }

  // a setter method to set set
  public void setaddressset(set addressset) {
   this.addressset = addressset;
  }

  // prints and returns all the elements of the set.
  public set getaddressset() {
   system.out.println("set elements :" + addressset);
   return addressset;
  }

  // a setter method to set map
  public void setaddressmap(map addressmap) {
   this.addressmap = addressmap;
  }
  // prints and returns all the elements of the map.
  public map getaddressmap() {
   system.out.println("map elements :" + addressmap);
   return addressmap;
  }

  // a setter method to set property
  public void setaddressprop(properties addressprop) {
   this.addressprop = addressprop;
  }
  // prints and returns all the elements of the property.
  public properties getaddressprop() {
   system.out.println("property elements :" + addressprop);
   return addressprop;
  }
}

以下是mainapp.java文件的内容:

package com.yiibai;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class mainapp {
  public static void main(string[] args) {
   applicationcontext context = 
       new classpathxmlapplicationcontext("beans.xml");

   javacollection jc=(javacollection)context.getbean("javacollection");

   jc.getaddresslist();
   jc.getaddressset();
   jc.getaddressmap();
   jc.getaddressprop();
  }
}

以下是配置文件beans.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"
  xsi:schemalocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- definition for javacollection -->
  <bean id="javacollection" class="com.yiibai.javacollection">

   <!-- results in a setaddresslist(java.util.list) call -->
   <property name="addresslist">
    <list>
      <value>india</value>
      <value>pakistan</value>
      <value>usa</value>
      <value>usa</value>
    </list>
   </property>

   <!-- results in a setaddressset(java.util.set) call -->
   <property name="addressset">
    <set>
      <value>india</value>
      <value>pakistan</value>
      <value>usa</value>
      <value>usa</value>
    </set>
   </property>

   <!-- results in a setaddressmap(java.util.map) call -->
   <property name="addressmap">
    <map>
      <entry key="1" value="india"/>
      <entry key="2" value="pakistan"/>
      <entry key="3" value="usa"/>
      <entry key="4" value="usa"/>
    </map>
   </property>

   <!-- results in a setaddressprop(java.util.properties) call -->
   <property name="addressprop">
    <props>
      <prop key="one">india</prop>
      <prop key="two">pakistan</prop>
      <prop key="three">usa</prop>
      <prop key="four">usa</prop>
    </props>
   </property>

  </bean>

</beans>

创建源代码和bean配置文件完成后,让我们运行应用程序。如果应用程序一切顺利,这将打印以下信息:

list elements :[india, pakistan, usa, usa]
set elements :[india, pakistan, usa]
map elements :{1=india, 2=pakistan, 3=usa, 4=usa}
property elements :{two=pakistan, one=india, three=usa, four=usa}

注入bean引用:
下面bean定义将帮助您了解如何注入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"
  xsi:schemalocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- bean definition to handle references and values -->
  <bean id="..." class="...">

   <!-- passing bean reference for java.util.list -->
   <property name="addresslist">
    <list>
      <ref bean="address1"/>
      <ref bean="address2"/>
      <value>pakistan</value>
    </list>
   </property>

   <!-- passing bean reference for java.util.set -->
   <property name="addressset">
    <set>
      <ref bean="address1"/>
      <ref bean="address2"/>
      <value>pakistan</value>
    </set>
   </property>

   <!-- passing bean reference for java.util.map -->
   <property name="addressmap">
    <map>
      <entry key="one" value="india"/>
      <entry key ="two" value-ref="address1"/>
      <entry key ="three" value-ref="address2"/>
    </map>
   </property>

  </bean>

</beans>

使用上面的bean定义,需要定义这样一种方式,他们应该能够处理的参考,以及setter方法。

注入null和空字符串的值
如果需要传递一个空字符串作为值,如下所示:

<bean id="..." class="examplebean">
  <property name="email" value=""/>
</bean>

前面的例子等同于java代码: examplebean.setemail("")

如果需要传递一个null值,如下所示:

<bean id="..." class="examplebean">
  <property name="email"><null/></property>
</bean>

前面的例子等同于java代码:examplebean.setemail(null)