sixlegs.com / blog / java / death-to-dynabeans.html

Root Beer Logo Root Beer

Chris Nokleberg's Fizzy Weblog

December 2003
Su M Tu W Th F Sa
  1 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Previous  |  Next  |  More...
#  Death to DynaBeans

"A DynaBean is a Java object that supports properties whose names and data types, as well as values, may be dynamically modified. To the maximum degree feasible, other components of the BeanUtils package will recognize such beans and treat them as standard JavaBeans for the purpose of retrieving and setting property values."

While I certainly understand the need for a dynamic JavaBean, I agree with others who feel that DynaBeans are more trouble than they are worth.

The impedance mismatch with JSTL due to not implementing Map is bad enough, but to me the worst thing is that you lock yourself into using BeanUtils and its ilk until the end of time. If you have a JavaBean-oriented library that does not use BeanUtils...tough luck!

Not one to let my CGLIB Golden Hammer go to waste [Emoticon], I have checked in a BeanGenerator class into CVS. You use it like so:

BeanGenerator bg = new BeanGenerator();
bg.addProperty("foo", Double.TYPE);
bg.addProperty("bar", String.class);
Object bean = bg.create();

The generated class is an real JavaBean, which means you can use standard bean utilities. This includes all of the classes in the net.sf.cglib.beans package (BeanCopier, BeanMap, and BulkBean). Do your part to end the tyranny of DynaBeans!

Update: In the comments I was reminded of http://dynclass.sf.net, which is a standalone library that serves the same purpose. I wonder why it isn't more widely used? Also, thanks to Juozas Baliuka, the original author of BeanGenerator and founder of CGLIB!

[Powered By FreeMarker]  [Valid Atom 1.0]  [Weblog Commenting by HaloScan.com]