浅析Java 反射机制的用途和缺点

 更新时间:2018年07月22日 15:16:35   作者:Alfredonwr  
这篇文章给大家分析了Java 反射机制的用途和缺点以及相关知识点内容,有兴趣的朋友可以参考学习下。

反射的用途 Uses of Reflection

Reflection is commonly used by programs which require the ability to examine or modify the
runtime behavior of applications running in the Java virtual machine. This is a relatively advanced
feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
反射被广泛地用于那些需要在运行时检测或修改程序行为的程序中。这是一个相对高级
的特性,只有那些语言基础非常扎实的开发者才应该使用它。如果能把这句警示时刻放在心
里,那么反射机制就会成为一项强大的技术,可以让应用程序做一些几乎不可能做到的事情。

反射的缺点 Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an
operation without using reflection, then it is preferable to avoid using it. The following concerns
should be kept in mind when accessing code via reflection.
尽管反射非常强大,但也不能滥用。如果一个功能可以不用反射完成,那么最好就不用。
在我们使用反射技术时,下面几条内容应该牢记于心:

性能第一 Performance Overhead

Because reflection involves types that are dynamically resolved, certain Java virtual machine
optimizations can not be performed. Consequently, reflective operations have slower performance
than their non-reflective counterparts, and should be avoided in sections of code which are called
frequently in performance-sensitive applications.
反射包括了一些动态类型,所以 JVM 无法对这些代码进行优化。因此,反射操作的效
率要比那些非反射操作低得多。我们应该避免在经常被 执行的代码或对性能要求很高的程
序中使用反射。

安全限制 Security Restrictions

Reflection requires a runtime permission which may not be present when running under a
security manager. This is in an important consideration for code which has to run in a restricted
security context, such as in an Applet.
使用反射技术要求程序必须在一个没有安全限制的环境中运行。如果一个程序必须在有
安全限制的环境中运行,如 Applet,那么这就是个问题了。

内部暴露 Exposure of Internals

Since reflection allows code to perform operations that would be illegal in non-reflective
code, such as accessing private fields and methods, the use of reflection can result in
unexpected side-effects, which may render code dysfunctional and may destroy portability.
Reflective code breaks abstractions and therefore may change behavior with upgrades of the
platform.
由于反射允许代码执行一些在正常情况下不被允许的操作(比如访问私有的属性和方
法),所以使用反射可能会导致意料之外的副作用--代码有功能上的错误,降低可移植性。
反射代码破坏了抽象性,因此当平台发生改变的时候,代码的行为就有可能也随着变化。

相关文章

  • SpringBoot集成MaxCompute的示例代码

    SpringBoot集成MaxCompute的示例代码

    这篇文章主要介绍了SpringBoot集成MaxCompute的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • SpringCloud微服务基础简介

    SpringCloud微服务基础简介

    今天带大家学习一下SpringCloud微服务的相关知识,文中有非常详细的图文示例及介绍,对正在学习SpringCloud微服务的小伙伴们很有帮助哦,需要的朋友可以参考下
    2021-05-05
  • SpringCloud微服务多应用脚手架的搭建与部署方式

    SpringCloud微服务多应用脚手架的搭建与部署方式

    这篇文章主要介绍了SpringCloud微服务多应用脚手架的搭建与部署方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • SpringBoot上传图片到指定位置并返回URL的实现

    SpringBoot上传图片到指定位置并返回URL的实现

    本文主要介绍了SpringBoot上传图片到指定位置并返回URL,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下<BR>
    2022-03-03
  • 使用SpringBoot中整合Redis

    使用SpringBoot中整合Redis

    这篇文章主要介绍了使用SpringBoot中整合Redis,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • SpringBoot依赖注入的三种方式

    SpringBoot依赖注入的三种方式

    本文将通过代码示例详细介绍SpringBoot依赖注入的三种方式,对学习依赖注入有一定的参考价值,需要的朋友可以参考一下
    2023-04-04
  • MyBatis-Plus结合Layui实现分页方法

    MyBatis-Plus结合Layui实现分页方法

    MyBatis-Plus 使用简单,本文主要介绍使用 service 中的 page 方法结合 Layui 前端框架实现分页效果,具有一定的参考价值,感兴趣的可以了解一下
    2021-08-08
  • 详解RabbitMQ中延迟队列结合业务场景的使用

    详解RabbitMQ中延迟队列结合业务场景的使用

    这篇文章主要介绍了详解RabbitMQ中延迟队列结合业务场景的使用,延迟队列中的元素都是带有时间属性的,延迟队列就是用来存放需要在指定时间被处理的元素的队列,需要的朋友可以参考下
    2023-05-05
  • Mybatis-plus foreach拼接字符串查询无数据返回问题

    Mybatis-plus foreach拼接字符串查询无数据返回问题

    这篇文章主要介绍了Mybatis-plus foreach拼接字符串查询无数据返回问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java IO文件编码转换实现代码

    Java IO文件编码转换实现代码

    这篇文章主要介绍了Java IO文件编码转换实现代码,有需要的朋友可以参考一下
    2013-12-12

最新评论