JDK动态代理与CGLib动态代理的区别对比

 更新时间:2019年02月11日 09:52:38   作者:邋遢的流浪剑客  
今天小编就为大家分享一篇关于JDK动态代理与CGLib动态代理的区别对比,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

案例:

public interface ForumService {
 void removeTopic(int topicId);
 void removeForum(int forumId);
}

对相关方法进行性能监控

public class ForumServiceImpl implements ForumService {
 public void removeTopic(int topicId) {
 // PerformanceMonitor.begin("com.hand.proxy.ForumServiceImpl.removeTopic");
 System.out.println("模拟删除Topic记录:" + topicId);
 try {
  Thread.sleep(20);
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
 // PerformanceMonitor.end();
 }
 public void removeForum(int forumId) {
 // PerformanceMonitor.begin("com.hand.proxy.ForumServiceImpl.removeForum");
 System.out.println("模拟删除Forum记录:" + forumId);
 try {
  Thread.sleep(20);
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
 // PerformanceMonitor.end();
 }
}

性能监控实现类:

public class PerformanceMonitor {
 // 通过一个ThreadLocal保存与调用线程相关的性能监视信息
 private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>();
 // 启动对某一目标方法的性能监视
 public static void begin(String method) {
 System.out.println("begin monitor...");
 MethodPerformance mp = new MethodPerformance(method);
 performanceRecord.set(mp);
 }
 public static void end() {
 System.out.println("end monitor...");
 MethodPerformance mp = performanceRecord.get();
 // 打印出方法性能监视的结果信息
 mp.printPerformance();
 }
}

用于记录性能监控信息:

public class PerformanceMonitor {
 // 通过一个ThreadLocal保存与调用线程相关的性能监视信息
 private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>();
 // 启动对某一目标方法的性能监视
 public static void begin(String method) {
 System.out.println("begin monitor...");
 MethodPerformance mp = new MethodPerformance(method);
 performanceRecord.set(mp);
 }
 public static void end() {
 System.out.println("end monitor...");
 MethodPerformance mp = performanceRecord.get();
 // 打印出方法性能监视的结果信息
 mp.printPerformance();
 }
}

1、JDK动态代理

public class PerformanceMonitor {
 // 通过一个ThreadLocal保存与调用线程相关的性能监视信息
 private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>();
 // 启动对某一目标方法的性能监视
 public static void begin(String method) {
 System.out.println("begin monitor...");
 MethodPerformance mp = new MethodPerformance(method);
 performanceRecord.set(mp);
 }
 public static void end() {
 System.out.println("end monitor...");
 MethodPerformance mp = performanceRecord.get();
 // 打印出方法性能监视的结果信息
 mp.printPerformance();
 }
}
public class ForumServiceTest {
 @Test
 public void proxy() {
 ForumService forumService = new ForumServiceImpl();
 PerformanceHandler handler = new PerformanceHandler(forumService);
 ForumService proxy = (ForumService) Proxy.newProxyInstance(forumService.getClass().getClassLoader(),
  forumService.getClass().getInterfaces(), handler);
 proxy.removeForum(10);
 proxy.removeTopic(1012);
 }
}

得到以下输出信息:

begin monitor...
模拟删除Forum记录:10
end monitor...
com.hand.proxy.ForumServiceImpl.removeForum花费21毫秒
begin monitor...
模拟删除Topic记录:1012
end monitor...
com.hand.proxy.ForumServiceImpl.removeTopic花费21毫秒

2、CGLib动态代理

 <!-- https://mvnrepository.com/artifact/cglib/cglib -->
 <dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>2.2.2</version>
 </dependency>
public class CglibProxy implements MethodInterceptor {
 private Enhancer enhancer = new Enhancer();
 public Object getProxy(Class clazz) {
 enhancer.setSuperclass(clazz);
 enhancer.setCallback(this);
 return enhancer.create();
 }
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
 PerformanceMonitor.begin(obj.getClass().getName() + "." + method.getName());
 Object result = proxy.invokeSuper(obj, args);
 PerformanceMonitor.end();
 return result;
 }
}
public class ForumServiceTest2 {
 @Test
 public void proxy() {
 CglibProxy proxy = new CglibProxy();
 ForumServiceImpl forumService = (ForumServiceImpl) proxy.getProxy(ForumServiceImpl.class);
 forumService.removeForum(10);
 forumService.removeTopic(1023);
 }
}

1)、JDK和CGLib的区别

  • JDK动态代理只能对实现了接口的类生成代理,而不能针对类
  • CGLib是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法(继承)

2)、Spring在选择用JDK还是CGLib的依据

  • 当Bean实现接口时,Spring就会用JDK的动态代理
  • 当Bean没有实现接口时,Spring使用CGLib来实现
  • 可以强制使用CGLib(在Spring配置中加入<aop:aspectj-autoproxy proxy-target-class=“true”/>)

3)、JDK和CGLib的性能对比

  • 使用CGLib实现动态代理,CGLib底层采用ASM字节码生成框架,使用字节码技术生成代理类,在JDK1.6之前比使用Java反射效率要高。唯一需要注意的是,CGLib不能对声明为final的方法进行代理,因为CGLib原理是动态生成被代理类的子类。
  • 在JDK1.6、JDK1.7、JDK1.8逐步对JDK动态代理优化之后,在调用次数较少的情况下,JDK代理效率高于CGLib代理效率,只有当进行大量调用的时候,JDK1.6和JDK1.7比CGLib代理效率低一点,但是到JDK1.8的时候,JDK代理效率高于CGLib代理

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • jenkins+Maven从SVN上构建项目的方法

    jenkins+Maven从SVN上构建项目的方法

    这篇文章主要介绍了jenkins+Maven从SVN上构建项目,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Java 用两个线程交替打印数字和字母

    Java 用两个线程交替打印数字和字母

    这篇文章主要介绍了Java 用两个线程交替打印数字和字母的方法,帮助大家更好的理解和学习使用Java,感兴趣的朋友可以了解下
    2021-03-03
  • Java中Integer两种转int方法比较

    Java中Integer两种转int方法比较

    本文主要介绍了Java Integer两种转int方法比较。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • Java发展史之Java由来

    Java发展史之Java由来

    本文主要给大家简单讲解了一下java的发展史,详细说明了java的由来以及如何一步步发展起来的,想了解的小伙伴可以来参考下
    2016-10-10
  • SpringBoot中配置双数据源的实现示例

    SpringBoot中配置双数据源的实现示例

    在许多应用程序中,可能会遇到需要连接多个数据库的情况,本文主要介绍了SpringBoot中配置双数据源的实现示例,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • Java集合List的使用详细解析

    Java集合List的使用详细解析

    这篇文章主要介绍了Java集合List的使用详细解析,List集合类中元素有序、且可重复,集合中的每个元素都有其对应的顺序索引,鉴于Java中数组用来存储数据的局限性,我们通常使用java.util.List替代数组,需要的朋友可以参考下
    2023-11-11
  • Java RSA加密解密实现方法分析【附BASE64 jar包下载】

    Java RSA加密解密实现方法分析【附BASE64 jar包下载】

    这篇文章主要介绍了Java RSA加密解密实现方法,结合实例形式分析了java基于第三方类库javabase64-1.3.1.jar实现RSA加密解密功能的具体步骤与相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • IntelliJ IDEA设置JVM运行参数的操作方法

    IntelliJ IDEA设置JVM运行参数的操作方法

    这篇文章主要介绍了IntelliJ IDEA设置JVM运行参数的操作方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-03-03
  • Java中创建ZIP文件的方法

    Java中创建ZIP文件的方法

    本文通过一段简单代码给大家介绍了java中创建zip文件的方法,代码超简单,感兴趣的朋友跟随脚本之家小编一起看看吧
    2018-06-06
  • 详谈java集合框架

    详谈java集合框架

    这篇文章主要介绍了详谈java集合框架 ,需要的朋友可以参考下
    2015-05-05

最新评论