Spring Framework Standalone Collections


Spring Framework allows to inject of collection objects into a bean through constructor dependency injection or setter dependency injection using <list>,<map>,<set>, etc. Given below is an example of the same.

Example Project

pom.xml: 

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>org.example</groupId>
  <artifactId>GFGArticles</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <name>GFGArticles</name>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.1.0.RELEASE</version>
    </dependency>
  
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.0.RELEASE</version>
    </dependency>
      
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      
  </dependencies>
</project>

Student.java: 

import java.util.*;
  
public class Student {
    
    private int roll;
    private String name;
    private LinkedHashSet phnNo;
    private List<String> degree;
    private Map<String,Integer> courses;
  
    public Student(int roll, String name, LinkedHashSet phnNo, List<String> degree, Map<String, Integer> courses) {
        this.roll = roll;
        this.name = name;
        this.phnNo = phnNo;
        this.degree = degree;
        this.courses = courses;
    }
  
    @Override
    public String toString() {
        return "Student{" +
                "\n roll=" + roll +
                "\n name=" + name +
                "\n phnNo=" + phnNo +
                "\n degree=" + degree +
                "\n courses=" + courses +
                '}';
    }
}

config.xml:

<?xml version="1.0" encoding="UTF-8"?>
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <bean class="Student" id="s">
            <constructor-arg value="10"/>
            <constructor-arg value="Priya"/>
            <constructor-arg>
                <set>
                    <value>9215111448</value>
                    <value>8026570991</value>
                    <value>7015247338</value>
                </set>
            </constructor-arg>
            <constructor-arg>
                <list>
                    <value>BTech</value>
                    <value>MTech</value>
                </list>
            </constructor-arg>
            <constructor-arg>
                <map>
                    <entry key="Hibernate" value="1"/>
                    <entry key="Spring" value="2"/>
                    <entry key="MySQL" value="1"/>
                </map>
            </constructor-arg>
        </bean>
</beans>

App.java:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class App
{
    public static void main( String[] args )
    {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("config.xml");
        Student s=ctx.getBean("s",Student.class);
        System.out.println(s);
    }
}

Output:

 

But Collection beans injected using the above method can not be reused. In order to create standalone Collection beans, util schema is used. The following code needs to be added to the configuration file:


config.xml:

<?xml version="1.0" encoding="UTF-8"?>
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd">
    <util:set set-class="java.util.LinkedHashSet" id="phnNo">
        <value>9312552334</value>
        <value>8723546787</value>
    </util:set>
    <util:list id="degree">
        <value>BA</value>
        <value>MA</value>
    </util:list>
    <util:map id="courses">
        <entry key="Django" value="2"/>
        <entry key="Bootstrap" value="1"/>
    </util:map>
    <bean class="Student" id="s">
        <constructor-arg value="12" type="int"/>
        <constructor-arg value="Nisha"/>
        <constructor-arg ref="phnNo"/>
        <constructor-arg ref="degree"/>
        <constructor-arg ref="courses"/>
    </bean>
</beans>

Output:

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence