JdkProxy

 1 package com.wonders.proxy.jdk;
 2 
 3 import java.lang.reflect.InvocationHandler;
 4 import java.lang.reflect.Method;
 5 import java.lang.reflect.Proxy;
 6 
 7 /**
 8  * @author : yushibin
 9  * @version : 1.0
10  * @date : 2021-03-16 23:06
11  * @description :
12  */
13 public class TargetJdkProxy implements InvocationHandler {
14 
15     Object target;
16 
17     public TargetJdkProxy(Object target) {
18         this.target = target;
19     }
20 
21     public Object getTargetProxy() {
22 
23         Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
24 
25         return proxy;
26     }
27     
28     @Override
29     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
30 
31         System.out.println("target method execute before......");
32 
33         Object obj = method.invoke(target, args);
34 
35         System.out.println("target method execute after......");
36 
37         return obj;
38     }
39 }

 

posted @ 2021-03-17 20:49  发丝有些凌乱丶  阅读(53)  评论(1编辑  收藏  举报