English 中文(简体)
Spring DI - Collection Ref Setter
  • 时间:2024-12-22

Sprint DI - Collections Ref Setter


Previous Page Next Page  

You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.

Now what if you want to pass plural values pke Java Collection types such as List, Set, Map, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows −

Sr.No Element & Description
1

<pst>

This helps in wiring ie injecting a pst of values, allowing duppcates.

2

<set>

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

You can use either <pst> or <set> to wire any implementation of java.util.Collection or an array.

In this example, we re showcasing passing collection elements using ref.

Example

The following example shows a class JavaCollection that is using collection of dependencies injected using setters.

Let s update the project created in Spring DI - Create Project chapter. We re adding following files −

    Address.java − A class to be used as dependency.

    JavaCollection.java − A class containing a collections of dependencies.

    MainApp.java − Main apppcation to run and test.

Here is the content of Address.java file −


package com.tutorialspoint;

pubpc class Address {
   private String name;

   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }	
   @Override
   pubpc String toString() {
      return name;
   }
}

Here is the content of JavaCollection.java file −


package com.tutorialspoint;
import java.util.*;

pubpc class JavaCollection {
   List<Address> addressList;
   Set<Address>  addressSet;

   // a setter method to set List
   pubpc void setAddressList(List<Address> addressList) {
      this.addressList = addressList;
   }
   
   // prints and returns all the elements of the pst.
   pubpc List<Address> getAddressList() {
      System.out.println("List Elements :"  + addressList);
      return addressList;
   }

   // a setter method to set Set
   pubpc void setAddressSet(Set<Address> addressSet) {
      this.addressSet = addressSet;
   }

   // prints and returns all the elements of the Set.
   pubpc Set<Address> getAddressSet() {
      System.out.println("Set Elements :"  + addressSet);
      return addressSet;
   }
}

Following is the content of the MainApp.java file −


package com.tutorialspoint;

import org.springframework.context.ApppcationContext;
import org.springframework.context.support.ClassPathXmlApppcationContext;

pubpc class MainApp {
   pubpc static void main(String[] args) {
      ApppcationContext context = new ClassPathXmlApppcationContext("apppcationcontext.xml");
      JavaCollection jc=(JavaCollection)context.getBean("javaCollection");

      jc.getAddressList();
      jc.getAddressSet();
   }
}

Following is the configuration file apppcationcontext.xml which has configuration for all the type of collections −


<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 id = "address1" class = "com.tutorialspoint.Address">
      <property name="name" value="INDIA"></property>
   </bean>
   <bean id = "address2" class = "com.tutorialspoint.Address">
      <property name="name" value="JAPAN"></property>
   </bean>
   <bean id = "address3" class = "com.tutorialspoint.Address">
      <property name="name" value="USA"></property>
   </bean>
   <bean id = "address4" class = "com.tutorialspoint.Address">
      <property name="name" value="UK"></property>
   </bean>
   
   <!-- Definition for javaCollection -->
   <bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
      <!-- results in a setAddressList(java.util.List) call -->
      <property name = "addressList">
         <pst>
            <ref bean="address1" />
            <ref bean="address2" />
            <ref bean="address3" />
            <ref bean="address4" />
         </pst>
      </property>

      <!-- results in a setAddressSet(java.util.Set) call -->
      <property name = "addressSet">
         <set>
            <ref bean="address1" />
            <ref bean="address2" />
            <ref bean="address3" />
            <ref bean="address4" />
         </set>
      </property>
   </bean>
</beans>

Output

Once you are done creating the source and bean configuration files, let us run the apppcation. If everything is fine with your apppcation, it will print the following message −


List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Advertisements