Java面向切面编程AOP---动态代理实现

       上一篇论述AOP动态代理的原理 这里显示实现其中的一种动态代理实现
       需求:矩形计算周长

       先有矩形的接口类:

package com.mrchen.myredistest.chenstest;

/**
 * 矩形 计算宽高
 */
public interface Rectangle {
     /**
      * 加法 a+b
      * @param a 被加数
      * @param b 加数
      * @return 和
      */
     double add(double a,double b);

     /**
      * 乘法
      * @param a 被乘数
      * @param b 乘数
      * @return 积
      */
     double multiplication(double a,double b);

     /**
      * 周长

      * @return  周长
      */
     double calculatePerimete();
}

   

 

   矩形的实现类:

package com.mrchen.myredistest.chenstest.shape;

import com.mrchen.myredistest.chenstest.Rectangle;

public class RectangleImpl implements Rectangle {
    private double mWith;
    private double mHeight;

    public RectangleImpl(double mWith, double mHeight) {
        this.mWith = mWith;
        this.mHeight = mHeight;
    }

    @Override
    public double add(double a, double b) {
        return a+b;
    }

    @Override
    public double multiplication(double a, double b) {
        return a*b;
    }

    @Override
    public double calculatePerimete() {
       double he= add(mWith,mHeight);
       double ji=multiplication(he,2);
        return ji;
    }
}

 

      程序入口类 计算周长:

package com.mrchen.myredistest.chenstest;

import com.mrchen.myredistest.chenstest.shape.RectangleImpl;

public class Test {
    public static void main(String[] args){
        Rectangle rectangle=new RectangleImpl(1,2);
        rectangle.calculatePerimete();

       
    }
}

 

 

     现在有需求 要加日志,怎么办呢?  这里就用AOP面向切面编程

      来个代理类

package com.mrchen.myredistest.chenstest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理类
 */
public class DynamicProxy implements InvocationHandler{
    //需要代理的目标类
    private Object target;

    /**
     * 固定写法,aop专用,绑定委托对象并且返回一个代理类
     * @param target
     * @return
     */
    public Object bind(Object target) {
        this.target=target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    /**
     *
     * @param proxy
     * @param method 要调用的方法
     * @param args 方法调用时所需要的参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result=null;
        System.out.println("记录日志:运算之前");
        result=method.invoke(target,args);
        System.out.println("记录日志:运算结果"+result);
        return result;
    }
}

 

      程序入口类进行修改

package com.mrchen.myredistest.chenstest;

import com.mrchen.myredistest.chenstest.shape.RectangleImpl;

public class Test {
    public static void main(String[] args){
        Rectangle rectangle=new RectangleImpl(1,2);
        rectangle.calculatePerimete();

        Rectangle rectangle1= (Rectangle) new DynamicProxy().bind(new RectangleImpl(1,2));
        rectangle1.calculatePerimete();
    }
}

 

      运算结果:

记录日志:运算之前
记录日志:运算结果6.0 

  这样就加入了日志系统

你可能感兴趣的:(Java,世界)