Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
How to use Collections with Spring Expression (SpEL) Example
2969 Views
Hints 
Below is an example of "How to use Collections with Spring Expression (SpEL) Example"
Step.1 Start a Java Project with required jars 
  1. Open Eclipse
  2. Click on menu New -> Others
  3. In wizards type "Java Project" and Select "Java Project"
  4. Click Next
  5. Enter project name as "SpELWithCollections", then click Next
  6. Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
  7. Click Finish.
Step.2 Project Explorer Preview 
bean-configuration.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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

	<util:list id="cityNames">
		<bean class="City" p:cityName="Newyork" p:population="1002" />
		<bean class="City" p:cityName="Texas" p:population="1500" />
		<bean class="City" p:cityName="Wasington" p:population="2000" />
		<bean class="City" p:cityName="London" p:population="4000" />
		<bean class="City" p:cityName="Beijing" p:population="950" />
		<bean class="City" p:cityName="Tokyo" p:population="800" />
	</util:list>

	<bean id="consolidatedCity" class="ConsolidatedCity">
		<property name="topCityNames" value="#{cityNames.?[population >= 1000]}" />
		
		<property name="favoriteCity" value="#{cityNames[2]}" />
		
		<property name="firstCity" value="#{cityNames.^[population >= 1000]}" />
		<property name="lastCity" value="#{cityNames.$[population >= 1000]}" />
		
		<property name="cityNamesList" value="#{cityNames.![cityName]}" />
		<property name="topCityNamesList" value="#{cityNames.?[population >= 1000].![cityName]}" />
		<property name="cityNamesInDetails" value="#{cityNames.![cityName+'('+population+')']}" />
	</bean>
</beans>
ConsolidatedCity.java
import java.util.List;

public class ConsolidatedCity {
	private List topCityNames;
	private City favoriteCity;
	private City firstCity;
	private City lastCity;
	private List cityNamesList;
	private List topCityNamesList;
	private List cityNamesInDetails;
	
	public List getTopCityNames() {
		return topCityNames;
	}
	public void setTopCityNames(List topCityNames) {
		this.topCityNames = topCityNames;
	}
	public City getFavoriteCity() {
		return favoriteCity;
	}
	public void setFavoriteCity(City favoriteCity) {
		this.favoriteCity = favoriteCity;
	}
	public City getFirstCity() {
		return firstCity;
	}
	public void setFirstCity(City firstCity) {
		this.firstCity = firstCity;
	}
	public City getLastCity() {
		return lastCity;
	}
	public void setLastCity(City lastCity) {
		this.lastCity = lastCity;
	}
	public List getCityNamesList() {
		return cityNamesList;
	}
	public void setCityNamesList(List cityNamesList) {
		this.cityNamesList = cityNamesList;
	}
	public List getTopCityNamesList() {
		return topCityNamesList;
	}
	public void setTopCityNamesList(List topCityNamesList) {
		this.topCityNamesList = topCityNamesList;
	}
	public List getCityNamesInDetails() {
		return cityNamesInDetails;
	}
	public void setCityNamesInDetails(List cityNamesInDetails) {
		this.cityNamesInDetails = cityNamesInDetails;
	}
}
City.java
public class City {
	private String cityName;
	private int population;
	
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public int getPopulation() {
		return population;
	}
	public void setPopulation(int population) {
		this.population = population;
	}
	
	public String toString(){
		return "City:"+this.cityName+"; Population:"+this.population+";";
	}
}
SpringRunner.java
import java.util.List;

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

public class SpringRunner {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean-configuration.xml");
		
		List cityNames = (List) context.getBean("cityNames");
		
		System.out.println("
All cities");
		for (City city:cityNames){
			System.out.println(city);
		}
		
		ConsolidatedCity consolidatedCity = (ConsolidatedCity) context.getBean("consolidatedCity");
		
		System.out.println("
Top cities");
		for (City city:consolidatedCity.getTopCityNames()){
			System.out.println(city);
		}
		
		System.out.println("
Favorite City");
		System.out.println(consolidatedCity.getFavoriteCity());
		
		System.out.println("
First City");
		System.out.println(consolidatedCity.getFirstCity());
		
		System.out.println("
Last City");
		System.out.println(consolidatedCity.getLastCity());
		
		System.out.println("
City Names");
		System.out.println(consolidatedCity.getCityNamesList());
		
		System.out.println("
Top City Names");
		System.out.println(consolidatedCity.getTopCityNamesList());
		
		System.out.println("
City Names with Details");
		System.out.println(consolidatedCity.getCityNamesInDetails());
	}
}
Output 
All cities
City:Newyork; Population:1002;
City:Texas; Population:1500;
City:Wasington; Population:2000;
City:London; Population:4000;
City:Beijing; Population:950;
City:Tokyo; Population:800;

Top cities
City:Newyork; Population:1002;
City:Texas; Population:1500;
City:Wasington; Population:2000;
City:London; Population:4000;

Favorite City
City:Wasington; Population:2000;

First City
City:Newyork; Population:1002;

Last City
City:London; Population:4000;

City Names
[Newyork, Texas, Wasington, London, Beijing, Tokyo]

Top City Names
[Newyork, Texas, Wasington, London]

City Names with Details
[Newyork(1002), Texas(1500), Wasington(2000), London(4000), Beijing(950), Tokyo(800)]
Download as Zip 
Link to download
SpELWithCollections

Hints.
Click on File menu. then click "Download"
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details