• JavaSE之动态代理


    动态代理

    动态代理需要确定要代理的对象,所以需要先new一个要代理的对象

    动态代理的好处

    可以在不改变方法源码的情况下,实现对方法功能的增强。
    简化了代码。

    提高了软件系统的可扩展性。

    Proxy

    新建一个代理类:

    Proxy.newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h)

    三个参数分别为:

    ClassLoader loader(类加载器): 一般通过当前类反射取得类加载器

    Class[] interfaces : 要代理的类实现的接口

    InvocationHandler h: 用匿名内部类实现,重写invoke()方法

    重写invoke方法

    invoke的三个参数

    public Object invoke(Object proxy, Method method, Object[] args)

    Object proxy :要代理的对象也就是开头说要new出来的对象

    Method method : 代理类需要代理的方法

    Object[] args : 方法的参数

    首先,创建代理对象。

    在创建时如果指定了Handler,那么先执行invoke,

    同时将所在对象的方法和参数传进来。传进来后继续执行方法体,方法体就写你要增强的内容

    实例

    1.先定义一个接口

    public interface Student {
        void eat();
    
        void sleep();
    
        void study();
    
        void play();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.创建接口实现类

    public class StudentImpl implements Student{
    
        @Override
        public void eat() {
            System.out.println("吃饭");
        }
    
        @Override
        public void sleep() {
            System.out.println("睡觉");
        }
    
        @Override
        public void study() {
            System.out.println("学习");
        }
    
        @Override
        public void play() {
            System.out.println("玩游戏");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.进行动态代理

    public static void main(String[] args) {
    
            //对StudentImpl实现动态代理
            //新建一个要代理的对象
            StudentImpl student = new StudentImpl();
            ClassLoader classLoader = Demo041.class.getClassLoader();
            Class[] classes = {Student.class};//数组用来接收多个Class对象,单一职责原则,一般只用一个,用哪个调用哪个
    
    		//多态。将Object类强转为Student,才能拿到Student中特有的方法
            Student proxy = (Student) Proxy.newProxyInstance(classLoader, classes, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("开始前先祈祷");
                    Object result = method.invoke(student, args);
                    System.out.println("结束后祈祷");
                    System.out.println("-------------------");
                    return result;
                }
            });
    
            proxy.eat();
            proxy.play();
            proxy.sleep();
            proxy.study();
        }
    
    • 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

    结果:

    在这里插入图片描述

    最后

    如果你对本文有疑问,你可以在文章下方对我留言,敬请指正,对于每个留言我都会认真查看。

  • 相关阅读:
    PCB阻抗计算
    安科瑞为工业能效行动计划提供EMS解决方案-Susie 周
    RabbitMQ快速入门
    电子制造企业部署WMS仓储管理系统的好处是什么
    WPF-封装自定义雷达图控件
    金仓数据库 KingbaseES SQL 语言参考手册 (21. KES正则表达式支持)
    JUC第十四讲:JUC锁: ReentrantReadWriteLock详解
    学了labview怎么系统的编一个程序?
    蓝桥杯备赛第五篇(动态规划)
    微信网页版能够使用(会顶掉微信app的登陆)
  • 原文地址:https://blog.csdn.net/weixin_47543906/article/details/127926295