Monday, May 13, 2013

Spring - Collection

Spring offers four types of collection configuration elements: list, set, map and properties. You can pass either a direct value or a reference of a bean to a collection element.

See our SpringCollection class -

public class SpringCollection {
    List<Student> studentList;
    Set<Student> studentSet;
    Map<Integer, Student> studentMap;
    Properties studentProperties;
    List<String> stringList;

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public Set<Student> getStudentSet() {
        return studentSet;
    }

    public void setStudentSet(Set<Student> studentSet) {
        this.studentSet = studentSet;
    }

    public Map<Integer, Student> getStudentMap() {
        return studentMap;
    }

    public void setStudentMap(Map<Integer, Student> studentMap) {
        this.studentMap = studentMap;
    }

    public Properties getStudentProperties() {
        return studentProperties;
    }

    public void setStudentProperties(Properties studentProperties) {
        this.studentProperties = studentProperties;
    }
    
    public List<String> getStringList() {
        return stringList;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public void display() {
        for (Student student : studentList) {
            System.out.println("studentList#");
            student.display();
        }

        for (Student student : studentSet) {
            System.out.println("studentSet#");
            student.display();
        }

        for (Map.Entry<Integer, Student> entry : studentMap.entrySet()) {
            System.out.println("studentMap#");
            System.out.println(entry.getKey().intValue());
            entry.getValue().display();
        }

        for (Entry<Object, Object> entry : studentProperties.entrySet()) {
            System.out.println("studentProperties#");
            System.out.println(entry.getKey() + ", " + entry.getValue());
            System.out.println();
        }

        for (String string : stringList) {
            System.out.println("stringList#");
            System.out.println(string);
            System.out.println();
        }
    }
}

Snip of spring.xml -
 
  
  
 

 
  
  
 

 
  
   
    
    
   
  

  
   
    
    
   
  

  
   
    
    
   
  

  
   
    student1
    student2
   
  

  
   
    student1
    student2
   
  
 

Snip of App code -
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        SpringCollection collection = (SpringCollection) context.getBean("collection");
        collection.display();

Output -
studentList#
I am a student.
Name is: James, and ID is: 1

studentList#
I am a student.
Name is: Tom, and ID is: 2

studentSet#
I am a student.
Name is: James, and ID is: 1

studentSet#
I am a student.
Name is: Tom, and ID is: 2

studentMap#
1
I am a student.
Name is: James, and ID is: 1

studentMap#
2
I am a student.
Name is: Tom, and ID is: 2

studentProperties#
2, student2

studentProperties#
1, student1

stringList#
student1

stringList#
student2

No comments:

Post a Comment