Spring 3 Dependency Injection via Constructor and Setter
Spring Framework implementation of the Inversion of Control
(IoC) principle, that is also known as Dependency Injection (DI). Dependency
injection (DI) is a process whereby objects define their dependencies, that is,
the other objects they work with, only through constructor arguments, arguments
to a factory method, or properties that are set on the object instance after it
is constructed or returned from a factory method. The container then injects
those dependencies when it creates the bean. DI exists in two major variants,
Constructor-based dependency injection and Setter-based dependency injection.
Constructor Based DI Example
Employee.java
package smgc;
public class Employee {
private int employeeId;
private
String employeeName;
public
Employee(int employeeId, String employeeName)
{
this.employeeId
= employeeId;
this.employeeName
= employeeName;
}
public int getEmployeeId()
{
return employeeId;
}
public String getEmployeeName()
{
return employeeName;
}
}
EmpMain.java
package smgc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@SuppressWarnings("deprecation")
public class EmpMain {
public static void
main(String[] args) {
//
TODO Auto-generated method stub
Resource
resource=newClassPathResource("Bean.xml");
BeanFactoryfactory=newXmlBeanFactory(resource);
Employee
emp = (Employee) factory.getBean("emp");
System.out.println("employee
Id : " + emp.getEmployeeId());
System.out.println("employee
Name : " + emp.getEmployeeName());
}
}
Bean.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">
<beanid="emp"class="smgc.Employee">
<constructor-argvalue="22"/>
<constructor-argvalue="Piyush"/>
</bean>
</beans>
Setter Based DI Example
Employee.java
packagecom.smgc.springcoreapplication;
publicclass Employee {
privateintemployeeId;
private String
employeeName;
publicintgetEmployeeId()
{
returnemployeeId;
}
publicvoidsetEmployeeId(intemployeeId)
{
this.employeeId
= employeeId;
}
public String
getEmployeeName() {
returnemployeeName;
}
publicvoidsetEmployeeName(String
employeeName) {
this.employeeName
= employeeName;
}
}
EmpMain.java
packagecom.smgc.springcoreapplication;
importorg.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class
EmpMain {
@SuppressWarnings("resource")
public static void main(String[]
args) {
// TODO
Auto-generated method stub
BeanFactorybeanFactory = new
ClassPathXmlApplicationContext("Bean.xml");
Employee employee = (Employee)
beanFactory.getBean("employee");
System.out.println("employee
ID: " + employee.getEmployeeId());
System.out.println("employee
Name : " + employee.getEmployeeName());
}
}
Bean.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">
<beanid="employee"class="com.smgc.springcoreapplication.Employee">
<propertyname="employeeId"value="22"/>
<propertyname="employeeName"value="Piyush"/>
</bean>
</beans>
Comments
Post a Comment