Spring Example to load Application Context using @Config and bean creation using @Bean annotations

 

                            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 AnnotationConfigApplicationContext. Here we will define the bean in the context file rather than in the application context xml file.    
 
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 class which is annotated with @Configuration.
 
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:
    Create Context class with @Configuration which indicates that this class will act as an configuration file. 
@Bean(name="temperatureBean") - creates a Centigrade bean with id "temperatureBean"
 
 
package com.javavision.context;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javavision.SpringStandAlone.model.Centigrade;

@Configuration
public class Context {

	@Bean (name="temperatureBean") 
	   public Centigrade createCentigradeBean(){
	      return new Centigrade();
	   }
	
}
 
This is Equivalent to :
 
	<bean id="temperatureBean" class="com.javavision.SpringStandAlone.model.Centigrade" />
 
 
 
Step 5:
 
  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));

	}

}
 
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"
        xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<context:annotation-config/>
	
</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:
 
 
 
Spring @Configuration + @Bean example
 
Source Code : SpringStandAlone-Annotation.rar
Web Analytics