J3 Limited
 
Google
WWW J3Ltd
 
Introduction To Spring

Application Overview

In the previous section, a bare bones Spring project was setup. In this section, the code for an example application is written.

The application developed is kept simple, so as to focus more on Spring than what the application does.

The goal of the application is to output text. The destination of where the text goes is configured using Spring dependency injection.

By using dependency injection, and an external xml configuration file, there is a central point of control over the behaviour of the application.

 

A new file called mainSpringContext.xml is created in the res folder.

res/mainSpringContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
">

<bean id="devPrint" class="springtutorial.DevelopmentPrinter"/>
<bean id="prodPrint" class="springtutorial.ProductionPrinter"/>
<alias alias="printer" name="devPrint"/>

<bean id="mainApp" class="springtutorial.Main">
<property name="springWiredProperty"
value="This value is defined in the Spring Context"/>
<property name="print" ref="printer"/>
</bean>
</beans>

The first part of the file is needed for it to be parsed by Spring.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

">

The next part defines 3 beans and one alias bean.

 <bean id="devPrint" class="springtutorial.DevelopmentPrinter"/>
<bean id="prodPrint" class="springtutorial.ProductionPrinter"/>
<alias alias="printer" name="devPrint"/>

<bean id="mainApp" class="springtutorial.Main">
<property name="springWiredProperty"
value="This value is defined in the Spring Context"/>
<property name="print" ref="printer"/>
</bean>
  • Bean devPrint maps to the DevelopmentPrinter Java bean.
  • Bean prodPrint maps to the ProductionPrinter Java bean.
  • Alias printer maps to the devPrint bean.
  • Bean mainApp is the application and maps to the Main Class. Two member variable values of mainApp are wired up by Spring here: springWiredProperty (a String value) and print (a bean).

Create The Interfaces

It is normal practice to define beans using a class and an interface. This ensures decoupling, as mentioned in Martin Fowler's Inversion of Control Containers and the Dependency Injection pattern.

A Java interface called IMain is created in the springtutorial package.

IMain.java
package springtutorial;

public interface IMain {
	public void go();
}

A printer interface, called IPrinter, is created.

IPrinter.java
package springtutorial;

public interface IPrinter {
	public void print(String line);
}

Implement the Interfaces

The Main class implements IMain, as shown below.

Main.java
package springtutorial;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main implements IMain {
	private String springWiredProperty;
	private IPrinter print;
	// Lists the files in which Spring beans are defined:
	private static final String[] SpringConfigLocations =
		{"mainSpringContext.xml"};
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = 
            new ClassPathXmlApplicationContext(SpringConfigLocations);
		IMain main = (IMain) context.getBean("mainApp");
		main.go();
	}

	@Override
	public void go() {
		print.print("It works " + springWiredProperty);
		
	}

	public String getSpringWiredProperty() {
		return springWiredProperty;
	}

	public void setSpringWiredProperty(String springWiredProperty) {
		this.springWiredProperty = springWiredProperty;
	}

	public IPrinter getPrint() {
		return print;
	}

	public void setPrint(IPrinter print) {
		this.print = print;
	}
	
	
}

In the above code listing it is worth noting the following:

  • Two properties are defined springWiredProperty and print. These properties have getters and setters defined, that allow Spring to set the values in this Spring bean.
  • The static void main() method initialises Spring with the xml configuration file (mainSpringContext.xml). main() then retrieves a bean called "mainApp" from Spring's application context, and calls go() on it.
  • The method go() calls print.print() on the Spring wired bean of this class, and passes another Spring wired property value to it (springWiredProperty)

Two implementations of the IPrinter interface are implemented.

ProductionPrinter.java
package springtutorial;

public class ProductionPrinter implements IPrinter {

	@Override
	public void print(String line) {
		System.out.print("Production: ");
		System.out.println(line);		
	}

}

 

DevelopmentPrinter.java
package springtutorial;

public class DevelopmentPrinter implements IPrinter {

	@Override
	public void print(String line) {
		System.out.print("Development: ");
		System.out.println(line);		
	}

}

Project Tree

At this stage the project is ready to be run as a java application. The project tree is as shown below.

Running the Application

The class Main.java is run as a Java application. The console is inspected to make sure all ran as expected:

That concludes the first Spring application. In this example, dependency injection has been used to specify which IPrinter implementation the application should use. To change it from the bean devPrint to prodPrint, only the xml configuration file needs to be touched.

The project files and source code can be downloaded here.

The next section describes how property files can be used to further configure a Spring application.

 

  Copyright © 2006 J3 Ltd Permission is granted to reproduce material on this page, on the condition that a reference to "WWW.J3Ltd.com" is given as the source of the material.