Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Spring Collections

Collections are very useful and frequently used with java technology. Generics also made it very easy to use collection with auto-boxing and un-boxing functionality. Collections are used to manipulate objects easily with java. In spring, collections are used to manage objects instead of using primitive data types. This tutorial explains how to use the collections API with Spring configurations.

Before collection, we used to pass single value at a time. If we are required to pass multiple or bundle of data to be passed like list, set, and map etc. To manage such kind or problem, spring provides a way of injecting collections. We can inject list, map, set and pros type collections in spring. There are separate tags are used for these type of collection types as listed:

  • <list> - This helps in wiring ie injecting a list of values, allowing duplicates.
  • <set> - This helps in wiring a set of values but without any duplicates.
  • <map> - This can be used to inject a collection of name-value pairs where name and value can be of any type.
  • <props> - This can be used to inject a collection of name-value pairs where the name and value are both Strings.

Example of Collection Injection in Spring


package springCollection;

import java.util.*;

public class Department {
private List<String> departmentList;
private Set<String> departmentSet;
private Map<String> departmentMap;
private Properties departmentProperties;

public void setDepartmentList(List<String> departmentList) {
this.departmentList = departmentList;
}
public void setDepartmentSet(Set<String> departmentSet) {
this.departmentSet = departmentSet;
}
public void setDepartmentMap(Map<String> departmentMap) {
this.departmentMap = departmentMap;
}
public void setDepartmentProperties
(List<String> departmentProperties) {

this.departmentProperties = departmentPropertiest;
}

public void displayContent(){
System.out.println("List :: "+departmentList);
System.out.println("List :: "+departmentSet);
System.out.println("List :: "+departmentMap);
System.out.println("List :: "+departmentProperties);

}

}

context.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="department" class="springCollection.Department">
<property name="departmentList" >
<list>
<value>IT</value>
<value>FINANCE</value>
<value>HR</value>
</list>
</property>

<property name="departmentSet" >
<set>
<value>MARKETING</value>
<value>MANAGEMENT</value>
<value>LOGISTIC</value>
</set>
</property>

<property name="departmentMap" >
<map>
<entry key="1" value="ADMIN"/>
<entry key="2" value="MARKETING"/>
<entry key="3" value="SECURITY"/>
</map>
</property>

<property name="departmentProperties" >
<props>
<prop key="1">"MANAGEMENT"</prop>
<prop key="2">"FINANCE"</prop>
<prop key="3">"IT"</prop>
</props>
</property>

</bean>

</beans>

Main.java

package springCollection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext
("springCollection/context.xml");

Department obj = (Department)context.getBean("department");
obj.displayContent();
}
}


     << Previous
Next >>