使用Java Reflection初始化成员变量 [英] Using Java Reflection to initialize member variables

查看:51
本文介绍了使用Java Reflection初始化成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以指出一些有用的信息,这些信息与Java中使用Reflection的最佳实践有关.

I'm hoping someone can point in the direction of some useful information pertaining to the best practices surrounding the use of Reflection in Java.

我正在支持的当前项目使用Oracle ADF Faces,并且我们发现,基于当前的目标,某些页面最终包含大量组件,这些组件需要在支持bean中进行初始化.团队中的一位开发人员设计了一种在bean构造函数中使用反射的解决方案,以初始化特定页面的所有成员组件.

The current project I'm supporting uses Oracle ADF Faces and we've found that based on the objectives at hand certain pages end up with a vast number of components which need to be initialized in the backing beans. One of the developers on the team devised a solution using reflection in the bean constructor to initialize all of the member components for a particular page.

有人担心这可能会违反最佳做法,尽管它可以节省单个开发人员一些时间,但可能会影响应用程序的性能.

The concern has been raised that this may violate best practices and though it may save individual developers some time it may impact the performance of the application.

有人以这种方式利用反射吗?是可以接受的,还是开发人员应该手动写出代码?

Has anyone utilized reflection in this way? Is it acceptable or should the developers manually write out the code?

推荐答案

在可能的情况下,应避免进行反射,因为您应注意一些通常属于编译器能力的任务.此外,反射的使用使重构更加困难,因为某些工具在反射上不起作用(请考虑Eclipse的重构).

Reflection should be avoided when possible, because you should take care of some tasks that are normally of the compiler competence; furthermore, the use of reflection makes refactoring harder, because some tools doesn't work on reflection (think about eclipse's refactor).

反思对性能有影响,但是我认为在可维护性方面这是一个小问题.

Reflection have an impact on performances, but I think this is a minor problem in respect of the mantainability issues.

如果您有不使用反射的解决方案,请使用它.

If you have a solution that doesn't use reflection, use it.

这篇关于使用Java Reflection初始化成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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