In this tutorial we are going to see about Spring setter based dependency injection with example program,
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
The following example shows a class SetterDI that can only be dependency-injected using pure setter injection. Now see the step by step process to create Setter Based Dependency Injection using Spring.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringSetterDI.
3. Add the following jars into your build path.
5. Next create Student class under com.javatutorialscorner.spring package
Student.java
6. Next create SetterDI class under com.javatutorialscorner.spring package
SetterDI.java
7. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
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
The main difference in Beans.xml file defined in constructor-based injection and setter-based injection.
1. The only difference is inside the <bean> element where we have used <constructor-arg> tags for constructor-based injection and <property> tags for setter-based injection.
2. In case you are passing a reference to an object, you need to use ref attribute of <property> tag and if you are passing a value directly then you should use value attribute.
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.
Now you can run the program see the following output in console.
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
The following example shows a class SetterDI that can only be dependency-injected using pure setter injection. Now see the step by step process to create Setter Based Dependency Injection using Spring.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringSetterDI.
3. Add the following jars into your build path.
commons-logging-1.1.3.jar4. Now create package com.javatutorialscorner.spring under SpringSetterDI Project
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
5. 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");
}
}
6. Next create SetterDI class under com.javatutorialscorner.spring package
SetterDI.java
package com.javatutorialscorner.spring;
public class SetterDI {
SetterDI() {
System.out.println("Inside SetterDI() Constructor");
}
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
System.out.println("Inside setStudent()");
this.student = student;
}
public void studentName() {
student.getStudentName();
}
}
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");
SetterDI setterDI = (SetterDI) context.getBean("setterDI");
setterDI.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="setterDI" class="com.javatutorialscorner.spring.SetterDI">
<property name="student" ref="student"></property>
</bean>
<bean id="student" class="com.javatutorialscorner.spring.Student">
</bean>
</beans>
The main difference in Beans.xml file defined in constructor-based injection and setter-based injection.
1. The only difference is inside the <bean> element where we have used <constructor-arg> tags for constructor-based injection and <property> tags for setter-based injection.
2. In case you are passing a reference to an object, you need to use ref attribute of <property> tag and if you are passing a value directly then you should use value attribute.
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.
Now you can run the program see the following output in console.
Inside SetterDI() Constructor
Inside Student() Constructor
Inside setStudent()
Strudent Name : Ram
0 comments:
Post a Comment