spring_(15) AOP 相关基础之一(动态代理解决问题)

IOC和AOP是Spring的两大基石。

AOP前奏

在这里插入图片描述

代码实现片段

在这里插入图片描述

问题

  • 代码混乱:越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀。每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点
  • 代码分散:以日志需求为例,只是为了满足这个单一需求,就不得不在多个模块(方法)里多次重复相同的日志代码。如果日志需求发生变化,必须修改所有模块

使用动态代理解决上述问题(开发不建议使用,对程序员要求较高,推荐AOP解决这个问题)

  • 代理设计模式的原理:使用一个代理将对象包装起来,然后用该代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。

在这里插入图片描述

例子程序

基本结构

在这里插入图片描述

ArithmeticCalculator.java

package com.spring.aop.helloworld;
public interface ArithmeticCalculator {
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}

ArithmeticCaluculatorImpl.java

package com.spring.aop.helloworld;

public class  ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public int add(int i, int j) {
        int result = i+j;
        return result;
    }

    @Override
    public int sub(int i, int j) {
        int result = i-j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        int result = i*j;
        return result;
    }

    @Override
    public int div(int i, int j) {
        int result = i/j;
        return result;
    }
}

ArithmeticCalculatorLoggingImpl.java

package com.spring.aop.helloworld;

public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator {

    @Override
    public int add(int i, int j) {
        System.out.println("The method add begins with["+i+","+j+"]");
        int result = i+j;
        System.out.println("The method add ends with"+result);
        return result;
    }

    @Override
    public int sub(int i, int j) {
        System.out.println("The method sub begins with["+i+","+j+"]");
        int result = i-j;
        System.out.println("The method sub ends with"+result);
        return result;
    }

    @Override
    public int mul(int i, int j) {
        System.out.println("The method mul begins with["+i+","+j+"]");
        int result = i*j;
        System.out.println("The method mul ends with"+result);
        return result;
    }

    @Override
    public int div(int i, int j) {
        System.out.println("The method div begins with["+i+","+j+"]");
        int result = i/j;
        System.out.println("The method div ends with"+result);
        return result;
    }
}

ArithmeticCalculatorLoggingProxy.java

package com.spring.aop.helloworld;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

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

public class ArithmeticCalculatorLoggingProxy {

    //要代理的对象
    private ArithmeticCalculator target;

    public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target){
        this.target = target;
    }

    public ArithmeticCalculator getLoggingProxy(){
            ArithmeticCalculator proxy = null;

            //代理对象由哪一个类加载器负责加载
            ClassLoader loader = target.getClass().getClassLoader();
            //代理对象的类型,即其中有哪些方法
            Class [] interfaces = new Class[]{ArithmeticCalculator.class};
            //当调用代理对象其中的方法时,该执行的代码
            InvocationHandler h = new InvocationHandler() {
                /**
                 * proxy:正在返回的那个代理对象,一般情况下,在invoke方法中都不使用该对象。
                 * method:正在被调用的方法
                 * args:调用方法时,传入的参数
                 * @param proxy
                 * @param method
                 * @param args
                 * @return
                 * @throws Throwable
                 */
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    //这里如果这样写会造成死循环System.out.println(proxy.toString());
                    String methodName = method.getName();
                    //日志
                    System.out.println("ATGUIGU->The method "+methodName+" begins with "+ Arrays.asList(args));
                    //执行方法
                    Object result = method.invoke(target,args);
                    //日志
                    System.out.println("ATGUIGU->The method "+methodName+" ends with "+result);
                    return  result ;
                }
            };
            proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader,interfaces,h);

            return proxy;
    }
}

Main.java

package com.spring.aop.helloworld;

public class Main {

    public static void main(String[] args){

//        ArithmeticCalculator arithmeticCalculator = null;
//        arithmeticCalculator = new ArithmeticCalculatorLoggingImpl();

        ArithmeticCalculator target = new ArithmeticCalculatorImpl();
        ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();


        int result = proxy.add(1,2);
        System.out.println("-->"+result);

        result = proxy .div(4,2);
        System.out.println("-->"+result);
        
    }

}

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42036647/article/details/84344559
今日推荐