Spring framework with AOP show many magic.One of them is Method Replacer .In this activity the called method is replaced by another method. To understand clearly have a good example-
Directory Structure-
Class Ract.java-
package com.test;
public class Ract {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent()
{
return "hello methd";
}
}
class Triangle.java-
package com.test;
public class Triangle {
private String type;
private int height;
private Ract ract;
public Ract getRact() {
return ract;
}
public void setRact(Ract ract) {
this.ract = ract;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getContent()
{
return "pre method";
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void draw()
{
System.out.println( getType()+"Triangle.\n The height is="+getHeight() );
System.out.println("ractangle is="+ract.getName());
System.out.println("getcontent method="+getContent());
}
}
Class MethodReplace.java-
package com.test;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class MethodReplace implements MethodReplacer {
@Override
public Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
return "huree!!!!I m Replaced";
}
}
class DrawApp.java-
package com.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawApp {
public static void main(String a[])
{
AbstractApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();//callsdestroy method dow the rsorce
Triangle tri= (Triangle) context.getBean("triangle");
tri.draw();
}
}
spring.xml -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="ract" class="com.test.Ract">
<property name="name" value="navin's"></property>
</bean>
<bean id="mrp" class="com.test.MethodReplace"></bean>
<bean id="triangle" class="com.test.Triangle">
<property name="type" value="test"></property>
<property name="height" value="2"></property>
<property name="ract" ref="ract"></property>
<replaced-method name="getContent" replacer="mrp" />
</bean>
</beans>
Output-
testTriangle.
The height is=2
ractangle is=navin's
getcontent method=huree!!!!I m Replaced
Directory Structure-
Class Ract.java-
package com.test;
public class Ract {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent()
{
return "hello methd";
}
}
class Triangle.java-
package com.test;
public class Triangle {
private String type;
private int height;
private Ract ract;
public Ract getRact() {
return ract;
}
public void setRact(Ract ract) {
this.ract = ract;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getContent()
{
return "pre method";
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void draw()
{
System.out.println( getType()+"Triangle.\n The height is="+getHeight() );
System.out.println("ractangle is="+ract.getName());
System.out.println("getcontent method="+getContent());
}
}
Class MethodReplace.java-
package com.test;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class MethodReplace implements MethodReplacer {
@Override
public Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
return "huree!!!!I m Replaced";
}
}
class DrawApp.java-
package com.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawApp {
public static void main(String a[])
{
AbstractApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();//callsdestroy method dow the rsorce
Triangle tri= (Triangle) context.getBean("triangle");
tri.draw();
}
}
spring.xml -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="ract" class="com.test.Ract">
<property name="name" value="navin's"></property>
</bean>
<bean id="mrp" class="com.test.MethodReplace"></bean>
<bean id="triangle" class="com.test.Triangle">
<property name="type" value="test"></property>
<property name="height" value="2"></property>
<property name="ract" ref="ract"></property>
<replaced-method name="getContent" replacer="mrp" />
</bean>
</beans>
Output-
testTriangle.
The height is=2
ractangle is=navin's
getcontent method=huree!!!!I m Replaced
For Further Reading,
0 comments:
Post a Comment