JDK动态代理

实例:为程序添加日志

1. ArithmeticCaluda.java

 1 public interface ArithmeticCaluda {
 2 
 3     /**
 4      * 加
 5      * @param a
 6      * @param b
 7      * @return 8      */
 9     double add(double a, double b);
10 
11     /**
12      * 减
13      * @param a
14      * @param b
15      * @return16      */
17     double sub(double a, double b);
18 
19     /**
20      * 乘
21      * @param a
22      * @param b
23      * @return24      */
25     double mul(double a, double b);
26 
27     /**
28      * 除
29      * @param a
30      * @param b
31      * @return32      */
33     double div(double a, double b);
34 
35 }

2.ArithmeticCaludaImp.java

 1 public class ArithmeticCaludaImp implements ArithmeticCaluda {
 2     /**
 3      * 加
 4      * @param a
 5      * @param b
 6      * @return 7      */
 8     public double add(double a,double b){
 9         double result=a+b;
10         return result;
11     }
12     /**
13      * 减
14      * @param a
15      * @param b
16      * @return17      */
18     public double sub(double a,double b){
19         double result=a-b;
20         return result;
21     }
22     /**
23      * 乘
24      * @param a
25      * @param b
26      * @return27      */
28     public double mul(double a,double b){
29         double result=a*b;
30         return result;
31     }
32     /**
33      * 除
34      * @param a
35      * @param b
36      * @return37      */
38     public double div(double a,double b){
39         double result=a/b;
40         return result;
41     }
42 }

3.LogProxy.java

 1 import java.lang.reflect.InvocationHandler;
 2 import java.lang.reflect.Method;
 3 import java.lang.reflect.Proxy;
 4 import java.util.Arrays;
 5 
 6 public class LogProxy implements InvocationHandler{
 7     //被代理对象
 8     private Object target;
 9 
10     public LogProxy(Object target) {
11         super();
12         this.target = target;
13     }
14     //得到代理对象
15     public Object getProxy(){
16         
17         //ClassLoader对象,被代理对象进行加载
18         ClassLoader loader=target.getClass().getClassLoader();
19         
20         //Interface接口的数组,被代理的对象实现的所有接口
21         Class[] interfaces=target.getClass().getInterfaces();
22         
23         //InvocationHandler对象,代理实例的调用处理程序
24         InvocationHandler h=this;
25         
26         return Proxy.newProxyInstance(loader, interfaces, h);
27     }
28     
29     /**
30      * proxy:     代理对象
31      * method:   要调用被代理的方法的Method对象
32      * args:      调用方法时接受的参数
33      */
34     @Override
35     public Object invoke(Object proxy, Method method, Object[] args)
36             throws Throwable {
37         System.out.println("the "+method.getName()+" method begin with "+Arrays.asList(args));
38         double result=(double) method.invoke(target, args);
39         System.out.println("the "+method.getName()+" method end with ["+result+"]");
40         return result;
41     }
42 }

4.Test.java

 1 import com.eduask.liusheng.aop.proxy.ArithmeticCaluda;
 2 import com.eduask.liusheng.aop.proxy.ArithmeticCaludaImp;
 3 
 4 public class Test {
 5     public static void main(String[] args) {
 6         ArithmeticCaluda target=new ArithmeticCaludaImp();
 7         LogProxy logProxy=new LogProxy(target);
 8         ArithmeticCaluda proxy=(ArithmeticCaluda) logProxy.getProxy();
 9         
10         double result=proxy.add(10, 10);
11         System.out.println(result);
12         
13         result=proxy.sub(10, 10);
14         System.out.println(result);
15         
16         result=proxy.mul(10, 10);
17         System.out.println(result);
18         
19         result=proxy.div(10, 10);
20         System.out.println(result);
21     }
22 }

运行结果:

the add method begin with [10.0, 10.0]
the add method end with [20.0]
20.0

the sub method begin with [10.0, 10.0]
the sub method end with [0.0]
0.0

the mul method begin with [10.0, 10.0]
the mul method end with [100.0]
100.0

the div method begin with [10.0, 10.0]
the div method end with [1.0]
1.0

 

JDK的动态代理依靠接口实现,如果有些类并没有实现接口,则不能使用JDK代理

Proxy类是专门完成代理的操作类,可以通过此类为一个或多个接口动态地生成实现类

posted @ 2017-07-12 12:35  若你喜欢怪人  阅读(101)  评论(0编辑  收藏  举报