<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-8255455458104156220.post-2151363385284508979</id><published>2007-07-15T09:34:00.000-07:00</published><updated>2007-07-16T02:14:59.343-07:00</updated><title type='text'>Accessing JavaBeans Nested Properties: testing Spring, BeanUtils and OGNL</title><content type='html'>One our application needs to access properties from a javabean using reflection&lt;br /&gt;e.g. get(“property1”, object)…&lt;br /&gt;&lt;br /&gt;Rather than re-inventing the wheel, I thought that we should use a library.  There are quite a few that do this kind of get/set properties… So the question was: Which One???&lt;br /&gt;&lt;br /&gt;I know of:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Spring beans (2.0.5) &lt;a href="http://www.springframework.org/"&gt;http://www.springframework.org&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Apache Commons BeanUtils (1.7.0) &lt;a href="http://jakarta.apache.org/commons/beanutils/"&gt;http://jakarta.apache.org/commons/beanutils/&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;my colleague Gerald mentioned OGNL from &lt;a href="http://www.ognl.org/"&gt;www.ognl.org&lt;/a&gt; (pronounced ‘like a drunken orthogonal’ to quote their documentation).&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;So with 3 candidates… which one is the best performing?&lt;br /&gt;&lt;br /&gt;OGNL seems to be, by far, the most flexible and rich library, but does that means it runs like a dead dog?&lt;br /&gt;&lt;br /&gt;I limited the problem to accessing a property value: being simple, nested or as part of an array.&lt;br /&gt;The Test: I shall access 100,000 a series of 8 properties. The classes are:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class A {&lt;br /&gt;  private int intProperty;&lt;br /&gt;  private Long longProperty;&lt;br /&gt;  private String stringProperty;&lt;br /&gt;  private Date dateProperty;&lt;br /&gt;  private B b = new B();&lt;br /&gt;}&lt;br /&gt;public class B {&lt;br /&gt;  private int intProperty = 5;&lt;br /&gt;  private C c = new C();&lt;br /&gt;  private D[] d = new D[10];&lt;br /&gt;}&lt;br /&gt;public class C {&lt;br /&gt;  private String stringProperty;&lt;br /&gt;}&lt;br /&gt;public class D {&lt;br /&gt;  private int intProperty = 1;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The Test creates one instance of A, that contains 1 instance of B which contains 1 instance of C and an array of 10 Ds.  I hope this is clear…&lt;br /&gt;The set of properties to get are: "intProperty", "longProperty", "dateProperty", "stringProperty", "b.intProperty", "b.c.stringProperty", "b.d[1].intProperty", "b.d[7].intProperty".&lt;br /&gt;&lt;br /&gt;So… the results?&lt;table&gt;&lt;theading&gt;&lt;/theading&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="font-weight: bold;"&gt;Library&lt;/td&gt;&lt;td style="font-weight: bold;"&gt;Total time (ms)&lt;/td&gt;&lt;td style="font-weight: bold;"&gt;average per set (micro sec)&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Spring&lt;/td&gt;&lt;td&gt;1,783 ms&lt;/td&gt;&lt;td&gt;17.8 micro sec&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bean Utils&lt;/td&gt;&lt;td&gt;2,242 ms&lt;/td&gt;&lt;td&gt;22.4 micro sec&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OGNL&lt;/td&gt;&lt;td&gt;50,293 ms&lt;/td&gt;&lt;td&gt;503 micro sec&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OGNL Expression&lt;/td&gt;&lt;td&gt;&lt;b&gt;1,595 ms&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;16 &lt;/span&gt;micro sec&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_H-RhdSKB1Go/RppQwccRzVI/AAAAAAAAAhE/fW8s0VNB8is/s1600-h/reflect2.PNG"&gt;&lt;img style="cursor: pointer;" src="http://3.bp.blogspot.com/_H-RhdSKB1Go/RppQwccRzVI/AAAAAAAAAhE/fW8s0VNB8is/s320/reflect2.PNG" alt="" id="BLOGGER_PHOTO_ID_5087467522267139410" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;What does this tell us?&lt;br /&gt;&lt;br /&gt;OGNL is at the same time the slowest and the fastest library on my laptop (Lenovo, dual-core) under java 1.5.0_10.  OGNL has 2 mechanisms, one is simply to call Ognl.getValue(“pathToProperty”, object) and the other one is to evaluate the expression upfront by Object expression = Ognl.parseExpression(“pathToProperty”) and then Ognl.getValue(expression, object);&lt;br /&gt;&lt;br /&gt;The second one is the fastest mechanism so, if you have the ability to ‘pre-compile’ your expressions, OGNL is for you… otherwise Spring Beans is doing a good job!&lt;br /&gt;&lt;br /&gt;The entire source code and Eclipse project is available &lt;a href="http://objectlabkit.sourceforge.net/objectlab/reflection-test.zip"&gt;here&lt;/a&gt;, feel free to comment and tell us about your experience.&lt;br /&gt;&lt;br /&gt;Enjoy!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8255455458104156220-2151363385284508979?l=objectlab.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://objectlab.blogspot.com/feeds/2151363385284508979/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=8255455458104156220&amp;postID=2151363385284508979' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8255455458104156220/posts/default/2151363385284508979'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8255455458104156220/posts/default/2151363385284508979'/><link rel='alternate' type='text/html' href='http://objectlab.blogspot.com/2007/07/accessing-javabeans-nested-properties.html' title='Accessing JavaBeans Nested Properties: testing Spring, BeanUtils and OGNL'/><author><name>ObjectLab/Appendium</name><uri>http://www.blogger.com/profile/10408712629355326011</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='15756673423235048599'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_H-RhdSKB1Go/RppQwccRzVI/AAAAAAAAAhE/fW8s0VNB8is/s72-c/reflect2.PNG' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry>