package com.agp.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public int plus(int i,int j);
public int minus(int i,int j);
public int multiply(int i,int j);
public int division(int i,int j);
}
public class CalculatorDynamicProxy {
private Calculator cal;
//或者是在InvocationHandler里面或者是在定义的动态代理类里面,必须要有一个接收对应的被代理类的引用。 最终用来作为InvocationHandler method.invoke(target,args)里//面的target被代理类的实例。
public CalculatorDynamicProxy(Calculator cal) {
this.cal = cal;
System.out.println(cal);
}
InvocationHandler ih=new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName=method.getName();
// System.out.println(methodName);
System.out.println("Start excute "+methodName+" with params:"+Arrays.asList(args));
int result=(int) method.invoke(cal, args);
System.out.println("End with result "+result);
return result;
// WHY 不能return this呢。 return this的时候提示
// :com.agp.aop.CalculatorDynamicProxy$1 cannot be cast to java.lang.Integer
// 因为实际代理生成的对象在执行方法是执行 对象.invoke。 如果return this还是return的 proxy对象。对应的这个proxy对象跟计算结果类型肯定不匹配。
}
};
public Calculator getCalculatorProxy(){
ClassLoader cl=ih.getClass().getClassLoader();
Class[] interfaces2=new Class[]{Calculator.class};
// Class<?>[] interfaces=cal.getClass().getInterfaces();
Calculator cal2= (Calculator) Proxy.newProxyInstance(cl, interfaces2, ih);
return cal2;
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public int plus(int i,int j);
public int minus(int i,int j);
public int multiply(int i,int j);
public int division(int i,int j);
}
public class CalculatorDynamicProxy {
private Calculator cal;
//或者是在InvocationHandler里面或者是在定义的动态代理类里面,必须要有一个接收对应的被代理类的引用。 最终用来作为InvocationHandler method.invoke(target,args)里//面的target被代理类的实例。
public CalculatorDynamicProxy(Calculator cal) {
this.cal = cal;
System.out.println(cal);
}
InvocationHandler ih=new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName=method.getName();
// System.out.println(methodName);
System.out.println("Start excute "+methodName+" with params:"+Arrays.asList(args));
int result=(int) method.invoke(cal, args);
System.out.println("End with result "+result);
return result;
// WHY 不能return this呢。 return this的时候提示
// :com.agp.aop.CalculatorDynamicProxy$1 cannot be cast to java.lang.Integer
// 因为实际代理生成的对象在执行方法是执行 对象.invoke。 如果return this还是return的 proxy对象。对应的这个proxy对象跟计算结果类型肯定不匹配。
}
};
public Calculator getCalculatorProxy(){
ClassLoader cl=ih.getClass().getClassLoader();
Class[] interfaces2=new Class[]{Calculator.class};
// Class<?>[] interfaces=cal.getClass().getInterfaces();
Calculator cal2= (Calculator) Proxy.newProxyInstance(cl, interfaces2, ih);
return cal2;
}
//动态代理类,由Proxy.newProxyInstance(cl,interfaces,ih)生成一个proxy对象实例,由于它利用反射技术,拥有interfaces里面的所有方法。 所以java的多态性,就可以强转为
//Calculator等接口实例。 这个proxy实例,执行方法时,实际是将这个方法method 和proxy实例本身 和args传递给 InvocationHandler实例执行invoke方法。
//invoke(Object proxy, Method method, Object[] args) 。invoke方法调用时调用实际的接口实现类的对应的方法。 然后将返回值返回给上层。
}