美文网首页
动态代理

动态代理

作者: 万物皆有序和公式 | 来源:发表于2019-05-31 16:57 被阅读0次

1 jdk代理

package com.example.demo.agent;

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


/**
 * @program: demo
 * @description:
 * @author: liuwei
 * @create: 2019-05-31 14:11
 **/
public class LogInvocationHandler implements InvocationHandler {
    public static final String SAY_HI = "sayHi";
    private Hello hello;

    public LogInvocationHandler(Hello hello) {
        this.hello = hello;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName()+"---------");
        Object invokeValue = null;
        if (SAY_HI.equals(method.getName())) {
            System.out.println("==========");
            invokeValue = method.invoke(hello, args);
            System.out.println("*********");
        }
        return invokeValue;
    }

    public static void main(String[] args) {
        Hello hello = (Hello) Proxy.newProxyInstance(LogInvocationHandler.class.getClassLoader(), new Class[]{Hello.class}
                , new LogInvocationHandler(new HelloImpl()));
        hello.sayHi("hi");
        hello.sayBye("bye");
    }
}

2 cjlib代理

package com.example.demo.agent;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @program: demo
 * @description:
 * @author: liuwei
 * @create: 2019-05-31 16:10
 **/
public class LogMethodInterceptor implements MethodInterceptor {

    public static final String SAY_HI = "sayHi";

    @Override
    public Object intercept(Object o,
                            Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object o1 = null;
        if (method.getName().equals(SAY_HI)) {
            System.out.println("------------");
            o1 = methodProxy.invokeSuper(o, objects);
            System.out.println("============");
        }

        return o1;
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(HelloImpl.class);
        enhancer.setCallback(new LogMethodInterceptor());
        HelloImpl hello = (HelloImpl) enhancer.create();
        hello.sayBye("bye");
        hello.sayHi("hi");
    }
}

pom

<dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2</version>
</dependency>

相关文章

  • 面试系列~动态代理实现与原理

    动态代理有JDK动态代理, CGLIB动态代理, SpringAOP动态代理 一,JDK动态代理  jdk动态代理...

  • 编程常用的设计模式

    动态代理和静态代理 静态代理 动态代理 静态代理与动态代理的区别 JDK中的动态代理和CGLIB 实现动态代理的方...

  • Spring的AOP原理分析

    一 动态代理 动态代理分为JDK动态代理和CGLIB动态代理 jdk动态代理 被代理类(目标类)和代理类必须实现同...

  • 设计模式之代理模式

    代理分为静态代理和动态代理。 动态代理又包括基于JDK的动态代理、基于CGlib 的动态代理、基于Aspectj实...

  • Java高级主题(五)——动态代理

    代理可以分为静态代理、动态代理,动态代理又可以分为 jvm的动态代理 和 cglib的动态代理。像spring框架...

  • 动态代理

    动态代理分为两类:1、基于接口的动态代理; (JDK动态代理 )2、基于类的动态代理;(cglib动态代理)3、J...

  • 动态代理的两种方式

    静态代理就不说了,基本用到的都是动态代理。 Java中动态代理有JDK动态代理和CGLIB动态代理。 JDK代理的...

  • Java动态代理

    通过以下几种方式介绍动态代理 动态代理涉及到的类 动态代理用法 Proxy类解析 动态代理类解析 动态代理涉及到的...

  • Spring之代理模式

    九、代理模式 目录:静态代理、动态代理AOP的底层机制就是动态代理。代理模式分为静态代理和动态代理。接触aop之前...

  • Java 代理

    静态代理 动态代理 动态代理, 日志切片使用反射获得方法 动态代理, 自定义注解(对注解的方法,使用动态代理添加切...

网友评论

      本文标题:动态代理

      本文链接:https://www.haomeiwen.com/subject/ozegtctx.html