如果Java中的反射减慢了命令的执行速度,为什么这么多框架会使用它呢? [英] If reflection in Java slows down execution by orders, why do so many frameworks use it ?

查看:400
本文介绍了如果Java中的反射减慢了命令的执行速度,为什么这么多框架会使用它呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,使用Java反射API会减慢订单执行代码的速度。但后来我发现它被用在Java宇宙中的很多地方。仅举几例:

As per my understanding, use of Java reflection API slows down code execution by orders. But then I see it being used in so many places in Java universe. To name a few :


  • 注释

  • Spring框架(AOP)

  • Hibernate

  • MyBatis

  • Annotations
  • Spring framework (AOP)
  • Hibernate
  • MyBatis

这意味着有一些关于java反射的事实(我称之为优化技术)我错过了。有什么指针吗?

Which implies that there's some fact about java reflection (aka optimization technique) that I have missed out on. Any pointers ?

推荐答案

要点:因为他们别无选择

Java不是一种动态语言,因此这些框架提供服务的唯一方法就是反思。

Java is not a dynamic language, so the only way these frameworks can provide their services is by reflection.

其次,请注意大多数反射工作这些框架在初始化期间只发生一次,因此运行时性能不受影响。

Second, notice that most of the reflection work these framework do happens only once, during initialization, so the runtime performance is not affected.

我注意到有一个区别是一直混淆:

There is one distinction that I notice being mixed up all the time:


  1. 反思查找成员;

  2. 反思成员访问(调用/读/写)。

  1. reflective lookup of members;
  2. reflective member access (invocation/read/write).

数字1很慢(这是你提到的订单);数字2是速度提升显着的速度,现在只比本机访问慢几倍。

Number 1 is slow (this is the "orders" you mention); number 2 is the one that has received significant speed improvements and is now only a couple of times slower than native access.

这篇关于如果Java中的反射减慢了命令的执行速度,为什么这么多框架会使用它呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆