BeanUtils与ReflectionToStringBuilder的性能(用于Bean类) [英] Performance of BeanUtils vs. ReflectionToStringBuilder (for use in Bean classes)

查看:453
本文介绍了BeanUtils与ReflectionToStringBuilder的性能(用于Bean类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序中有很多Java Bean类,并且我试图找到一种在这些Bean中实现toString()方法的简单方法. toString()方法将用于记录整个应用程序,并应打印Bean中所有属性的属性-值"对.

我正在尝试两种选择:
1. BeanUtils.describe()(Apache commons-beanutils)
2. ReflectionToStringBuilder.toString()(Apache commons-lang)

由于这是一个预期会带来高流量的Web应用程序,因此该实现必须轻巧且不会影响性能. (内存使用,处理器使用等是主要考虑因素.)

我想知道其中哪一项按照上述标准效果更好.据我了解,反射是一项繁重的工作,但是更多的细节和对这两个选项的深入了解将有助于我选择最佳的解决方案.

解决方案

我们在对象的toString()方法中使用ToStringBuilder.reflectionToString().在生产环境中,我们在运行时没有任何问题.当然,我们很少使用toString()方法.

我们也使用BeanUtils.describe(),但出于其他目的. BeanUtils使用PropertyUtilsBean来保留对其进行自省的bean的内部缓存.似乎这将使它比其他具有性能上的优势,但是在ReflectionToString源代码中有一点点探索,并且由于它最终依赖于java.lang.Class的实现,因此缓存也在那里发挥了作用.

这两种方法看起来都是可行的选择,但是BeanUtils.describe()将返回属性Map,其中reflectionToString将返回格式化的String.我想这取决于您要如何处理输出.

我建议,如果您的应用程序严重依赖于在对象上调用toString(),则使用特定的实现可能会更有益.

I have a large number of Java bean classes in my web application, and I am trying to find a simple way to implement the toString() methods in these beans. The toString() method would be used for logging throughout the application, and should print the attribute-value pairs of all attributes in the bean.

I am trying out two alternatives:
1. BeanUtils.describe() (Apache commons-beanutils)
2. ReflectionToStringBuilder.toString() (Apache commons-lang)

Since this is a web application expected to have high traffic, the implementation has to be lightweight and should not impact performance. (Memory use, processor use, etc are main considerations).

I'd like to know which of these performs better according the criteria mentioned above. As far as I know, reflection is a heavy operation, but more details and insight into both these options would help me choose the optimal solution.

解决方案

We use ToStringBuilder.reflectionToString() in our objects' toString() methods. We have not had any issues running like this in a production environment. Granted, we rarely use the toString() method.

We also use BeanUtils.describe(), but for another purpose. BeanUtils uses PropertyUtilsBean which keeps an internal cache of beans for which it has performed introspection. It would seem that this would give it a performance advantage over the other, but a little poking around in the reflectionToString source and it seems that since it ultimately relies on the implementation of java.lang.Class, caching comes into play there as well.

Either looks like a viable choice, but BeanUtils.describe() will return a Map of properties where reflectionToString will return a formatted String. I guess it depends on what you want to do with the output.

I would suggest that if your application is heavily dependent on calling toString() on your objects, having a specific implementation might be more beneficial.

这篇关于BeanUtils与ReflectionToStringBuilder的性能(用于Bean类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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