In this tutorial you will learn about the Inversion Cf Control
In this tutorial you will learn about the Inversion Cf ControlThe Spring framework provides a powerful container that manages the components. This container is based on the Inversion Of Control and it is implemented by Dependency Injection design pattern. The container is responsible for choosing the resources here.
An example of IOC is given below please consider the example
Create an InterfaceEmployee.java
package net.roseindia; public interface Employee { public String showDetail(); }
PartimeEmployee.java
package net.roseindia; public class PartimeEmployee implements Employee { @Override public String showDetail() { return "I Am A Part Time Employee"; } }
FulltimeEmployee.java
package net.roseindia; public class FulltimeEmployee implements Employee { @Override public String showDetail() { return "I Am Full Time Employee"; } }
EmployeeService.java
package net.roseindia; public class EmployeeService { Employee employee; public void setEmployee(Employee employee) { this.employee = employee; } public String printDetail() { return employee.showDetail(); } }
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="fulltimeEmployee" class="net.roseindia.FulltimeEmployee"></bean> <bean id="partimeEmployee" class="net.roseindia.PartimeEmployee"></bean> <bean id="employeeService" class="net.roseindia.EmployeeService"> <property name="employee"> <ref local="partimeEmployee"/> </property> </bean> </beans>
MainClaz.java
package net.roseindia; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainClaz { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml"); EmployeeService employeeService = (EmployeeService) context.getBean("employeeService"); System.out.println(employeeService.printDetail()); } }
Am A Part Time Employee |