0%

java动态代理

JDK代理模式

代理模式

代理模式是一种通过代理对象来代替真实对象,而可以在不修改原有对象的前提下,提供额外的功能操作,扩展目标对象的功能

静态代理

静态代理中,对目标对象的每个方法的增强都是手动完成的,在接口新增一些方法,目标对象和代理对象都需要进行修改(使用场景非常少)

动态代理

相比较于静态代理来说,动态代理更加灵活。我们不需要针对每个目标类都单独创建一个代理类,我们可以直接使用代理对象实现类(CGLB动态动态代理机制)

动态代理实现

JDK动态代理

在 Java 动态代理机制中 InvocationHandler 接口和 Proxy 类是核心。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* loader 类加载器
* interfaces 被代理类实现的一些接口
* h 实现了InvocationHandler的对象
*/
//newProxyInstance(),这个方法主要用来生成一个代理对象
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
......
}

1
2
3
4
5
6
7
8
9
10
11
12
/**
* proxy :动态生成的代理类
* method : 与代理类对象调用的方法相对应
* args : 当前 method 方法的参数
*/
public interface InvocationHandler {
/**
* 当你使用代理对象调用方法的时候实际会调用到这个方法
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}

3.1.2. JDK 动态代理类使用步骤

  1. 定义一个接口及其实现类;
  2. 自定义 InvocationHandler 并重写invoke方法,在 invoke 方法中我们会调用原生方法(被代理类的方法)并自定义一些处理逻辑;
  3. 通过 Proxy.newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h) 方法创建代理对象;

java中InvocationHandler接口中第一个参数proxy详解

CGLB动态代理

JDK动态代理只能代理实现了接口的类

CGLIB(Code Generation Library)是一个基于ASM的字节码生成库,它允许我们在运行时对字节码进行修改和动态生成。CGLIB 通过继承方式实现代理。很多知名的开源框架都使用到了CGLIB, 例如 Spring 中的 AOP 模块中:如果目标对象实现了接口,则默认采用 JDK 动态代理,否则采用 CGLIB 动态代理。

在 CGLIB 动态代理机制中 MethodInterceptor 接口和 Enhancer 类是核心。

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
* obj: 被代理的对象(需要增强的方法)
* method: 被拦截的方法(需要增强的方法)
* args: 方法入参
* proxy: 用于调用原始方法
*/
public interface MethodInterceptor
extends Callback{
// 拦截被代理类中的方法
public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args,
MethodProxy proxy) throws Throwable;
}

CGLIB 动态代理类使用步骤

​ 定义一个类;

​ 自定义 MethodInterceptor 并重写 intercept 方法,intercept 用于拦截增强被代理类的方法,和 JDK 动态代理中的 invoke 方法类似;

​ 通过 Enhancer 类的 create()创建代理类;

代码示例

不同于 JDK 动态代理不需要额外的依赖

1
2
3
4
5
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
  1. 实现一个使用阿里云发送短信的类

    1
    2
    3
    4
    5
    6
    7
    8
    package github.javaguide.dynamicProxy.cglibDynamicProxy;

    public class AliSmsService {
    public String send(String message) {
    System.out.println("send message:" + message);
    return message;
    }
    }
  2. 自定义 MethodInterceptor(方法拦截器)

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
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
* 自定义MethodInterceptor
*/
public class DebugMethodInterceptor implements MethodInterceptor {


/**
* @param o 代理对象(增强的对象)
* @param method 被拦截的方法(需要增强的方法)
* @param args 方法入参
* @param methodProxy 用于调用原始方法
*/
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
//调用方法之前,我们可以添加自己的操作
System.out.println("before method " + method.getName());
Object object = methodProxy.invokeSuper(o, args);
//调用方法之后,我们同样可以添加自己的操作
System.out.println("after method " + method.getName());
return object;
}

}
  1. 获取代理类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import net.sf.cglib.proxy.Enhancer;

public class CglibProxyFactory {

public static Object getProxy(Class<?> clazz) {
// 创建动态代理增强类
Enhancer enhancer = new Enhancer();
// 设置类加载器
enhancer.setClassLoader(clazz.getClassLoader());
// 设置被代理类
enhancer.setSuperclass(clazz);
// 设置方法拦截器
enhancer.setCallback(new DebugMethodInterceptor());
// 创建代理类
return enhancer.create();
}
}
  1. 实际使用
1
2
AliSmsService aliSmsService = (AliSmsService) CglibProxyFactory.getProxy(AliSmsService.class);
aliSmsService.send("java");

JDK 动态代理和 CGLIB 动态代理对比

  1. JDK 动态代理只能代理实现了接口的类或者直接代理接口,而 CGLIB 可以代理未实现任何接口的类。 另外, CGLIB 动态代理是通过生成一个被代理类的子类来拦截被代理类的方法调用,因此不能代理声明为 final 类型的类和方法。
  2. 就二者的效率来说,大部分情况都是 JDK 动态代理更优秀,随着 JDK 版本的升级,这个优势更加明显。