日韩性视频-久久久蜜桃-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)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 精品999视频 | 国产香蕉97碰碰碰视频在线观看 | 久久精品高清视频 | 日韩中文字| a资源在线 | 1区2区视频 | 在线看欧美 | 亚洲+小说+欧美+激情+另类 | 激情无码人妻又粗又大 | 青青在线视频观看 | ww久久| 国产乱码一区二区三区播放 | 日韩中字在线 | 欧美国产片 | 国产一区二区a | 日精品| 欧美一区,二区 | 欧美中文字幕在线播放 | av高清在线观看 | 国产在线视频在线观看 | 亚洲最大视频网 | 久久国产精品无码一区二区 | 亚洲国产精品激情在线观看 | 国产人妖在线播放 | 国产精品久久久久三级 | 九九热播视频 | 在线观看免费黄视频 | 欧美成人精品一区二区男人小说 | 亚洲国产精品成人久久蜜臀 | 色月婷婷| 香蕉视频网页 | 久久精品99国产 | 日本一区二区三区在线播放 | 日韩欧美激情 | 久久六六 | 午夜电影天堂 | 成人a在线观看 | 色漫| 成年人爱爱视频 | 免费激情视频网站 | 国产区av| 一级黄色美女 | 青青草免费公开视频 | 国产成人区 | 人人狠狠| 自拍偷拍电影 | 日韩欧美网址 | 国产三级久久 | 亚洲精品欧美日韩 | 动漫美女被吸奶 | 四虎国产在线观看 | 91成人福利在线 | 成年人网站在线免费观看 | 亚洲九九爱 | 精品国偷自产在线 | 超碰福利在线观看 | 国产无遮挡又黄又爽 | 草莓视频在线观看入口w | 手机成人免费视频 | 99久久影院| 日韩va中文 | 婷婷在线视频观看 | 国产毛片久久久久久久 | 来吧亚洲综合网 | 香蕉视频网站在线观看 | 在线精品免费视频 | 日本性爱视频在线观看 | 性自由色xxxx免费视频 | 99人妻少妇精品视频一区 | 欧美性受xxxx白人性爽 | 91人人澡人人爽人人精品 | 婷婷亚洲精品 | 男人免费网站 | 国产区欧美区日韩区 | 久久久久国色av免费观看性色 | 久久丝袜美腿 | 91嫩草在线 | 人碰人人 | 1024视频污 | 一区二区三区伦理片 | 永久黄网站 | 亚洲男人天堂影院 | 欧美裸体精品 | 五月99久久婷婷国产综合亚洲 | 无码人妻丰满熟妇啪啪欧美 | 国产伦精品一区二区三区在线观看 | 欧美成人免费观看 | 性爱免费视频 | 在线观看色网 | 黄色免费入口 | 操日韩 | 久草福利网 | 国产精品污www在线观看 | 精品无码一区二区三区爱欲 | 日韩精品一 | 天天av综合 | 久久免费电影 | 极品女神无套呻吟啪啪 | 天天爽夜夜爽一区二区三区 |