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()); } } }
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/16727392.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步