Spring standalone using ClassPathXmlApplicationContext

 

                            NOTE: Source Code available for download at the bottom of the page

 
What are we going to learn?
    We will develop a stand alone Maven Spring application which will convert the temperature from/to Centigrade or Fahrenheit. We will inject the required class into our main class using Spring. We will look at loading application context using ClassPathXmlApplicationContext.
 
What do you need ?
  • Maven
  • JDK 1.5 or higher
  • eclipse 
  • Spring 4.0
  • Tomcat
What's the overall design?
  The idea is to create an interface Temperature which will have one method 
                public Double convertTemperature(Double temp);
  Then create two implementation classes ( Centigrade and Fahrenheit ) for the interface.
  The implementation class will be accessed using Spring context to make the call.
 
Let's get started !
 
Step 1:
 
    Create a maven project of artifact "maven-archetype-quickstart" as shown here. Remember to select the correct artifact "maven-archetype-quickstart" as shown below.
 
artificat selection
 
 
 
Step 2:
 
  In order to use spring, update pom.xml file with spring dependency.
 
pom.xml
	<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javavision</groupId>
	<artifactId>SpringStandAlone</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringStandAlone</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
	</dependencies>
</project>
 
 
Step 3:
 
    Create Temperature Interface, Centigrade and Fahrenheit classes. 
 
Temperature Interface
package com.javavision.SpringStandAlone.model;

public interface Temperature {
	public Double convertTemperature(Double temp);

}
 
 
Centigrade class
package com.javavision.SpringStandAlone.model;

public class Centigrade implements Temperature{

	public Double convertTemperature(Double temp) {
		return (temp - 32)*5/9;
	}

}


Fahrenheit class
package com.javavision.SpringStandAlone.model;

public class Fahrenheit implements Temperature{

	public Double convertTemperature(Double temp) {
		return (temp - 32)*5/9;
	}

}


 
Step 4:
 
  Write main method class and load the spring context.
 
package com.javavision.SpringStandAlone;

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

import com.javavision.SpringStandAlone.model.Temperature;

public class MainCaller {

	public static void main(String[] args) {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"application-context.xml");
		Temperature temperature = (Temperature)applicationContext.getBean("temperatureBean");
		System.out.println("Calling converter : " + temperature.convertTemperature(34.5));

	}

}
 
 
Step 5:
    Create application-context.xml file under resources 
 
application-context.xml
<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-2.5.xsd">
 
	<bean id="temperatureBean" class="com.javavision.SpringStandAlone.model.Centigrade" />
	
</beans>
 
 
 
  • In our application-context file, we have used Centigrade as the implementation class. This can be swapped with Fahrenheit class without any changes in our main class. Thus providing loose coupling.
  • The life cycle management i.e instatntiation/initialization of the Centigrade class is done by the Spring container thus leaving the main class to do it's intended job.
 
Project Structure:
 
Project Structure
 
 
 

Source Code : SpringStandAlone.rar

Web Analytics