详细场景
为了使署理类和被署理类对第三方有沟通的函数,署理类和被署理类一般实现一个民众的interface,该interface界说如下
java; gutter: true">public interface Calculator {
public Integer add(Integer num1, Integer num2);
public Integer minus(Integer num1, Integer num2);
}
被署理类界说如下
java; gutter: true">public class CalculatorImpl implements Calculator {
@Override
public Integer add(Integer num1, Integer num2) {
int ret = num1 + num2;
System.out.println("in calculatorImpl, res: " + ret);
return ret;
}
@Override
public Integer minus(Integer num1, Integer num2) {
int ret = num1 - num2;
System.out.println("int calculatorImpl, res: " + ret);
return ret;
}
}
署理需求:在add函数和minus函数挪用前后别离输出before invocation和after invocation字样
静态署理办理方案
代码如下:简朴直接,无需赘言,假如calculator里边不只有add和minus,尚有divide,product,log,sin…呢,呵呵哒
public class StaticCalculatorProxy implements Calculator {
Calculator obj;
public StaticCalculatorProxy(Calculator obj) {
this.obj = obj;
}
@Override
public Integer add(Integer num1, Integer num2) {
System.out.println("in StaticCalculatorProxy, before invocation");
Integer ret = obj.add(num1, num2);
System.out.println("in StaticCalculatorProxy, after invocation");
return ret;
}
@Override
public Integer minus(Integer num1, Integer num2) {
System.out.println("in StaticCalculatorProxy, before invocation");
Integer ret = obj.minus(num1, num2);
System.out.println("in StaticCalculatorProxy, after invocation");
return ret;
}
}
动态署理办理方案
首先编写实现InvocationHandler接口的类,用于请求转发,实现如下
public class CalculatorHandler implements InvocationHandler {
private Object obj; //被署理类
public CalculatorHandler(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("in calculatorhandler, before invocation");
Object ret = method.invoke(obj, args); //执行被署理类要领
System.out.println("in calculationhandler, after invocation");
return ret;
}
}
生成动态署理
CalculatorImpl calculatorImpl = new CalculatorImpl();//被署理类 CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorImpl); Calculator calculator = (Calculator) Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler); System.out.println(calculator.add(1,2)); System.out.println(calculator.minus(1, 2));
无论calculator中包括几多函数,动态署理只需实现一次,实际工程中,System.out.println(“in calculatorhandler, before invocation”)大概是加缓存,打日志等操纵
动态署理如何事情的
为了搞清楚动态署理如何事情,首先看看生成的动态署理的代码是什么,借助[1]中ProxyUtil代码
public class ProxyUtils {
/**
* Save proxy class to path
*
* @param path path to save proxy class
* @param proxyClassName name of proxy class
* @param interfaces interfaces of proxy class
* @return
*/
public static boolean saveProxyClass(String path, String proxyClassName, Class[] interfaces) {
if (proxyClassName == null || path == null) {
return false;
}
// get byte of proxy class
byte[] classFile = ProxyGenerator.generateProxyClass(proxyClassName, interfaces);
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(classFile);
out.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
}
获得了生成的动态署理代码如下:
public final class $Proxy0 extends Proxy
implements Calculator
{
public $Proxy0(InvocationHandler invocationhandler)
{
super(invocationhandler);
}
public final boolean equals(Object obj)
{
try
{
return ((Boolean)super.h.invoke(this, m1, new Object[] {
obj
})).booleanValue();
}
catch(Error _ex) { }
catch(Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
public final String toString()
{
try
{
return (String)super.h.invoke(this, m2, null);
}
catch(Error _ex) { }
catch(Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
public final Integer minus(Integer integer, Integer integer1)
{
try
{
return (Integer)super.h.invoke(this, m4, new Object[] {
integer, integer1
});
}
catch(Error _ex) { }
catch(Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
public final Integer add(Integer integer, Integer integer1)
{
try
{
return (Integer)super.h.invoke(this, m3, new Object[] {
integer, integer1
});
}
catch(Error _ex) { }
catch(Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
public final int hashCode()
{
try
{
return ((Integer)super.h.invoke(this, m0, null)).intValue();
}
catch(Error _ex) { }
catch(Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
private static Method m1;
private static Method m2;
private static Method m4;
private static Method m3;
private static Method m0;
static
{
try
{
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {
Class.forName("java.lang.Object")
});
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus", new Class[] {
Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
});
m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add", new Class[] {
Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
});
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
}
catch(NoSuchMethodException nosuchmethodexception)
{
throw new NoSuchMethodError(nosuchmethodexception.getMessage());
}
catch(ClassNotFoundException classnotfoundexception)
{
throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
}
}