前段時間,對系統進行性能測試,發現Spring的BeanUtils阻塞了很多線程,下面是高並發時的thread dump信息:
1 "RMI TCP Connection(2657)-127.0.0.1" daemon prio=10 tid=0x0000000057ade800 nid=0x2f02 waiting for monitor entry [0x000000005860e000]
2 java.lang.Thread.State: BLOCKED (on object monitor)
3 at java.util.Collections$SynchronizedMap.get(Collections.java:1975)
4 - waiting to lock <0x0000000765c72f18> (a java.util.Collections$SynchronizedMap)
5 at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:135)
6 at org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:340)
當時想能否數據庫層和頁面展現層使用同一套POJO,避免對象的拷貝。但這樣同時也會導致頁面展現層和數據庫層間的耦合層度較高。在網上查了下資料后才了解到CGLib的BeanCopier的性能要好很多,如下是摘要:
- BeanCopier的性能是PropertyUtils (apache-common)的504倍。
- PropertyUtils的性能是BeanUtils(apache-common)的1.71倍
但BeanCopier不支持集合類屬性的拷貝(待驗證?)。在改成BeanCopier后,系統性能比之前好了很多。在使用BeanCopier時,創建BeanCopier實例會很耗時,建議能緩存這個實例。
相關鏈接:
cglib相關性能測試對比:http://blog.csdn.net/liulin_good/article/details/6411215
cglib源碼學習交流:http://agapple.iteye.com/blog/799827
我也造了個輪子:BeanMapping(屬性拷貝):http://www.iteye.com/topic/1075671