• Spring-动态代理深入了解


    😀前言
    本篇的Spring-AOP系类文章第二篇扩展了Spring-动态代理然后开发了简易的AOP类

    🏠个人主页尘觉主页
    在这里插入图片描述

    🧑个人简介:大家好,我是尘觉,希望我的文章可以帮助到大家,您的满意是我的动力😉😉

    在csdn获奖荣誉: 🏆csdn城市之星2名
    ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ 💓Java全栈群星计划top前5
    ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ 🤗 端午大礼包获得者

    💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看🥰
    如果文章有什么需要改进的地方还请大佬不吝赐教 先在次感谢啦😊

    🥰Spring-动态代理深入了解

    😁代码演示–动态代理深入

    需求说明

    1. 有一个 SmartAnimal 接口,可以完成简单的加减法, 要求在执行 getSum()和 getSub() 时,输出执行前,执行过程,执行后的日志输出

    💖输出结果

    日志-方法名-getSum-参数 10.0 2.0
    方法内部打印 result = 12.0
    日志-方法名-getSum-结果 result= 12.0


    日志-方法名-getSub-参数 10.0 2.0
    方法内部打印 result = 8.0
    日志-方法名-getSub-结果 result= 8

    创建步骤

    1. 创建接口 SmartAnimalable.java
    public interface SmartAnimalable {
        //求和
        float getSum(float i, float j);
        //求差
        float getSub(float i, float j);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 实现类SmartDog
    public class SmartDog implements SmartAnimalable {
        @Override
        public float getSum(float i, float j) {
            //System.out.println("日志-方法名-getSum-参数 " + i + " " + j);
            float result = i + j;
            System.out.println("方法内部打印result = " + result);
            //System.out.println("日志-方法名-getSum-结果result= " + result);
            return result;
        }
    
        @Override
        public float getSub(float i, float j) {
            //System.out.println("日志-方法名-getSub-参数 " + i + " " + j);
            float result = i - j;
            System.out.println("方法内部打印result = " + result);
            //System.out.println("日志-方法名-getSub-结果result= " + result);
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. 创建MyProxyProvider类

    注意这里就没有使用上面直接new一个对象来调用了而是直接使用反射

    这里也考虑了出现异常的问题

    执行思路

    定义我们要执行的目标对象, 该对象需要实现SmartAnimalable

    构造器

    方法, 可以返回代理对象,该代理对象可以执行目标对象

    • 先到的类加载器/对象

    • 得到要执行的目标对象的接口信息

    • 创建InvocationHandler

    最后 创建代理对象并返回

    public class MyProxyProvider {
    
        //定义我们要执行的目标对象, 该对象需要实现SmartAnimalable
        private SmartAnimalable target_obj;
    
        //构造器
        public MyProxyProvider(SmartAnimalable target_obj) {
            this.target_obj = target_obj;
        }
    
        //方法, 可以返回代理对象,该代理对象可以执行目标对象
        public SmartAnimalable getProxy() {
    
            //1. 先到的类加载器/对象
            ClassLoader classLoader = target_obj.getClass().getClassLoader();
    
            //2. 得到要执行的目标对象的接口信息
            Class<?>[] interfaces = target_obj.getClass().getInterfaces();
    
            //3. 创建InvocationHandler
            InvocationHandler invocationHandler = new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    Object result = null;
                    try {
                        System.out.println("方法执行前-日志-方法名-" + method.getName() + "-参数 "
                                + Arrays.asList(args)); //这里从AOP看,就是一个横切关注点-前置通知
                        //使用反射调用方法
                        result = method.invoke(target_obj, args);
                        System.out.println("方法执行正常结束-日志-方法名-" + method.getName() + "-结果result= "
                                + result);//从AOP看, 也是一个横切关注点-返回通知
    
                    } catch (Exception e) {
                        e.printStackTrace();
                        //如果反射执行方法时,出现异常,就会进入到catch{}
                        System.out.println("方法执行异常-日志-方法名-" + method.getName()
                                + "-异常类型=" + e.getClass().getName());//从AOP看, 也是一个横切关注点-异常通知
                    } finally {//不管你是否出现异常,最终都会执行到finally{}
                        //从AOP的角度看, 也是一个横切关注点-最终通知
                        System.out.println("方法最终结束-日志-方法名-" + method.getName());
                    }
    
                    return result;
                }
            };
    
            //创建代理对象
            SmartAnimalable proxy =
                    (SmartAnimalable)Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
            return proxy;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    创建测试类
    @Test
        public void smartDogTestByProxy() {
    
            SmartAnimalable smartAnimalable = new SmartDog();
    
            MyProxyProvider myProxyProvider =
                    new MyProxyProvider(smartAnimalable);
    
            //我们返回了代理对象
            SmartAnimalable proxy =
                    myProxyProvider.getProxy();
    
            proxy.getSum(10, 2);
            System.out.println("====================");
            proxy.getSub(10, 2);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    💞 ● 问题提出

    1. 在 MyProxyProvider.java 中, 我们的输出语句功能比较弱,在实际开发中,我们希望是 以一个方法的形式,嵌入到真正执行的目标方法前

    使用土方法解决前面的问题

    把输出语句抽出来在上面形成一个方法

    创建步骤

    1. 创建MyProxyProvider
    //我们一个方法,在目标对象执行前执行
        public void before(Object proxy, Method method, Object[] args) {
           System.out.println("before-方法执行前-日志-方法名-" + method.getName() + "-参数 "
                   + Arrays.asList(args)); //这里从AOP看,就是一个横切关注点-前置通知
        }
    
        //我们一个方法,在目标对象执行后执行
        public void after(Method method, Object result) {
           System.out.println("after-方法执行正常结束-日志-方法名-" + method.getName() + "-结果result= "
                   + result);//从AOP看, 也是一个横切关注点-返回通知
        }
    public SmartAnimalable getProxy() {
    
            //1. 先到的类加载器/对象
            ClassLoader classLoader = target_obj.getClass().getClassLoader();
    
            //2. 得到要执行的目标对象的接口信息
            Class<?>[] interfaces = target_obj.getClass().getInterfaces();
    
            //3. 创建InvocationHandler
            InvocationHandler invocationHandler = new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    Object result = null;
                    try {
    
                        before(proxy, method, args);
    
    
                        //使用反射调用方法
                        result = method.invoke(target_obj, args);
    
                        after(method, result);
                    } catch (Exception e) {
                        e.printStackTrace();
                        //如果反射执行方法时,出现异常,就会进入到catch{}
                        System.out.println("方法执行异常-日志-方法名-" + method.getName()
                                + "-异常类型=" + e.getClass().getName());//从AOP看, 也是一个横切关注点-异常通知
                    } finally {//不管你是否出现异常,最终都会执行到finally{}
                        //从AOP的角度看, 也是一个横切关注点-最终通知
                        System.out.println("方法最终结束-日志-方法名-" + method.getName());
                    }
    
                    return result;
                }
            };
    
            //创建代理对象
            SmartAnimalable proxy =
                    (SmartAnimalable) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
            return proxy;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    该方法问题分析:

    耦合度高

    ❤️‍🔥对土方法解耦-开发简易的 AOP 类

    创建步骤

    1. 创建WyxAOP.java
    /**
     * 自己编写一个一个极简的AOP类
     */
    public class WyxAOP {
    
        //我们一个方法,在目标对象执行前执行
        public static void before(Object proxy, Method method, Object[] args) {
            System.out.println("WyxAOP-方法执行前-日志-方法名-" + method.getName() + "-参数 "
                    + Arrays.asList(args)); //这里从AOP看,就是一个横切关注点-前置通知
        }
    
        //我们一个方法,在目标对象执行后执行
        public static void after(Method method, Object result) {
            System.out.println("WyxAOP-方法执行正常结束-日志-方法名-" + method.getName() + "-结果result= "
                    + result);//从AOP看, 也是一个横切关注点-返回通知
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 修改MyProxyProvider
     public SmartAnimalable getProxy() {
    
            //1. 先到的类加载器/对象
            ClassLoader classLoader = target_obj.getClass().getClassLoader();
    
            //2. 得到要执行的目标对象的接口信息
            Class<?>[] interfaces = target_obj.getClass().getInterfaces();
    
            //3. 创建InvocationHandler
            InvocationHandler invocationHandler = new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    Object result = null;
                    try {
    
                        WyxAOP.before(proxy, method, args);
    
                        //使用反射调用方法
                        result = method.invoke(target_obj, args);
    
                       WyxAOP.after(method, result);
    
                    } catch (Exception e) {
                        e.printStackTrace();
                        //如果反射执行方法时,出现异常,就会进入到catch{}
                        System.out.println("方法执行异常-日志-方法名-" + method.getName()
                                + "-异常类型=" + e.getClass().getName());//从AOP看, 也是一个横切关注点-异常通知
                    } finally {//不管你是否出现异常,最终都会执行到finally{}
                        //从AOP的角度看, 也是一个横切关注点-最终通知
                        System.out.println("方法最终结束-日志-方法名-" + method.getName());
                    }
    
                    return result;
                }
            };
    
            //创建代理对象
            SmartAnimalable proxy =
                    (SmartAnimalable) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
            return proxy;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    完成测试
        @Test
        public void smartDogTestByProxy() {
    
            SmartAnimalable smartAnimalable = new SmartDog();
    
            MyProxyProvider myProxyProvider =
                    new MyProxyProvider(smartAnimalable);
    
            //我们返回了代理对象
            SmartAnimalable proxy =
                    myProxyProvider.getProxy();
    
            proxy.getSum(10, 2);
            System.out.println("====================");
            proxy.getSub(10, 2);
    
        } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    😄总结

    本篇总结了Spring-动态代理然后开发了简易的AOP类使更容易理解

    😍Spring-AOP系类文章
    第一篇-> Spring-AOP的基本介绍以及通过先动态代理方式实现

    😁热门专栏推荐
    想学习vue的可以看看这个
    java基础合集
    数据库合集
    redis合集
    nginx合集
    linux合集
    等等等还有许多优秀的合集在主页等着大家的光顾感谢大家的支持

    🤔欢迎大家加入我的社区 尘觉社区

    文章到这里就结束了,如果有什么疑问的地方请指出,诸佬们一起来评论区一起讨论😁
    希望能和诸佬们一起努力,今后我们一起观看感谢您的阅读🍻
    如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力🤞

  • 相关阅读:
    Hadoop 3.x(入门)----【Hadoop概述】
    基于rest_framework的ModelViewSet类编写登录视图和认证视图
    Vue3+Vite3+Vant初体验及踩过的一些坑
    MySQL数据优化
    JS 数组的操作
    【机器学习】逻辑回归
    【SI好文翻译】铜箔表面纹理对损耗的影响:一个有效的模型(四)
    Fisher信息与最大似然估计的渐进正态性(附有在Bernoulli分布上的计算)
    mac下mysql 常用命令
    软考-软件开发模型
  • 原文地址:https://blog.csdn.net/apple_67445472/article/details/132123384