Introduction to Spring Web Framework-3
April 1, 2008 by muralis
Bean Definition Configuration File
All the basic definition of the Bean classes along with the Configuration Information, their relationships with other Bean objects can be defined in the Xml Configuration File. The Schema Definition of the Bean Xml Configuration file is very vast in terms of Definitions and Functionalities and the subsequent sections aims in covering only the major configuration features.
Making Associations between Bean Objects
It is a common thing in an Application where we can see so many components interacting within each other thereby full-filling the Client needs. For that, they should Establish Relation-ship between them which can be done easily in the Configuration file. Assume that we have two Components Player and Team, whose class structures are given below,
Player.java
public class Player {
private String name;
private Team team;
}
Team.java
public class Team {
private String name;
}
In the above case we have two Bean Components namely Player and Team. Since a Player is dedicated to one Team, we have a reference to the Team object inside the Player class. Now Team becomes a property for the Player class. However, it is not a simple property that holds string or integer; instead it is a complex holds whose data-type is of Type Team.
Following is the sample xml snippet for making association between Player and Team objects.
team-player.xml
India
Sachin Tendulkar
Note that the Team (india) is referenced into the Player object using the ‘ref’ element through the property bean whose value corresponds to the ‘id’ attribute of the previously defined Team object.
Mapping Collection Properties
In this section, let us strengthen the relation-ship between Team and Player classes to include various Collection Properties. Following is the class structure of the Team Class.
Team.java
package net.javabeat.articles.spring.complex;
import java.util.*;
public class Team {
private String name;
private Set players;
public Team() {
}
public Team(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getPlayers() {
return players;
}
public void setPlayers(Set players) {
this.players = players;
}
@Override
public String toString(){
return “[Name = " + name + ", Players = " +
playersAsString(players) + "]“;
}
private String playersAsString(Collection collection){
if(collection == null || collection.isEmpty()){
return “”;
}
StringBuilder result = new StringBuilder();
Iterator iterator = collection.iterator();
result.append(”[");
while (iterator.hasNext()){
String name = ((Player)iterator.next()).getName();
result.append("{" + name + "}");
}
result.append("]“);
return result.toString();
}
}
Since a Team contains more than one player, we have represented this in programming aspects in the form of Set players. Let us see later how to map the set of players in the Configuration File. Also note that the toString() method is overridden to print some meaningful information about the Object. Following is the complete code listing of the Player Class. Since a player would have played against many teams and have taken runs, a property representing Map keyed with Team along with an Integer (which represents the number of runs scored against a particular Team) is defined.
Player.java
package net.javabeat.articles.spring.complex;
import java.util.*;
public class Player {
private String name;
private Team team;
private Map runsScored;
public Player() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
public Map getRunsScored(){
return runsScored;
}
public void setRunsScored(Map runsScored){
this.runsScored = runsScored;
}
@Override
public String toString(){
return “[Name = " + getName() + " , Team Name = "
+ getTeam().getName() + ", RunsScored = "
+ runsScoredAsString() + "]“;
}
private String runsScoredAsString(){
if (runsScored == null || runsScored.isEmpty()){
return “”;
}
StringBuilder result = new StringBuilder();
result.append(”[");
Iterator<Map.Entry> iterator =
runsScored.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry entry = iterator.next();
result.append("{" + entry.getKey().getName() + "," +
entry.getValue() + "}");
}
result.append("]“);
return result.toString();
}
}
Note that, here also the toString() method is overridden to print the information from the Map which contains information about the opposition Team along with the number of runs a player has scored.
player-team.xml
India
Australia
South Africa
Sachin Tendulkar
5638
6383
Rahul Dravid
3638
3981
Saurov Ganguly
4688
2343
Take a careful look into the Xml File. Since a Team represents a set of players, this information is configured in the Xml File thorough the help of ’set’ element. And since the elements in the set itself are bean objects, they are mentioned using the ‘ref’ element. Following is the snippet code for that,
And coming to the Player object, since the property ‘runsScored’ is represented as a Map whose key is the Team object valued with the number of runs scored which is an integer, the whole structure is represented using the map element with key and value elements. Following is the xml snippet for the same,
5638
6383
Following is the code snippet of the Client along with the output information, which loads and print values of several of the bean objects
Client Application
Resource resource = new FileSystemResource(”./src/resources/player-team.xml”);
BeanFactory factory = new XmlBeanFactory(resource);
Team india = (Team)factory.getBean(”india”);
System.out.println(india);
Player tendulkar = (Player)factory.getBean(”tendulkar”);
System.out.println(tendulkar);
Player dravid = (Player)factory.getBean(”dravid”);
System.out.println(dravid);
Player ganguly = (Player)factory.getBean(”ganguly”);
System.out.println(ganguly);
Output of the program
[Name = India, Players = [{Sachin Tendulkar}{Rahul Dravid}{Saurov Ganguly}]]
[Name = Sachin Tendulkar , Team Name = India, RunsScored =
[{Australia,5638}{South Africa,6383}]]
[Name = Rahul Dravid , Team Name = India, RunsScored =
[{Australia,3638}{South Africa,3981}]]
[Name = Saurov Ganguly , Team Name = India,
RunsScored = [{Australia,4688}{South Africa,2343}]]
Importing Configuration Files
Having some much Bean Definition Information in one Configuration File wont look nice especially if the size of the File grows large. Spring provides a modular solution for this problem wherein Bean Definition Objects for one type can be defined in a separate file and the same can be included in the main configuration file. Considering the previous example, we can define all the Player objects in player.xml, Team objects in team.xml and can have a main.xml file which includes both player.xml and team.xml files.
Following is the xml snippet for the same,
main.xml