Apache Commons Beanutils为开源软件,可在Apache官网http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi下载,使用它还需另一个Apache开源软件Apache
Commons Logging,可在Apache官网http://commons.apache.org/proper/commons-logging/download_logging.cgi下载,我使用的是commons-beanutils-1.9.1-bin.zip和commons-logging-1.1.3-bin.zip

public class User {
private String name;
private Integer age;
private boolean single;
private Map<Integer,Integer> map; public User(){} public User(String name,Integer age,boolean single,
Map<Integer,Integer> map){
this.name = name;
this.age = age;
this.single = single;
this.map = map;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public boolean isSingle() {
return single;
} public void setSingle(boolean single) {
this.single = single;
} public Map<Integer, Integer> getMap() {
return map;
} public void setMap(Map<Integer, Integer> map) {
this.map = map;
} public String toString(){
String str = "name:"+name+" age:"+age+" single:"+single+" ";
if(null != map && map.size() > 0){
str += "map[";
Iterator<Integer> it = map.keySet().iterator();
while(it.hasNext()){
Integer key = it.next();
Integer value = map.get(key);
str += key+":"+value+" ";
}
str += "]";
}
return str;
}
}
package com.sean;

import java.util.HashMap;
import java.util.Map; import org.apache.commons.beanutils.BeanUtils; public class Test {
public static void main(String[] args) throws Exception {
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(1, 1);
User u1 = new User("tom",123,true,map);
User u2 = new User();
BeanUtils.copyProperties(u2, u1);
System.out.println(u1.toString());
System.out.println(u2.toString());
}
}

运行结果为(比较复杂的属性也可以被复制并且只有拥有get/set方法的属性才可以被复制):

name:tom age:123 single:true map[1:1 ]
name:tom age:null single:true map[1:1 ]

更详细的使用说明就不介绍了,接下来看看BeanUtils是如何实现的,好戏刚刚开始

BeanUtils:

.......
public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException {
BeanUtilsBean.getInstance().copyProperties(dest, orig);
}
......
/**
* Gets the instance which provides the functionality for {@link BeanUtils}.
* This is a pseudo-singleton - an single instance is provided per (thread) context classloader.
* This mechanism provides isolation for web apps deployed in the same container.
*
* @return The (pseudo-singleton) BeanUtils bean instance
*/
public static BeanUtilsBean getInstance() {
return BEANS_BY_CLASSLOADER.get();
}
......

这里特意带上了方法说明,getInstance()方法并不是一个简单的单例模式,而是一个“伪单例”模式

ContextClassLoaderLocal<T>:

......
private static final ContextClassLoaderLocal<BeanUtilsBean> BEANS_BY_CLASSLOADER
= new ContextClassLoaderLocal<BeanUtilsBean>() {
// Creates the default instance used when the context classloader is unavailable
@Override
protected BeanUtilsBean initialValue() {
return new BeanUtilsBean();
}
};
......
/**
* Gets the instance which provides the functionality for {@link BeanUtils}.
* This is a pseudo-singleton - an single instance is provided per (thread) context classloader.
* This mechanism provides isolation for web apps deployed in the same container.
* @return the object currently associated with the context-classloader of the current thread.
*/
public synchronized T get() {
// synchronizing the whole method is a bit slower
// but guarantees no subtle threading problems, and there's no
// need to synchronize valueByClassLoader // make sure that the map is given a change to purge itself
valueByClassLoader.isEmpty();
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) { T value = valueByClassLoader.get(contextClassLoader);
if ((value == null) && !valueByClassLoader.containsKey(contextClassLoader)) {
value = initialValue();
valueByClassLoader.put(contextClassLoader, value);
}
return value;
}
} catch (SecurityException e) { /* SWALLOW - should we log this? */ }
// if none or exception, return the globalValue
if (!globalValueInitialized) {
globalValue = initialValue();
globalValueInitialized = true;
}//else already set
return globalValue;
}
.......

和ContextClassLoader配合实现了一个线程中单例的“伪单例”模式,真正的亮点

Apache Commons Beanutils对象属性批量复制(pseudo-singleton)的更多相关文章

  1. Apache Commons Beanutils 一 (使用PropertyUtils访问Bean属性)

    BeanUtils简要描述 beanutils,顾名思义,是java bean的一个工具类,可以帮助我们方便的读取(get)和设置(set)bean属性值.动态定义和访问bean属性: 细心的话,会发 ...

  2. BeanUtils对象属性copy的性能对比以及源码分析

    1. 对象属性拷贝的常见方式及其性能 在日常编码中,经常会遇到DO.DTO对象之间的转换,如果对象本身的属性比较少的时候,那么我们采用硬编码手工setter也还ok,但如果对象的属性比较多的情况下,手 ...

  3. org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法区别

    知识点 org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作 ...

  4. org.apache.commons.beanutils.BeanUtils的常见用法

    import org.apache.commons.beanutils.BeanUtils BeanUtils1. public static void copyProperty(Object bea ...

  5. Apache Commons Beanutils 三 (BeanUtils、ConvertUtils、CollectionUtils...)

    前言 前面已经学习了Apache Commons Beanutils包里的PropertyUtils和动态bean,接下来将学习剩下的几个工具类,个人觉得还是非常实用的,特别是CollectionUt ...

  6. Apache Commons Beanutils 二 (动态Bean - DynaBeans)

    相关背景 上一篇介绍了PropertyUtils的用法,PropertyUtils主要是在不修改bean结构的前提下,动态访问bean的属性: 但是有时候,我们会经常希望能够在不定义一个Java类的前 ...

  7. org.apache.commons.beanutils.ConversionException: No value specified解决办法

    转自:https://www.cnblogs.com/linjiqin/archive/2011/07/21/2112628.html 当用到了java.sql.Date时间等非内置对象时,如果对象为 ...

  8. org.apache.commons.beanutils.BeanMap简单使用例子

    一.org.apache.commons.beanutils.BeanMap; 将一个java bean允许通过map的api进行调用, 几个支持的操作接口: Object get(Object ke ...

  9. 再续前缘-apache.commons.beanutils的补充

    title: 再续前缘-apache.commons.beanutils的补充 toc: true date: 2016-05-32 02:29:32 categories: 实在技巧 tags: 插 ...

随机推荐

  1. C#开发漂亮的数字时钟

    今天用C#做了一个漂亮的数字时钟.界面如下. 实现技术:主要是通过Graphics类的DrawImage方法来绘制数字时钟中所有的数字,这些数字是从网上找的一些图片文件.时钟使用DateTime中No ...

  2. 保持与 Microsoft Azure Files 的连接

    我们在最近的博客文章中介绍了 Azure StorageFiles的预览版,请单击此处.该文章包含 Azure Files 的相关信息,说明了如何申请预览版并开始使用,还介绍了一些有助于创建共享和传 ...

  3. 【转】VIRTUALBOX导入已有.VDI文件步骤

    原文网址:http://blog.csdn.net/wanghai__/article/details/6703923 1.安装SUN VIRTUALBOX 2.新建,进入到“新建虚拟电话”对话框,下 ...

  4. ruby编程语言-学习笔记1

    安装完 ruby ri irb ruby-devel 1. 先来个简单的,写个helloworld  给新手们 (terminal中,# 代表root权限,$ 代表用户权限, 前面的就不写了.) # ...

  5. WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z

    操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作.那么对于WinForm程序怎么实现呢? 实现原理:拦截Application ...

  6. HDOJ-ACM1020(JAVA)

    题意:给字母计数,如果是字母后面没有相同的,原样输出,如果有则输出这个字母的个数和字母本身. import java.util.*; import java.io.*; public class Ma ...

  7. Codeforces182D - Common Divisors(KMP)

    题目大意 如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S1和S2,求它们的公共因子个数 题解 如果它们有公共因子,那么显然它们的最小公共因子肯定是 ...

  8. POJ3254Corn Fields(状压DP)

    题意: John 有一个豪华的M*N个格子组成的新牧场 他想种美味的玉米 但是有些位置不能种 而且他种地不选择相邻的格子 求所有可能的种地方法 (不种也算一种选择)输入:第一行M和N, 第二行M*N地 ...

  9. PC-信使服务之不用聊天软件也能通信

    net send 192.168.1.2 OK 二台电脑都要开启messenger服务.

  10. 8-15-Exercise

    8-15-小练 这次的题目......只觉得泪奔啊......T T A.HDU 1042   N! 因为0<=n<=1000,故一定要用数组或字符串[同样因为n<=1000故用数组 ...