日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/5/22 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 beanutil 批量copy_Apache Commons Beanutils对象属性批量复制(pseudo-singleton) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产一区二区视频在线观看 | 日本视频h| 艳情五月 | 亚洲精品视频在线观看视频 | 日韩免费在线 | 97精品人人a片免费看 | 精品福利电影 | 神马午夜伦理影院 | 91视频在线观看网站 | 狠狠网| 深夜福利1000| 18无码粉嫩小泬无套在线观看 | 中文字幕一区二区三区夫目前犯 | 国产h视频在线观看 | 40到50岁中老年妇女毛片 | av大片在线免费观看 | 牛牛av国产一区二区 | 青娱乐精品视频 | 一区精品在线 | 免费的黄色片 | 双性人hdsexvideos | 亚洲特级黄色片 | 日本福利在线观看 | 韩国av免费在线观看 | 欧美日韩综合精品 | 久久蜜臀| 欧美福利一区 | 日本大乳奶做爰 | 天天夜碰日日摸日日澡性色av | 精品国产精品三级精品av网址 | 一本色道久久88亚洲精品综合 | 天堂av成人 | 日韩精品一区二 | 99热日本 | 秘密基地在线观看完整版免费 | 欧美xxxx83d| 欧美一级在线 | 亚洲日本一区二区 | 中文字幕在线视频免费 | 亚洲一区二区三区91 | 美女国产免费 | 亚日韩在线| 第五色婷婷 | 国产精品一区二区人妻喷水 | 波多野结衣电车痴汉 | 欧美激情影音先锋 | 在线观看视频毛片 | 强行挺进白丝老师翘臀网站 | 成人免费视频网 | 久久国产精品免费看 | 日本三级网站在线观看 | 91av福利视频 | 中文字幕一二三区 | 内射一区二区三区 | 日韩免费在线观看 | 亚洲最新av网址 | 精品视频无码一区二区三区 | 国产 福利 在线 | 五月天婷婷丁香 | 国产亚洲欧美日韩高清 | 大肉大捧一进一出好爽动态图 | 日韩wwww | 美女啪啪网| 欧美成人第一页 | 91麻豆视频在线观看 | 日韩av在线电影 | 欧美一级录像 | 绝顶高潮合集videos | 国产喷水在线 | 欧美日韩一卡二卡三卡 | 香蕉在线网站 | 一本一道久久a久久综合蜜桃 | www插插插无码免费视频网站 | 亚洲色成人一区二区三区小说 | 天天插天天操天天干 | 国产a级片视频 | 欧美亚一区二区三区 | 国产小视频自拍 | 琪琪成人 | 国产在线看 | 免费成人看视频 | av激情在线 | 免费h漫禁漫天天堂 | 黄色视屏网站 | 草免费视频| 日韩国产欧美一区二区三区 | 丰满人妻一区二区三区53视频 | 亚洲狼人综合 | 中国免费观看的视频 | 国产精品久久久久av | 人人爽人人草 | 免费极品av一视觉盛宴 | 精品一区二区三区蜜臀 | 日韩色在线观看 | 国产性生活片 | 亚洲淫| 成人aaaaa| 97毛片 | 久久免费看毛片 |