Setter Dependency Injection
This post will show how to use Setter dependency injection in Spring IOC. As in the earlier post I already mentioned about the dependency injection and its types. Please refer the below link for more details:-
In case of setter dependency injection, container calls the setter methods on the bean class after executing a factory method or no-argument constructor for the instantiation of our beans or spring container (IOC container) will inject the dependencies for using the setter methods in a bean class. This is more preferable dependency injection as compare to constructor dependency injection.
Example of Setter Dependency injection:-
System Requirements:-
- Eclipse Editor or any other.
- JDK 1.5 or higher(I am using jdk 1.7.0_03)
- Spring jars.
Required Jars are:-
spring-core.jar
spring-2.5.jar
spring-beans.jar
spring-context.jar
commons-logging.jar
dom4j-1.4.jar
antlr.jar
log4j.jar
Steps for creating Eclipse java project for implementing setter dependency injection:-
- Create a java project in eclipse and name it as SpringSetterInjectionImplementation.
- Create a package in the src folder with the name as com.spring.gauarv.setterinjection.example.
- Create the below files in this package and place the corresponding code in those files.
- Create an XML file and name it as setterinjection-beans.xml and place this file in the classpath outside the src folder.
- Execute the SetterInjectionClient.java by selecting the option Run as Java Application.
CreateProject.java
package com.spring.gauarv.setterinjection.example;
public class CreateProject {
public CreateProject() {
System.out.println("INSIDE CREATEPROJECT CLASS CONSTRUCTOR");
}
public void createJavaProject() {
System.out.println("INSIDE CREATEJAVAPROJECT() METHOD");
}
}
public class CreateProject {
public CreateProject() {
System.out.println("INSIDE CREATEPROJECT CLASS CONSTRUCTOR");
}
public void createJavaProject() {
System.out.println("INSIDE CREATEJAVAPROJECT() METHOD");
}
}
EclipseTool.java
package com.spring.gauarv.setterinjection.example;
public class EclipseTool {
private CreateProject createProject;
/**
* @return the createProject
*/
public CreateProject getCreateProject() {
return createProject;
}
/**
* @param createProject
* the createProject to set
*/
public void setCreateProject(CreateProject createProject) {
this.createProject = createProject;
}
public void javaProjectCreation() {
createProject.createJavaProject();
}
}
public class EclipseTool {
private CreateProject createProject;
/**
* @return the createProject
*/
public CreateProject getCreateProject() {
return createProject;
}
/**
* @param createProject
* the createProject to set
*/
public void setCreateProject(CreateProject createProject) {
this.createProject = createProject;
}
public void javaProjectCreation() {
createProject.createJavaProject();
}
}
setterinjection-beans.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-3.0.xsd">
<bean id="eclipseTool" class="com.spring.gauarv.setterinjection.example.EclipseTool">
<property name="createProject">
<bean id="createProject"
class="com.spring.gauarv.setterinjection.example.CreateProject" />
</property>
</bean>
</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-3.0.xsd">
<bean id="eclipseTool" class="com.spring.gauarv.setterinjection.example.EclipseTool">
<property name="createProject">
<bean id="createProject"
class="com.spring.gauarv.setterinjection.example.CreateProject" />
</property>
</bean>
</beans>
SetterInjectionClient.java
package com.spring.gauarv.setterinjection.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetterInjectionClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"setterinjection-beans.xml");
EclipseTool eclipseTool = (EclipseTool) context.getBean("eclipseTool");
eclipseTool.javaProjectCreation();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetterInjectionClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"setterinjection-beans.xml");
EclipseTool eclipseTool = (EclipseTool) context.getBean("eclipseTool");
eclipseTool.javaProjectCreation();
}
}
Project structure :-
Result:-
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
INSIDE CREATEPROJECT CLASS CONSTRUCTOR
INSIDE CREATEJAVAPROJECT() METHOD
log4j:WARN Please initialize the log4j system properly.
INSIDE CREATEPROJECT CLASS CONSTRUCTOR
INSIDE CREATEJAVAPROJECT() METHOD
No comments:
Post a Comment