Spring framework provides one of the good mechanism of method injection. Method injection means changing methods from class. Means on call of that particular method we can execute another method which is not in that class. We generally use this mechanism when we have some licensed code and we are not running with authority to change code but we are in need to change just one particular method.
Here I am giving short demo with my eclipse IDE. You can download this IDE from http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/installers/myeclipse-10.7-offline-installer-windows.exe here.
Now start the IDE and keep all default settings.
1. Create simple java project.
2. Now right click on project go to "my eclipse" option and in that select "add Spring capabilities".
Select 2.0 version of spring framework and finish it.
3. Now open your project and you will see applicationContext.xml file under "src" folder.
4. Open that file and past following code into that,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "emp" class = "testpkg.Employee">
<replaced-method name = "empSal" replacer="employeeReplacer"/>
</bean>
<bean id= "employeeReplacer" class = "testpkg.EmployeeReplacer"/>
</beans>
5. Now create new package called "testpkg".
6. In that package create class called "Employee" and past following code,
package testpkg;
public class Employee {
public String empSal() {
return "bonus";
}
}
7. Now create class "EmployeeReplacer" and past following code in it,
package testpkg;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class EmployeeReplacer implements MethodReplacer{
@Override
public Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
return "extra bonus";
}
}
8. Now create "Main" class in the same package and past following code in it,
package testpkg;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee ms = (Employee) ctx.getBean("emp");
String str = ms.empSal();
System.out.println("This is return : " + str);
}
}
Now if you are executing this code then in normal situation it must print "This is return : bonus".
But because of spring method injection now it will print "This is return : extra bonus". In this way we are executing some other method on behalf of class method.
Here I am giving short demo with my eclipse IDE. You can download this IDE from http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/installers/myeclipse-10.7-offline-installer-windows.exe here.
Now start the IDE and keep all default settings.
1. Create simple java project.
2. Now right click on project go to "my eclipse" option and in that select "add Spring capabilities".
Select 2.0 version of spring framework and finish it.
3. Now open your project and you will see applicationContext.xml file under "src" folder.
4. Open that file and past following code into that,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "emp" class = "testpkg.Employee">
<replaced-method name = "empSal" replacer="employeeReplacer"/>
</bean>
<bean id= "employeeReplacer" class = "testpkg.EmployeeReplacer"/>
</beans>
5. Now create new package called "testpkg".
6. In that package create class called "Employee" and past following code,
package testpkg;
public class Employee {
public String empSal() {
return "bonus";
}
}
7. Now create class "EmployeeReplacer" and past following code in it,
package testpkg;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class EmployeeReplacer implements MethodReplacer{
@Override
public Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
return "extra bonus";
}
}
8. Now create "Main" class in the same package and past following code in it,
package testpkg;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee ms = (Employee) ctx.getBean("emp");
String str = ms.empSal();
System.out.println("This is return : " + str);
}
}
Now if you are executing this code then in normal situation it must print "This is return : bonus".
But because of spring method injection now it will print "This is return : extra bonus". In this way we are executing some other method on behalf of class method.
No comments:
Post a Comment