Google

Oct 11, 2012

Spring DI (aka IOC) Tutorial

Thus Spring tutorial is an extension to the Hibernate tutorial where the CourseDao is injected via Spring as opposed to directly instantiating it.

Step 1: As demonstrated in other tutorials, the first step is to have the relevant Spring framework dependency jar files. This is where Maven comes in handy to download and manage dependencies. The pom.xml file in the previous tutorial needs to now include Spring framework related jar files. I have also cleaned up the pom file by defining the version numbers via properties.



<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.mytutorial</groupId>
 <artifactId>simple</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

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

    <!-- define the version numbers -->
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <junit.version>3.8.1</junit.version>
  <hsqldb.version>2.2.8</hsqldb.version>
  <hibernate.version>3.2.6.GA</hibernate.version>
  <hibernate-annotations.version>3.3.1.GA</hibernate-annotations.version>
  <spring.version>3.0.5.RELEASE</spring.version>


 </properties>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>${junit.version}</version>
   <scope>test</scope>
  </dependency>

  <!-- jar that contains the HSQLDB driver class -->
  <dependency>
   <groupId>org.hsqldb</groupId>
   <artifactId>hsqldb</artifactId>
   <version>${hsqldb.version}</version>
  </dependency>

  <!-- GA means General Availability -->
  <!-- Hibernate framework JARS -->
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate</artifactId>
   <version>${hibernate.version}</version>
  </dependency>

  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-annotations</artifactId>
   <version>${hibernate-annotations.version}</version>
  </dependency>


  <!-- Spring 3 dependencies -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>

 </dependencies>
</project>


If you right-mouse-click on the folder "simple", and then select "Maven --> Update Dependencies" you should see the relevant JARs added as shown below.


Step 2: Next step is to define your beans that you want to inject via a XML configuration file know as the spring application context. This file goes in the src/main/resources folder. Make sure this folder is in the classpath. If it is not, right-mouse-click on the "resources" folder and then select "Build Path --> Use as Source Folder". Here is the Spring configuration file simpleContext.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-3.0.xsd">
 
 <bean id="courseDao" class="com.mytutorial.dao.CourseDaoImpl" />
  
</beans>

Step 3: Redefine the HibernateTutorial  class to use Spring.

package com.mytutorial;

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

import com.mytutorial.dao.CourseDao;

public class HibernateTutorial {
 
 public static void main(String[] args) {
  
  //bootstrap spring via its context file
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "simpleContext.xml");
  
  CourseDao cDao = (CourseDao)context.getBean("courseDao");
  cDao.readAllCourses();
 }

}


As you can see, CourseDao is injected via Spring. That's all to it. In reality, there will be a service layer between the DAO layer (e.g. CourseDao) and the class that makes use of the service (e.g. HibernateTutorial). Let's look at this scenario to understand and appreciate DI (i.e. Dependency Injection) and how Spring does it.

Step 1: Define the service interface and implementation classes CourseService and CourseServiceImpl respectively.

The interface CourseService

package com.mytutorial.service;

public interface CourseService {
    abstract void listAllEnrolments();
}


The implementation  CourseServiceImpl

package com.mytutorial.service;

import com.mytutorial.dao.CourseDao;

public class CourseServiceImpl implements CourseService {
 
 //the implementation is injected via Spring's setter injection
 CourseDao dao;

 public void listAllEnrolments() {
  dao.readAllCourses();
 }

 public CourseDao getDao() {
  return dao;
 }

 //Spring IOC container invokes this method
 public void setDao(CourseDao dao) {
  this.dao = dao;
 }
 
}

Step 2: Modify the HibernateTutorial to inject the CourseService and CourseDao will be injected via the modified Spring configuration shown in Step 3.

package com.mytutorial;

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

import com.mytutorial.service.CourseService;

public class HibernateTutorial {
 
 public static void main(String[] args) {
  
  //bootstrap spring via its context file
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "simpleContext.xml");
  
  CourseService cService = (CourseService)context.getBean("courseService");
  cService.listAllEnrolments();
 }

}

Step 3: The config file simpleContext.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-3.0.xsd">
 
 <bean id="courseDao" class="com.mytutorial.dao.CourseDaoImpl" />
 
 <bean id="courseService" class="com.mytutorial.service.CourseServiceImpl">
      <property name="dao" ref="courseDao" />
 </bean>
  
</beans>





Spring is vast, and I have covered more step by step Spring tutorials at Spring concepts and step by step tutorial style examples.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home