Spring Framework Part I-The Context

[article] [edit page] [discussion] [history]

From Humanitarian-FOSS Project Development Site

Spring is a FOSS framework for developing "industrial strength" (i.e., enterprise) applications. Spring is a layered and modular architecture that has various features. Over the course of these tutorials we will look at several of its key functionalities.

Image:Spring framework.gif

In this tutorial we will look at the Spring Context module, which is useful for building user interfaces (UI), for interacting with an application's context. An application's context may determine its functionality in a number of different ways, including localization and internationalization.


Inversion of Control (IOC)

The Spring Context framework enables you to implement the |Inversion of Control (IOC) design pattern, which is also referred to as the Dependency Injection (DI) pattern. The DI pattern allows an application to specify a dependency at run time (rather than compile time). For example, suppose you had application to list the members of a social network. The listMembers() method depends on how the members are stored and this can vary from context to context. The Spring framework provides a mechanism whereby this dependency can be resolved at run time--by specifying a class.

In the IOC or DI pattern you do not create objects in your code (using contructors). Instead there are different ways to resolve these dependencies, including Java beans.

A Simple Example with Animals

To illustrate how this works in the spring framework, we will walk through a simple example. In this example, we are implementing a pet store that can have various kinds of animals. Rather than creating animal objects at compile time, we create them at run time using Java beans.

Our domain is a set of animal classes and interfaces:

  • The AnimalInterface class defines the methods that our pet store denizens will use in the application:
public abstract interface AnimalInterface {
    public abstract String speak();   // Abstract methods
    public abstract String getKind(); 
  • The Animal class will provide a container for a pet store animal, such as a cat. Note that it has no constructor but that it does have set() and get() methods for its animal variable:
public class Animal {
    private AnimalInterface animal;
    public void setAnimal(AnimalInterface animal) { this.animal = animal;}
    public AnimalInterface getAnimal() { return this.animal; }
    public String toString() {
	return "I am a " + animal.getKind() + " and I go "  + animal.speak();
    public  String speak() { return toString(); }   
  • The Cat class implements AnimalInterface. Note again, no constructor, but set() and get() methods for each of its variables.
{public class Cat implements AnimalInterface {
    protected String kind; // cat
    protected String voice; // meow
    public void setKind(String kind) { this.kind = kind; }
    public String getKind() {  return this.kind;  }
    public void setVoice(String voice) { this.voice = voice; }
    public String speak() { return voice; }

The Pet Store Application

In this simple example, our pet store consists simply of an array of Animals and their behavior is simply to speak(). Note how the (Animal)appContext.getBean() method is used to instantiate the animals in the pet store:

public class CreatePetStore {
    public static void main(String[] args){
        System.out.println("Start of Spring petstore example");
        try {
	    System.out.println("CreatePetStore started");
       	    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {
            Animal  animals[] = new Animal[3];
            for (int k = 0; k < animals.length; k++) {
	        animals[k] = (Animal)appContext.getBean("animal" + (k+1));
	    for (int k = 0; k < animals.length; k++) {
	    System.out.println("CreatePetStore end");
	}  catch(Exception e){
	System.out.println("End of Spring petstore example");

This application begins by setting up the ApplicationContext by reading certain information from the petstore.xml file. It then creates 3 animals with the getBean() method and stores them in an array. It then let's them speak(). Note that the pet store has no idea what kinds of animals it contains--this information has been abstracted away to the Context and can be supplied at run time.

Specifying the Context in XML

The petstore.xml file contains a number of bean definitions that tie the data to the application. Note that each bean is associated with a Java class that defines that object. And note that the properties correspond to the instance variables in those classes. In order for this to work, the Animal class must have an instance variable named animal and it must have valid set() and get() methods for that variable:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
	<bean id="animal1" class="domain.Animal">
		<property name="animal">
		  <ref bean="catBean" />

	<bean id="catBean" class="domain.Cat">
	      <property name="kind">
	      <property name="voice">


Download the source code for this example. Create an eclipse project for it. Add two new kinds of animals to the pet store.


Personal tools