Spring如何基于Proxy及cglib实现动态代理

所属分类: 软件编程 / java 阅读数: 75
收藏 0 赞 0 分享

spring中提供了两种动态代理的方式,分别是Java Proxy以及cglib

JavaProxy只能代理接口,而cglib是通过继承的方式,实现对类的代理

添加一个接口以及对应的实现类

public interface HelloInterface {
  void sayHello();
}
public class HelloInterfaceImpl implements HelloInterface {
  @Override
  public void sayHello() {
    System.out.println("hello");
  }
}

JavaProxy通过实现InvocationHandler实现代理

public class CustomInvocationHandler implements InvocationHandler {
  private HelloInterface helloInterface;

  public CustomInvocationHandler(HelloInterface helloInterface) {
    this.helloInterface = helloInterface;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("before hello for proxy");
    Object result = method.invoke(helloInterface, args);
    System.out.println("after hello for proxy");
    return result;
  }
}

而cglib实现MethodInterceptor进行方法上的代理

public class CustomMethodInterceptor implements MethodInterceptor {
  @Override
  public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    System.out.println("before hello for cglib");
    Object result = methodProxy.invokeSuper(o, objects);
    System.out.println("after hello for cglib");
    return result;
  }

}

分别实现调用代码

public static void main(String[] args) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(HelloInterfaceImpl.class);
    enhancer.setCallback(new CustomMethodInterceptor());
    HelloInterface target = (HelloInterface) enhancer.create();
    target.sayHello();

    CustomInvocationHandler invocationHandler = new CustomInvocationHandler(new HelloInterfaceImpl());
    HelloInterface target2 = (HelloInterface) Proxy.newProxyInstance(Demo.class.getClassLoader(), new Class[]{HelloInterface.class}, invocationHandler);
    target2.sayHello();
  }

可以看到对于的代理信息输出

before hello for cglib
hello
after hello for cglib
before hello for proxy
hello
after hello for proxy

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多