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.
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.
Step 3: Redefine the HibernateTutorial class to use Spring.
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 CourseServiceImplrespectively.
The interface CourseService
The implementation CourseServiceImpl
Step 2: Modify the HibernateTutorial to inject the CourseService and CourseDao will be injected via the modified Spring configuration shown in Step 3.
Step 3: The config file simpleContext.xml.
Spring is vast, and I have covered more step by step Spring tutorials at Spring concepts and step by step tutorial style examples.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" <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.
1
2
3
4
5
6
7
8
| <beans xmlns= "http://www.springframework.org/schema/beans" 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| 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 CourseServiceImplrespectively.
The interface CourseService
1
2
3
4
5
| package com.mytutorial.service; public interface CourseService { abstract void listAllEnrolments(); } |
The implementation CourseServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| 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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| 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(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
| <beans xmlns= "http://www.springframework.org/schema/beans" 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.
Comments
Post a Comment