最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年12月05日 17:15:10   投稿:goldensun  
這篇文章主要介紹了詳解Java的Spring框架中bean的注入集合,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下

使用value屬性和使用<property>標(biāo)簽的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)

相關(guān)文章

  • 詳解JAVA 內(nèi)存管理

    詳解JAVA 內(nèi)存管理

    這篇文章主要介紹了JAVA 內(nèi)存管理的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • SpringCloud Config分布式配置中心使用教程介紹

    SpringCloud Config分布式配置中心使用教程介紹

    springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用
    2022-12-12
  • JavaWeb項目打開網(wǎng)頁出現(xiàn)Session Error的異常解決方案

    JavaWeb項目打開網(wǎng)頁出現(xiàn)Session Error的異常解決方案

    這篇文章主要介紹了JavaWeb項目打開網(wǎng)頁出現(xiàn)Session Error的異常解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot+Vue+Redis實現(xiàn)單點登錄(一處登錄另一處退出登錄)

    SpringBoot+Vue+Redis實現(xiàn)單點登錄(一處登錄另一處退出登錄)

    小編接到一個需求,需要實現(xiàn)用戶在瀏覽器登錄后,跳轉(zhuǎn)到其他頁面,當(dāng)用戶在其它地方又登錄時,前面用戶登錄的頁面退出登錄,這篇文章主要介紹了SpringBoot+Vue+Redis實現(xiàn)單點登錄,需要的朋友可以參考下
    2019-12-12
  • java中replaceAll替換圓括號實例代碼

    java中replaceAll替換圓括號實例代碼

    正則表達(dá)式的保留字符主要有:圓括號、方括號、花括號、豎線、橫線、點號、加號、星號、反斜桿等等,下面這篇文章主要給大家介紹了關(guān)于java中replaceAll替換圓括號的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • 淺談@mapper引入不到引入的是@MapperScan的問題

    淺談@mapper引入不到引入的是@MapperScan的問題

    這篇文章主要介紹了淺談@mapper引入不到引入的是@MapperScan的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • springboot中的controller注意事項說明

    springboot中的controller注意事項說明

    這篇文章主要介紹了springboot中的controller注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java http token請求代碼實例

    java http token請求代碼實例

    這篇文章主要介紹了java http token請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Struts2學(xué)習(xí)筆記(9)-Result配置全局結(jié)果集

    Struts2學(xué)習(xí)筆記(9)-Result配置全局結(jié)果集

    這篇文章主要介紹Struts2中使用Result配置全局結(jié)果集的方法,希望能給大家做一個參考。
    2016-06-06
  • java 格式化輸出數(shù)字的方法

    java 格式化輸出數(shù)字的方法

    在實際工作中,常常需要設(shè)定數(shù)字的輸出格式,如以百分比的形式輸出,或者設(shè)定小數(shù)位數(shù)等,現(xiàn)稍微總結(jié)如下
    2014-01-01

最新評論

泾川县| 太康县| 乌鲁木齐市| 舒城县| 夏河县| 泸西县| 高密市| 蕲春县| 蒙自县| 长顺县| 永清县| 浑源县| 濮阳县| 武汉市| 北票市| 乌海市| 介休市| 什邡市| 淮阳县| 洛川县| 勐海县| 镶黄旗| 苗栗县| 怀仁县| 漾濞| 六枝特区| 文水县| 嘉鱼县| 玛曲县| 塘沽区| 靖宇县| 庆云县| 郁南县| 洪雅县| 汨罗市| 铁力市| 黄石市| 辉县市| 察隅县| 石棉县| 治县。|