DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection.
Employee.java
package com.jsl.spring.di;
public class Employee {
private String empno;
private String ename;
private Address address;
public void setEmpno(String empno) {
this.empno = empno;
}
public String getEmpno() {
return empno;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getEname() {
return ename;
}
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return address;
}
}
Address.java
package com.jsl.spring.di;
public class Address {
private String city;
private String state;
private String country;
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setState(String state) {
this.state = state;
}
public String getState() {
return state;
}
public void setCoutry(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
}
MainClass.java
package com.jsl.spring.di;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("employee.xml");
Employee emp=context.getBean(Employee.class);
System.out.println("The Employee Details are : ");
System.out.println("Empno is :"+emp.getEmpno());
System.out.println("Ename is :"+emp.getEname());
System.out.println("City is :"+emp.getAddress().getCity());
System.out.println("State is :"+emp.getAddress().getState());
System.out.println("Country is :"+emp.getAddress().getCoutry());
}
}
employee.xml
The output is :
The Employee Details are : Empno is :1001 Ename is :JSLTech City is :Bangalore State is :Karnataka Country is :India
Constructor Injection
Employee.java
package com.jsl.spring.di;
public class Employee {
private String empno;
private String ename;
private Address address;
public Employee(String empno,String ename,Address address){
this.empno=empno;
this.ename=ename;
this.address=address;
}
public String getEmpno() {
return empno;
}
public String getEname() {
return ename;
}
public Address getAddress() {
return address;
}
}
Address.java
package com.jsl.spring.di;
public class Address {
private String city;
private String state;
private String country;
public Address(String city,String state,String country) {
this.city = city;
this.state=state;
this.country=country;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getCountry() {
return country;
}
}
MainClass.java
package com.jsl.spring.di; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("employee.xml");
Employee emp=context.getBean(Employee.class);
System.out.println("The Employee Details are : ");
System.out.println("Empno is :"+emp.getEmpno());
System.out.println("Ename is :"+emp.getEname());
System.out.println("City is :"+emp.getAddress().getCity());
System.out.println("State is :"+emp.getAddress().getState());
System.out.println("Country is :"+emp.getAddress().getCountry());
}
}
employee.xml
The output is : The Employee Details are : Empno is :1001 Ename is :JSLTech City is :Bangalore State is :Karnataka Country is :India
No comments:
Post a Comment