天天看點

Spring認證-Spring注入集合

您已經了解了如何使用值屬性配置原始資料類型,并使用Bean 配置檔案中的 标記的ref屬性配置對象引用。這兩種情況都涉及将奇異值傳遞給 bean。

現在,如果您想傳遞多個值,例如 Java 集合類型,例如 List、Set、Map 和 Properties,該怎麼辦。為了處理這種情況,Spring 提供了四種類型的集合配置元素,如下所示 -

沒有 元素和描述

1

<清單>

這有助于接線,即注入值清單,允許重複。

2

<設定>

這有助于連接配接一組值但沒有任何重複。

3

<地圖>

這可用于注入名稱-值對的集合,其中名稱和值可以是任何類型。

4

<道具>

這可用于注入名稱和值都是字元串的名稱-值對集合。

您可以使用 或 來連接配接 java.util.Collection 或數組的任何實作。

您将遇到兩種情況 (a) 傳遞集合的直接值和 (b) 傳遞 bean 的引用作為集合元素之一。

例子

讓我們有一個工作的 Eclipse IDE 并采取以下步驟來建立一個 Spring 應用程式 -

腳步 描述

1 建立一個名為SpringExample的項目,并在建立的項目的src檔案夾下建立一個包com.tutorialspoint。

2 使用添加外部 JAR選項添加所需的 Spring 庫,如Spring Hello World 示例章節中所述。

3 建立Java類JavaCollection和MainApp下com.tutorialspoint包。

4 在src檔案夾下建立 Beans 配置檔案Beans.xml。

5 最後一步是建立所有 Java 檔案和 Bean 配置檔案的内容并運作應用程式,如下所述。

這是JavaCollection.java檔案的内容-

package com.tutorialspoint;

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檔案的内容-

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

d">

<!-- 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 = "one">INDIA</prop>
        <prop key = "two">Pakistan</prop>
        <prop key = "three">USA</prop>
        <prop key = "four">USA</prop>
     </props>
  </property>           

完成源檔案和 bean 配置檔案的建立後,讓我們運作應用程式。如果您的應用程式一切正常,它将列印以下消息 -

List Elements :[INDIA, Pakistan, USA, USA]

Set Elements :[INDIA, Pakistan, USA]

ap Elements :{1 = INDIA, 2 = Pakistan, 3 = USA, 4 = USA}

Property Elements :{two = Pakistan, one = INDIA, three = USA, four = USA}

注入 Bean 引用

以下 Bean 定義将幫助您了解如何将 bean 引用作為集合元素之一注入。即使您可以将引用和值混合在一起,如以下代碼片段所示 -

<!-- 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 定義,您需要以這樣一種方式定義 setter 方法,以便它們也應該能夠處理引用。

注入 null 和空字元串值

如果您需要傳遞一個空字元串作為值,那麼您可以按如下方式傳遞它 -

前面的示例等效于 Java 代碼:exampleBean.setEmail("")

如果您需要傳遞一個 NULL 值,那麼您可以按如下方式傳遞它 -

<null/>

前面的示例等效于 Java 代碼:exampleBean.setEmail(null)

繼續閱讀