Spring4---AOP简介

什么是AOP?

在这里插入图片描述

举个栗子┗|-_-|┛

  • 现在定义一个简单计算功能程序,假设计算的数为两个数"x",“y”,功能要求
    1. 实现两个数的加减乘除的基本运算
    2. 每次进行计算时都要想控制台打印日志信息,格式为"调用的方法----(x = x的值,y = y的值) "
    3. 要求每次计算时需要验证操作的x或y是否为正数,如果是负数,则不进行计算
  1. 定义一个CalcUtil接口,该接口使用泛型描述"x"和"y",使用泛型"R"描述返回结果
package mao.shu.spring.aop;

public interface CalcUtil<X,Y,R> {
    public R plus(X x,Y y);
    public R subtract(X x,Y y);
    public R multiplication(X x,Y y);
    public R division(X x,Y y);
}

  • 定义一个IntCalc类实现CalcUtil接口
package mao.shu.spring.aop;

public class IntCalc implements CalcUtil<Integer,Integer,Integer> {
    @Override
    public Integer plus(Integer x, Integer y) throws Exception {
        if(x < 0 || y < 0){
            throw new Exception("请输入正数");
        }
        System.out.println("执行plus()方法 (x = "+x+" y = "+y+")");
        return x +y;
    }

    @Override
    public Integer subtract(Integer x, Integer y) throws Exception {
        if(x < 0 || y < 0){
            throw new Exception("请输入正数");
        }
        System.out.println("执行subtract()方法 (x = "+x+" y = "+y+")");
        return x - y;
    }

    @Override
    public Integer multiplication(Integer x, Integer y) throws Exception {
        if(x < 0 || y < 0){
            throw new Exception("请输入正数");
        }
        System.out.println("执行multiplication()方法 (x = "+x+" y = "+y+")");
        return x * y;
    }

    @Override
    public Integer division(Integer x, Integer y) throws Exception {
        if(x < 0 || y < 0){
            throw new Exception("请输入正数");
        }
        System.out.println("执行division()方法 (x = "+x+" y = "+y+")");
        return x / y;
    }
}

  • 测试
package mao.shu.spring.aop;

import org.junit.Test;

import static org.junit.Assert.*;

public class IntCalcTest {
    private IntCalc intCalc = new IntCalc();


    @Test
    public void plus() throws Exception {
        System.out.println(intCalc.plus(10,20));
    }

    @Test
    public void subtract() throws Exception {
        System.out.println(intCalc.subtract(45,89));
    }

    @Test
    public void multiplication() throws Exception {
        System.out.println(intCalc.multiplication(45,78));
    }

    @Test
    public void division() throws Exception {
        System.out.println(intCalc.division(78,89));
    }
}
  • 得到的结果

在这里插入图片描述

发现问题

  • 虽然以上的代码实现了基础的功能,但是可以发现日志的处理和验证操作,在每一个方法中否需要进行,而且代码相似.
  • 如果以后日志功能或者验证功能需要发生变化,则需要修改每一个方法,维护不方便.
  • 如果能将日志功能和验证功能单独抽取出来就好了.

使用动态代理解决问题

在这里插入图片描述

  1. 创建CalcProxy代理类,将数据验证和打印日志的任务交由代理类完成
package mao.shu.spring.aop.calc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

public class CalcProxy implements InvocationHandler {
    private Object target;//代理的对象

    public CalcProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        validate(args);
        System.out.println(method.getName()+" 开始执行 ( "+ Arrays.toString(args) +" )");
        method.invoke(target,args);//执行方法
        System.out.println(method.getName()+" 执行结束 ( "+ Arrays.toString(args) +" )");
        return null;
    }

    /**
     * 负责进行参数验证的方法
     * @param args
     * @return
     */
    private boolean validate(Object[] args){
        for (int i = 0; i < args.length; i++) {
            int temp = (int)args[i];
            if(temp < 0){
                try {
                    throw new Exception("请输入正确的正数");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            }
        }
        return true;
    }
}

  • 重新编写IntCalc类,去掉日志打印和验证操作
@Override
public Integer plus(Integer x, Integer y) throws Exception {
    return x + y;
}
@Override
public Integer subtract(Integer x, Integer y) throws Exception {
    return x - y;
}
@Override
public Integer multiplication(Integer x, Integer y) throws Exception {
    return x * y;
}
@Override
public Integer division(Integer x, Integer y) throws Exception {
    return x / y;
}
  • 编写测试类
package mao.shu.spring.aop;

import mao.shu.spring.aop.calc.CalcProxy;
import mao.shu.spring.aop.calc.CalcUtil;
import mao.shu.spring.aop.calc.IntCalc;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Proxy;

public class IntCalcTest {
    private CalcUtil intCalc;
    private CalcProxy calcProxy;

    @Before
    public void before(){
        intCalc = new IntCalc();
        intCalc = (CalcUtil) Proxy.newProxyInstance(
                intCalc.getClass().getClassLoader(),
                intCalc.getClass().getInterfaces(),
                new CalcProxy(intCalc));
    }
    @Test
    public void plus() throws Exception {
        System.out.println(intCalc.plus(10, 20));
    }

    @Test
    public void subtract() throws Exception {
        System.out.println(intCalc.subtract(45, 89));
    }

    @Test
    public void multiplication() throws Exception {
        System.out.println(intCalc.multiplication(45, 78));
    }

    @Test
    public void division() throws Exception {
        System.out.println(intCalc.division(78, 89));
    }
}
  • 执行结果

在这里插入图片描述

以AOP理解代理类

在这里插入图片描述

AOP 术语

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值