国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 14:47:02
字體:
供稿:網(wǎng)友

使用value屬性和使用<property>標簽的ref屬性在你的bean配置文件中的對象引用,這兩種情況下可以處理單值到一個bean,如果你想通過多元值,如Java Collection類型List, Set, Map 及 Properties。要處理這種情況,Spring提供了四種類型的如下集合的配置元素:

2015125170730018.png (578×174)

可以使用<list> 或<set> 來連接任何實現(xiàn)java.util.Collection或數(shù)組。

會遇到兩種情況(a)將收集的直接的值及(b)傳遞一個bean的引用作為集合的元素之一。

例子:
我們使用Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個Spring應(yīng)用程序:

2015125170747225.png (589×320)

這里是JavaCollection.java文件的內(nèi)容:

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文件的內(nèi)容:

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>

創(chuàng)建源代碼和bean配置文件完成后,讓我們運行應(yīng)用程序。如果應(yīng)用程序一切順利,這將打印以下信息:

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定義,需要定義這樣一種方式,他們應(yīng)該能夠處理的參考,以及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)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 田东县| 柳河县| 呼伦贝尔市| 宁陕县| 惠东县| 彝良县| 镇赉县| 元氏县| 敖汉旗| 巢湖市| 阿坝县| 墨竹工卡县| 明光市| 黄龙县| 浠水县| 绥棱县| 嘉善县| 东明县| 兴化市| 潼关县| 张家港市| 台北县| 德保县| 贺兰县| 莒南县| 静宁县| 承德市| 乌拉特中旗| 富裕县| 夹江县| 东光县| 历史| 汪清县| 文成县| 平和县| 兴安县| 绥棱县| 句容市| 常德市| 军事| 嘉善县|