728x90 AdSpace

Monday, 7 April 2014

Spring Constructor-based dependency injection

In this tutorial we are going to see about Spring Constructor-based dependency injection with example program.
Constructor-based Dependency Injection
Constructor-based DI is accomplished by the container invoking a constructor with a number of arguments, each representing a dependency. Calling a static factory method with specific arguments to construct the bean is nearly equivalent, and this discussion treats arguments to a constructor and to a static factory method similarly.
The following example class ConstructorDI that can only be dependency-injected with constructor injection. Notice that there is nothing special about this class, it is a POJO that has no dependencies on container specific interfaces, base classes or annotations.
Constructor argument resolution
See about  - Spring DI – Constructor Argument Resolution.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringConstructorDI.
3. Add the following jars into your build path.
commons-logging-1.1.3.jar
spring-beans-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar
spring-context-support-3.2.6.RELEASE.jar
spring-core-3.2.6.RELEASE.jar
spring-expression-3.2.6.RELEASE.jar
4. Now create package com.javatutorialscorner.spring under SpringConstructorDI Project
5. Next create ConstructorDI class under com.javatutorialscorner.spring package
ConstructorDI.java
package com.javatutorialscorner.spring;

public class ConstructorDI {
private Student student;

ConstructorDI(Student student) {
System.out.println("Inside ConstructorDI() Constructor");
this.student = student;
}

public void studentName() {
student.getStudentName();
}

}

6. Next create Student class under com.javatutorialscorner.spring package

Student.java

package com.javatutorialscorner.spring;

public class Student {
Student() {
System.out.println("Inside Student() Constructor");
}

void getStudentName() {
System.out.println("Strudent Name : Ram");
}
}


7. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file

RunApp.java

package com.javatutorialscorner.spring;

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

public class RunApp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ApplicationContext context = new ClassPathXmlApplicationContext(
"Beans.xml");
ConstructorDI constructorDI = (ConstructorDI) context.getBean("constructorDI");
constructorDI.studentName();
}

}

In above program

First step I created Application context using ClassPathXmlApplicationContext  which load configuration from bean file  which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml

Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.

Once Object Created you can access any method from that class.

8. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).

Bean.xml

<?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="constructorDI" class="com.javatutorialscorner.spring.ConstructorDI">
<constructor-arg ref="student" />
</bean>
<bean id="student" class="com.javatutorialscorner.spring.Student">
</bean>
</beans>

Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.

Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to   variable without changing the class file.
The is used to inject constructor argument.

Now you can run the program see the following output in console.

Inside Student() Constructor
Inside ConstructorDI() Constructor
Strudent Name : Ram
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Spring Constructor-based dependency injection Rating: 5 Reviewed By: Annamalai Thangaraj