beanutil 批量copy_Apache Commons Beanutils对象属性批量复制(pseudo-singleton)
Apache Commons Beanutils為開源軟件,可在Apache官網(wǎng)http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi下載,使用它還需另一個(gè)Apache開源軟件Apache
Commons Logging,可在Apache官網(wǎng)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());
}
}
運(yùn)行結(jié)果為(比較復(fù)雜的屬性也可以被復(fù)制并且只有擁有g(shù)et/set方法的屬性才可以被復(fù)制):
name:tom age:123 single:true map[1:1 ]
name:tom age:null single:true map[1:1 ]
更詳細(xì)的使用說明就不介紹了,接下來看看BeanUtils是如何實(shí)現(xiàn)的,好戲剛剛開始
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()方法并不是一個(gè)簡單的單例模式,而是一個(gè)“偽單例”模式
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配合實(shí)現(xiàn)了一個(gè)線程中單例的“偽單例”模式,真正的亮點(diǎn)
總結(jié)
以上是生活随笔為你收集整理的beanutil 批量copy_Apache Commons Beanutils对象属性批量复制(pseudo-singleton)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: audio 上一首 下一首 自定义样式_
- 下一篇: openlayer右键菜单_使用Open