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 map;

public User(){}

public User(String name,Integer age,boolean single,

Map 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 getMap() {

return map;

}

public void setMap(Map map) {

this.map = map;

}

public String toString(){

String str = "name:"+name+" age:"+age+" single:"+single+" ";

if(null != map && map.size() > 0){

str += "map[";

Iterator 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 map = new HashMap();

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:

......

private static final ContextClassLoaderLocal BEANS_BY_CLASSLOADER

= new ContextClassLoaderLocal() {

// 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配合实现了一个线程中单例的“伪单例”模式,真正的亮点

beanutil 批量copy_Apache Commons Beanutils对象属性批量复制(pseudo-singleton)相关推荐

  1. BeanCopier、BeanUtils 对象属性拷贝

    开发为了隔离变化,常将 DAO 查询出来的DO和对前端提供的 DTO 隔离开来,它们的结构都是类似的.写很多冗长的b.setFiled(a.getFiled())这样的代码,是繁琐无意义的.于是需要简 ...

  2. BeanUtils对象之间的复制

    1.maven依赖<dependency><groupId>commons-beanutils</groupId><artifactId>commons ...

  3. java实例拷贝,Apache Commons包 BeanUtils 对象拷贝实例demo

    Apache Commons包 BeanUtils 对象拷贝实例demo 发布时间:2019-05-08作者:laosun阅读(1846) Apache Commons包 BeanUtils 对象拷贝 ...

  4. json vue 对象转数组_vue.js基于v-for实现批量渲染 Json数组对象列表数据示例

    本文实例讲述了vue.js基于v-for实现批量渲染 json数组对象列表数据.分享给大家供大家参考,具体如下: vuejs的出现减轻了对dom的直接操作,同时它提供的 v-for 渲染列表数据也给我 ...

  5. beanutils copyproperties_你还在用BeanUtils进行对象属性拷贝?

    作者:Van_Fan 在做业务的时候,为了隔离变化,我们会将DAO查询出来的DO和对前端提供的DTO隔离开来.大概90%的时候,它们的结构都是类似的:但是我们很不喜欢写很多冗长的b.setF1(a.g ...

  6. BeanUtils.copyProperties() 赋值的对象属性为空 踩过的大坑

    最近项目中有个一个类的属性想复制到另一个类的属性里,两个类的属性名称和类型都一致,想到了用 BeanUtils.copyProperties() 这个方法进行拷贝,在使用的过程中出现了一个问题:拷贝完 ...

  7. ae批量修改字体_AE脚本-批量文字替换图层样式属性编辑脚本Aescripts pt_TextEdit 2.41 + 使用教程...

    AE脚本-批量文字替换图层样式属性编辑脚本Aescripts pt_TextEdit 2.41 + 使用教程 Aescripts pt_TextEdit 2.41可以快速的修改合成中的文字图层的任何属 ...

  8. ARCGIS中按照某字段属性批量导出shp ,即将一个shp图层分割成单个的shp

    如何通过某字段属性批量导出各个字段对应的shp文件?比如我现有一副国家的的行政区划图,想要提取各个省份的shp文件. 通过Analysis Tools--Extract--Split工具实现. 步骤如 ...

  9. Java 反射将配置文件数据加载到对象属性中

    Java 反射将配置文件数据加载到对象属性中 Java 反射 可以根据类名找到相应的类,也可以将配置文件中的值加载到对应属性中. 需要用到的包:spring-core-3.1.2.Release.ja ...

最新文章

  1. mysql 索引合并
  2. 函数的实参 函数的形参 闭包 js
  3. php使用file,PHP中is_file()函数使用指南
  4. Android WebView开发问题及优化汇总
  5. Android启动(Booting)
  6. How far away ?(dfs水题)
  7. 论文浅尝 | 基于Freebase的问答研究
  8. Struts2 文件上传 文件类型 大小过滤
  9. CCAI2018 | 韩家炜:大规模文本数据挖掘的新方向
  10. Android 签名机制 v1、v2、v3
  11. VM虚拟机安装苹果雪豹操作系统
  12. 360主机卫士linux安装软件,360主机卫士Linux专杀版0.4.1 官方版(32位+64位)下载_云间下载...
  13. MyDockFinder(mydock myfinder合二为一版)
  14. PMP项目管理的就业前景
  15. DeFi黑暗森林历险记,我这样追回了百万美元
  16. linux环境启动tomcat成功后,访问链接一直在转圈
  17. 快手亮相第七届全球数字营销峰会,以全域内容营销驱动商业新增长
  18. Python列表(list)的添加元素方法 append、extend、insert-------(超级详细)
  19. Echarts 图表制作建议指南思维导图,教你什么数据画什么图
  20. Python str split方法

热门文章

  1. 【重大更新】DevExpress v17.1新版亮点(ASP.NET篇)
  2. CSS display overflow 属性 cursor光标类型
  3. [转载]ACM搜索算法总结(总结)
  4. Asp.net MVC3.0 入门指南 7.1 展示查找页面
  5. 【观影】摩托日记、盲井
  6. SDWAN动态路径选择是什么?SDWAN成本降低的意义是什么?
  7. #if...#endif的用法总结
  8. Eureka restTemplate访问超时
  9. 《流畅的python》第四章 文本和字节序列
  10. MVC初级知识之五——MVCHtmlHelper使用