Continued Spring IOC


As you can see this architecture is tightly coupled. We create an instance of the QuizMaster in the QuizMasterService class in the following way.
1.private QuizMaster quizMaster = new SpringQuizMaster();
To make our quiz master Struts genius we need to make modifications to the QuizMasterServiceclass like this.
1.private QuizMaster quizMaster = new StrutsQuizMaster();
So it is tightly coupled. Now lets see how we can avoid this by using the Dependency Injectiondesign pattern. The Spring framework provides prowerful container to manage the components. The container is based on the Inversion of Control (IoC) principle and can be implemented by using the Dependency Injection (DI) design pattern. Here the component only needs to choose a way to accept the resources and the container will deliver the resource to the components.
In this example instead of we, directly creating an object of the QuizMaster bean in theQuizMasterService class, we make use of the container to do this job for us. Instead of hard coding any values we will allow the container to inject the required dependancies.
We can inject the dependancies using the setter or constructor injection. Here we will see how we can do this using the setter injection.
01.QuizMasterService.java
02.-----------------------
03.package com.vaannila;
04. 
05.public class QuizMasterService {
06. 
07.QuizMaster quizMaster;
08. 
09.public void setQuizMaster(QuizMaster quizMaster) {
10.this.quizMaster = quizMaster;
11.}
12. 
13.public void askQuestion()
14.{
15.System.out.println(quizMaster.popQuestion());
16.}
17.}
The value for the QuizMaster will be set using the setQuizMaster() method. The QuizMaster object is never instantiated in the QuizMasterService class, but still we access it. Usually this will throw a NullPointerException, but here the container will instantiate the object for us, so it works fine.
After making all the changes, the class diagram of the example look like this.
The container comes into picture and it helps in injecting the dependancies.
The bean configuration is done in the beans.xml file.
01.<?xml version="1.0" encoding="UTF-8"?>
02.<beans xmlns="http://www.springframework.org/schema/beans"
03.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04.xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
05. 
06.<bean id="springQuizMaster" class="com.vaannila.SpringQuizMaster"></bean>
07.<bean id="strutsQuizMaster" class="com.vaannila.StrutsQuizMaster"></bean>
08.<bean id="quizMasterService" class="com.vaannila.QuizMasterService">
09.<property name="quizMaster">
10.<ref local="springQuizMaster"/>
11.</property>
12.</bean>
13. 
14.</beans>
We define each bean using the bean tag. The id attribute of the bean tag gives a logical name to the bean and the class attribute represents the actual bean class. The property tag is used to refer the property of the bean. To inject a bean using the setter injection you need to use the reftag.
Here a reference of SpringQuizMaster is injected to the QuizMaster bean. When we execute this example, "Are you new to Spring?" gets printed in the console.
To make our QuizMaster ask questions related to Struts, the only change we need to do is, to change the bean reference in the ref tag.
1.<bean id="quizMasterService" class="com.vaannila.QuizMasterService">
2.<property name="quizMaster">
3.<ref local="strutsQuizMaster"/>
4.</property>
5.</bean>
In this way the Dependency Injection helps in reducing the coupling between the components.
To execute this example add the following jar files to the classpath.
1.antlr-runtime-3.0
2.commons-logging-1.0.4
3.org.springframework.asm-3.0.0.M3
4.org.springframework.beans-3.0.0.M3
5.org.springframework.context-3.0.0.M3
6.org.springframework.context.support-3.0.0.M3
7.org.springframework.core-3.0.0.M3
8.org.springframework.expression-3.0.0.M3


Comments

Popular Posts