欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?注入集合實(shí)現(xiàn)過程示例詳解

 更新時(shí)間:2023年09月12日 10:19:48   作者:小萬哥  
這篇文章主要為大家介紹了Spring?注入集合實(shí)現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

定義Bean配置文件

使用<property>標(biāo)簽的value屬性配置原始數(shù)據(jù)類型和ref屬性配置對(duì)象引用的方式來定義Bean配置文件。這兩種情況都涉及將單一值傳遞給Bean。那么如果您想傳遞多個(gè)值,例如Java集合類型,如List、Set、Map和Properties怎么辦?為了處理這種情況,Spring提供了四種類型的集合配置元素

Spring集合配置元素

如下所示:

序號(hào)元素 & 描述
1<list>
用于注入一組值,允許重復(fù)。
2<set>
用于注入一組值,但不允許重復(fù)。
3<map>
可用于注入一組名稱-值對(duì),其中名稱和值可以是任何類型。
4<props>
可用于注入一組名稱-值對(duì),其中名稱和值都是字符串。

用<list>或<set>注入

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

在處理集合時(shí),通常會(huì)遇到兩種情況:(a)傳遞集合的直接值和(b)將Bean的引用作為集合元素之一傳遞。

示例

假設(shè)您已經(jīng)準(zhǔn)備好Eclipse IDE,并采取以下步驟創(chuàng)建Spring應(yīng)用程序:

步驟描述

1 創(chuàng)建一個(gè)名為SpringExample的項(xiàng)目,在創(chuàng)建的項(xiàng)目中的src文件夾下創(chuàng)建一個(gè)名為com.tutorialspoint的包。

2 使用"Add External JARs"選項(xiàng)添加所需的Spring庫,如Spring Hello World示例章節(jié)中所述。

3 在com.tutorialspoint包下創(chuàng)建Java類JavaCollection和MainApp。

4 在src文件夾下創(chuàng)建Beans配置文件Beans.xml。

5 最后一步是創(chuàng)建所有Java文件和Bean配置文件的內(nèi)容,并按以下說明運(yùn)行應(yīng)用程序。

以下是JavaCollection.java文件的內(nèi)容:

package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
   List addressList;
   Set  addressSet;
   Map  addressMap;
   Properties addressProp;
   // 用于設(shè)置List的setter方法
   public void setAddressList(List addressList) {
      this.addressList = addressList;
   }
   // 打印并返回列表的所有元素。
   public List getAddressList() {
      System.out.println("List Elements :"  + addressList);
      return addressList;
   }
   // 用于設(shè)置Set的setter方法
   public void setAddressSet(Set addressSet) {
      this.addressSet = addressSet;
   }
   // 打印并返回Set的所有元素。
   public Set getAddressSet() {
      System.out.println("Set Elements :"  + addressSet);
      return addressSet;
   }
   // 用于設(shè)置Map的setter方法
   public void setAddressMap(Map addressMap) {
      this.addressMap = addressMap;
   }
   // 打印并返回Map的所有元素。
   public Map getAddressMap() {
      System.out.println("Map Elements :"  + addressMap);
      return addressMap;
   }
   // 用于設(shè)置Property的setter方法
   public void setAddressProp(Properties addressProp) {
      this.addressProp = addressProp;
   }
   // 打印并返回Property的所有元素。
   public Properties getAddressProp() {
      System.out.println("Property Elements :"  + addressProp);
      return addressProp;
   }
}

以下是MainApp.java文件的內(nèi)容:

package com.tutorialspoint;
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配置文件的內(nèi)容:

<?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.tutorialspoint.JavaCollection">
      <!-- 產(chǎn)生 setAddressList(java.util.List) 調(diào)用 -->
      <property name = "addressList">
         <list>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </list>
      </property>
      <!-- 產(chǎn)生 setAddressSet(java.util.Set) 調(diào)用 -->
      <property name = "addressSet">
         <set>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </set>
      </property>
      <!-- 產(chǎn)生 setAddressMap(java.util.Map) 調(diào)用 -->
      <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>
      <!-- 產(chǎn)生 setAddressProp(java.util.Properties) 調(diào)用 -->
      <property name = "addressProp">
         <props>
            <prop key = "one">INDIA</prop>
            <prop key = "one">INDIA</prop>
            <prop key = "two">Pakistan</prop>
            <prop key = "three">USA</prop>
            <prop key = "four">USA</prop>
         </props>
      </property>
   </bean>
</beans>

當(dāng)您完成創(chuàng)建源代碼和Bean配置文件后,讓我們運(yùn)行應(yīng)用程序。如果一切正常,應(yīng)用程序?qū)⒋蛴∫韵孪ⅲ?/p>

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)

以上就是Spring 注入集合實(shí)現(xiàn)過程示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring 注入集合的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java中StopWatch工具類的用法詳解

    Java中StopWatch工具類的用法詳解

    stopWatch 是org.springframework.util 包下的一個(gè)工具類,使用它可直觀的輸出代碼執(zhí)行耗時(shí),以及執(zhí)行時(shí)間百分比,下面就跟隨小編一起來看看它的具體用法吧
    2024-12-12
  • 解析Java和Eclipse中加載本地庫(.dll文件)的詳細(xì)說明

    解析Java和Eclipse中加載本地庫(.dll文件)的詳細(xì)說明

    本篇文章是對(duì)Java和Eclipse中加載本地庫(.dll文件)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出

    SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以參考下
    2024-02-02
  • 帶你入門java雪花算法原理

    帶你入門java雪花算法原理

    SnowFlake 算法,是 Twitter 開源的分布式 id 生成算法。其核心思想就是:使用一個(gè) 64 bit 的 long 型的數(shù)字作為全局唯一 id。在分布式系統(tǒng)中的應(yīng)用十分廣泛,且ID 引入了時(shí)間戳,基本上保持自增的
    2021-06-06
  • jvm字符串常量池在什么內(nèi)存區(qū)域問題解析

    jvm字符串常量池在什么內(nèi)存區(qū)域問題解析

    這篇文章主要介紹了jvm字符串常量池在什么內(nèi)存區(qū)域的問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 詳解Spring Security認(rèn)證流程

    詳解Spring Security認(rèn)證流程

    這篇文章主要介紹了Spring Security認(rèn)證流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • spring的TransactionalEventListener事務(wù)感知源碼解析

    spring的TransactionalEventListener事務(wù)感知源碼解析

    這篇文章主要為大家介紹了spring的TransactionalEventListener事務(wù)感知源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • java異步調(diào)用Feign接口空指針問題解決

    java異步調(diào)用Feign接口空指針問題解決

    這篇文章主要為大家介紹了java異步調(diào)用Feign接口空指針問題解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決

    SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決

    這篇文章主要介紹了SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 簡(jiǎn)單了解java獲取類的3種方式

    簡(jiǎn)單了解java獲取類的3種方式

    這篇文章主要介紹了java獲取類的3種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評(píng)論