📜  春季-注入系列

📅  最后修改于: 2020-11-11 07:02:57             🧑  作者: Mango


您已经了解了如何在Bean配置文件中使用标记的ref属性使用value属性和对象引用来配置原始数据类型。两种情况都涉及将奇异值传递给bean。

现在,如果要传递诸如Java Collection类型的复数值(如List,Set,Map和Properties),该怎么办。为了处理这种情况,Spring提供了四种类型的集合配置元素,如下所示:

Sr.No Element & Description
1

This helps in wiring ie injecting a list of values, allowing duplicates.

2

This helps in wiring a set of values but without any duplicates.

3

This can be used to inject a collection of name-value pairs where name and value can be of any type.

4

This can be used to inject a collection of name-value pairs where the name and value are both Strings.

您可以使用连接java.util.Collection或数组的任何实现。

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

让我们拥有一个运行良好的Eclipse IDE,并采取以下步骤来创建一个Spring应用程序:

Steps Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes JavaCollection, and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这是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文件的内容-

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 ,它具有所有集合类型的配置-





   
   
      
      
      
         
            INDIA
            Pakistan
            USA
            USA
         
      

      
      
         
            INDIA
            Pakistan
            USA
            USA
         
      

      
      
         
            
            
            
            
         
      
      
      
      
         
            INDIA
            INDIA
            Pakistan
            USA
            USA
         
      
   


完成创建源和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引用作为集合的元素之一进行注入。甚至您可以将引用和值混合在一起,如以下代码片段所示-





   
   

      
      
         
            
            
            Pakistan
         
      
      
      
      
         
            
            
            Pakistan
         
      
      
      
      
         
            
            
            
         
      
   


要使用上面的bean定义,您需要以一种也应该能够处理引用的方式定义setter方法。

注入空和空字符串值

如果您需要传递一个空字符串作为值,则可以按以下方式传递它:


   

前面的示例等效于Java代码:exampleBean.setEmail(“”)

如果需要传递NULL值,则可以按以下方式传递它-


   

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