In Spring, the Inversion of Control (IoC) principle is implemented using the Dependency Injection (DI) design pattern. But note that In spring framework both terms are used interchangeably. Let's understand dependency injection with the help of an example.
Example of Tight Coupling
Let's have a look at the class diagram of this example. The green arrows indicate generalization and the blue arrows indicates association.
As you can see this architecture is tightly coupled. We create an instance of the QuizMaster in the QuizMasterService class in the following way.
So it is tightly coupled. Also we have to hard-code the stuff here. This is not good for development environments, because we have again and again change code for some simple stuff. Now lets see how we can avoid this by using the Dependency Injection design pattern.
How to reduce tight coupling here?
So the solution we can think of is either use factory pattern. But still we will see another method called DI or dependency injection is far better than factory pattern.
The container comes into picture and it helps in injecting the dependancies.
The bean configuration is done in the beans.xml file.
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 ref tag.
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.
Example of Tight Coupling
The QuizMater interface exposes the popQuestion() method. To keep things simple, our QuizMaster will generate only one question.
Getting the interface ready
Getting the implementations ready
The StrutsQuizMaster and the SpringQuizMaster class implements QuizMaster interface and they generate questions related to struts and spring respectively.
Getting the interface ready
QuizMaster.java
package com.vaani.bean.quizmaster;
public interface QuizMaster {
public String popQuestion();
}
The StrutsQuizMaster and the SpringQuizMaster class implements QuizMaster interface and they generate questions related to struts and spring respectively.
StrutsQuizMaster.java
public class StrutsQuizMaster implements QuizMaster {
@Override
public String popQuestion() {
return "Are you new to Struts?";
}
}
SpringQuizMaster.java
public class SpringQuizMaster implements QuizMaster {We have a QuizMasterService class that displays the question to the user. The QuizMasterService class holds reference to the QuizMaster.
@Override
public String popQuestion() {
return "Are you new to Spring?";
}
}
public class QuizMasterService {Finally we create the QuizProgram class to conduct quiz.
private QuizMaster quizMaster = new SpringQuizMaster();
public void askQuestion()
{
System.out.println(quizMaster.popQuestion());
}
}
public class QuizProgram {As you can see it is pretty simple, here we create an instance of the QuizMasterService class and call the askQuestion() method. When you run the program as expected "Are you new to Spring?" gets printed in the console.
public static void main(String[] args) {
QuizMasterService quizMasterService = new QuizMasterService();
quizMasterService.askQuestion();
}
}
Let's have a look at the class diagram of this example. The green arrows indicate generalization and the blue arrows indicates association.
As you can see this architecture is tightly coupled. We create an instance of the QuizMaster in the QuizMasterService class in the following way.
private QuizMaster quizMaster = new SpringQuizMaster();To make our quiz master Struts genius we need to make modifications to the QuizMasterService class like this.
private QuizMaster quizMaster = new StrutsQuizMaster();
So it is tightly coupled. Also we have to hard-code the stuff here. This is not good for development environments, because we have again and again change code for some simple stuff. Now lets see how we can avoid this by using the Dependency Injection design pattern.
How to reduce tight coupling here?
So the solution we can think of is either use factory pattern. But still we will see another method called DI or dependency injection is far better than factory pattern.
Reducing coupling via Factory Pattern
Now one solution to this is factory pattern.We can make a factory class, which produces all beans for us.
Beans in the example there were SpringQuizMaster and StrutsQuizMaster.
First of all we can create the factory:
Now you should get your service ready.And in service we can simply do this:
But still if there is still new requirement, to add other beans like HibernateQuizMaster...we have to edit this factory class as well, so still have to build and all that stuff. So here comes spring in picture.
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.
So now we do this by spring to reduce coupling.
Beans in the example there were SpringQuizMaster and StrutsQuizMaster.
First of all we can create the factory:
public class QuizMasterFactory {
public static QuizMaster getQuizMaster(String subject)
{
if("Spring".equals(subject))
return new SpringQuizMaster();
else if("Struts".equals(subject))
return new StrutsQuizMaster();
else
throw new IllegalArgumentException("No subject exists with name "+subject);
}
}
public class QuizMasterServiceFromFactory {Now in main method we can do:
QuizMaster quizMaster;
public void setQuizMaster(QuizMaster quizMaster) {
this.quizMaster = quizMaster;
}
public void askQuestion()
{
System.out.println(quizMaster.popQuestion());
}
}
public static void main(String[] args) {
QuizMaster quizMaster = QuizMasterFactory.getQuizMaster("Spring");
QuizMasterServiceFromFactory quizMasterService = new QuizMasterServiceFromFactory();
quizMasterService.setQuizMaster(quizMaster);
quizMasterService.askQuestion();
}
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.
So now we do this by spring to reduce coupling.
Spring Method or reducing coupling via DI or IOC
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.
Now consider this service, which refer these beans, but have now nothing to worry about how to create beans :
Now consider this service, which refer these beans, but have now nothing to worry about how to create beans :
public class QuizMasterServiceSpring {
QuizMaster quizMaster;
public void setQuizMaster(QuizMaster quizMaster) {
this.quizMaster = quizMaster;
}
public void askQuestion()
{
System.out.println(quizMaster.popQuestion());
}
}
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.
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="springQuizMaster" class="com.vaani.spring.quizmaster.SpringQuizMaster"></bean>
<bean id="strutsQuizMaster" class="com.vaani.spring.quizmaster.StrutsQuizMaster"></bean>
<bean id="quizMasterService" class="com.vaani.spring.quizmaster.QuizMasterService">
<property name="quizMaster">
<ref local="springQuizMaster"/>
</property>
</bean>
</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 ref tag.
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.
<bean id="quizMasterService" class="com.vaani.spring.quizmaster.QuizMasterService">
<property name="quizMaster">
<ref local="strutsQuizMaster"/>
</property>
</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.
To execute this example add the following jar files to the classpath.
- antlr-runtime-3.0
- commons-logging-1.0.4
- org.springframework.asm-3.0.0.M3
- org.springframework.beans-3.0.0.M3
- org.springframework.context-3.0.0.M3
- org.springframework.context.support-3.0.0.M3
- org.springframework.core-3.0.0.M3
- org.springframework.expression-3.0.0.M3
So instead of changing code in java, we have to change the xml only, whenever the need be. Also from the spring example, it was clear why we call it inversion of control. Because we needed beans in the service, we have to first define and initiate these beans, and then give it to service class. But beauty of spring is that we don't have to worry about how spring will initialize these beans. We can provide beans in any order and spring will determine how to initialize those. See other dependency injection frameworks.
Download the Source
You can download the source code with "Tight coupled" Quizmaster here.
You can download the source code of Factory method pattern solving coupling problem here.
Also you can finally download the whole example with spring from here.
No comments:
Post a Comment