In this tutorial you will learn about Spring Dependency Injection (DI) concept with example.
Dependency Injection is a design pattern that allows us to remove dependencies from code so that its easier to maintain and test the code.
Lets try to understand why we really need dependency injection.
Consider two classes Circle and DrawShape as given below.
class Circle{ void drawCircle(){ //some code } } class DrawShape{ void draw(){ Circle obj = new Circle(); obj.drawCircle(); } }
In this example DrawShape class is dependent on Circle class because we have used new keyword to create Circle object. In case we change name of Circle class or its constructor arguments then we have to also do changes in DrawShape class as it uses Circle class.
Assume if the Circle class is used in 100 other classes. In that case we have to do changes in 100 classes and that will require lot of time and efforts. Also we can not test DrawShape class without importing Circle class.
So it becomes difficult to manage and test the code. To solve this problem Dependency Injection concept is used.
In Spring Framework there are two ways to inject dependency.
- Dependency Injection Using Constructor (Constructor Injection)
- Dependency Injection Using Setter Method (Setter Injection)
You can watch below video to understand Dependency Injection easily.
Spring Constructor Injection Example
Create a spring project with following files.
Shape.java
package com.spring; public interface Shape { public void draw(); }
Circle.java
package com.spring; public class Circle implements Shape { @Override public void draw() { System.out.println("Drawing Circle...."); } }
DrawShape.java
package com.spring; public class DrawShape { Shape shape; DrawShape(Shape shape){ this.shape = shape; } public void drawShape(){ shape.draw(); } }
TestClass.java
package com.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestClass { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("configuration.xml"); DrawShape obj = (DrawShape) context.getBean("drawShapeBean"); obj.drawShape(); } }
configuration.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.xsd"> <bean id="drawShapeBean" class="com.spring.DrawShape"> <constructor-arg ref="circleBean"/> </bean> <bean id="circleBean" class="com.spring.Circle"></bean> </beans>
In above code I have used <constructor-arg> tag, it is used to inject Circle class object in DrawShape constructor at runtime.
Output
When you will run the project it will show following output in ide console.
Spring Setter Method Injection Example
We simply replace constructor with setter method setShape() in DrawShape class.
package com.spring; public class DrawShape { Shape shape; public void setShape(Shape shape){ this.shape = shape; } public void drawShape(){ shape.draw(); } }
Also replace <constructor-arg> tag with <property> tag in configuration.xml file.
<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="drawShapeBean" class="com.spring.DrawShape"> <property name="shape" ref="circleBean"/> </bean> <bean id="circleBean" class="com.spring.Circle"></bean> </beans>
The <property> tag inject the Circle object in setter method of DrawShape class at runtime.
All other code will be same as used in construction injection example.
Comment below in case you are facing any difficulty to understand Spring Dependency Injection concept.
thanks for giving such a nice information with this coding example,
very good post.
good luck