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.
Copying properties of one object to another object is often tedious and error-prone for developers. BeanUtils class provides a copyProperties method that copies the properties of source object to target object where the property name is same in both objects.
The Java BeanUtils are the components of the Apache Commons which are derived from JavaAPI and provides component architecture for the Java language. The Java BeanUtils design patterns uses utility classes that helps to get and set the property values on Java classes for retrieving and defining the bean properties.
As they both ultimately use Reflection you aren't likely to notice much difference, unless the higher-level API is doing things you don't need done. See also java. beans.
public class ReflectionToStringBuilder extends ToStringBuilder. Assists in implementing Object. toString() methods using reflection. This class uses reflection to determine the fields to append. Because these fields are usually private, the class uses AccessibleObject.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With