Spring 高级 jdk代理原理-进阶篇
一、获取目标方法的返回
package com.mangoubiubiu.show.a13; import java.lang.reflect.Method; public class A13 { interface Foo{ void foo() ; int bar() ; } interface InvocationHandler{ Object invoke(Object proxy,Method method,Object[] args ) throws Throwable; } static class Target implements Foo{ @Override public void foo() { System.out.println("target foo"); } @Override public int bar() { System.out.println("target bar"); return 100; } } public static void main(String [] param) throws Throwable { Foo proxy = new $Proxy0((Object p,Method method,Object[] args ) -> { System.out.println("before"); return method.invoke(new Target(),args); }); proxy.foo(); proxy.bar(); } }
package com.mangoubiubiu.show.a13; import java.lang.reflect.Method; public class A13 { interface Foo{ void foo() ; int bar() ; } interface InvocationHandler{ Object invoke(Object proxy,Method method,Object[] args ) throws Throwable; } static class Target implements Foo{ @Override public void foo() { System.out.println("target foo"); } @Override public int bar() { System.out.println("target bar"); return 100; } } public static void main(String [] param) throws Throwable { Foo proxy = new $Proxy0((Object p,Method method,Object[] args ) -> { System.out.println("before"); return method.invoke(new Target(),args); }); proxy.foo(); int bar = proxy.bar(); System.out.println("目标对象的返回结果是"+bar); } }
二、使用jdk自带的InvocationHandler
package com.mangoubiubiu.show.a13; import java.lang.reflect.Method; public class A13 { interface Foo{ void foo() ; int bar() ; } // interface InvocationHandler{ // Object invoke(Object proxy,Method method,Object[] args ) throws Throwable; // } static class Target implements Foo{ @Override public void foo() { System.out.println("target foo"); } @Override public int bar() { System.out.println("target bar"); return 100; } } public static void main(String [] param) throws Throwable { Foo proxy = new $Proxy0((Object p,Method method,Object[] args ) -> { System.out.println("before"); return method.invoke(new Target(),args); }); proxy.foo(); int bar = proxy.bar(); System.out.println("目标对象的返回结果是"+bar); } }
package com.mangoubiubiu.show.a13; import com.mangoubiubiu.show.a13.A13.Foo; import com.mangoubiubiu.show.a13.A13.Target; //import com.mangoubiubiu.show.a13.A13.InvocationHandler; import org.springframework.cglib.proxy.UndeclaredThrowableException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class $Proxy0 extends Proxy implements Foo { public $Proxy0(InvocationHandler h){ super(h); } @Override public void foo() { try { h.invoke(this,foo,new Object[0]); } catch (RuntimeException | Error e) { throw e; } catch (Throwable e){ throw new UndeclaredThrowableException(e); } } @Override public int bar() { try { Object result = h.invoke(this,bar, new Object[0]); return (int) result; } catch (RuntimeException | Error e) { throw e; } catch (Throwable e){ throw new UndeclaredThrowableException(e); } } static Method foo; static Method bar; static { try { foo = Foo.class.getMethod("foo"); bar = Foo.class.getMethod("bar"); } catch (NoSuchMethodException e) { throw new NoSuchMethodError(e.getMessage()); } } }