现在有个复杂对象bean需要在赋值后在另一个地方用,想通过复制的方式拿到这个对象。首选是深度克隆,虽然发现该对象的父类已经实现了Cloneable接口,但父类是通过jar包引入的,而且在clone方法中对该bean一个map<String, Object>属性的处理有问题:获取到map的value时,直接强制转型为某一具体bean,而子类这个map在put时并非该具体bean的类型,因此克隆时报转型失败错误。考虑方案二,通过java序列化方式克隆,父类也已经实现了Serializable接口,但写对象和度对象方法都是空实现,本地重新实现后也报错了。使用json序列化倒是可以,就是性能不高。最后决定采用Dozer框架,Dozer底层也是apache的beanUtils方式复制bean,但更易用,可以直接用xml文件或者注解映射两个类,类可以是不同类型,字段也可以是不同类型、不同名称。
使用前需要导入5个jar包:
commons-beanutils-1.9.3.jar
commons-lang3-3.5.jar
commons-logging-1.2.jar
dozer-5.4.0.jar
slf4j-api-1.7.25.jar
这里通过注解来实现一个例子,具体使用参见下面代码:
package com.wulinfeng.dozer; import java.util.Date; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class CopyComponent { private String name; private int age; private Date date; private Map<String, String> internalMap; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public Map<String, String> getInternalMap() { return internalMap; } public void setInternalMap(Map<String, String> internalMap) { this.internalMap = internalMap; } @Override public String toString() { StringBuilder names = new StringBuilder(); if (null != internalMap && internalMap.entrySet() != null) { Iterator<Entry<String, String>> iter = internalMap.entrySet().iterator(); while (iter.hasNext()) { Entry<String, String> c = iter.next(); names.append("key: "); names.append(c.getKey()); names.append("value: "); names.append(c.getValue()); } } return "name: " + name + ", age: " + age + ", date: " + date + ", list : " + names.toString(); } }
package com.wulinfeng.dozer; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.dozer.DozerBeanMapper; import org.dozer.Mapping; public class Component { @Mapping("age") private String desc; @Mapping("name") private Integer price; private Date date; @Mapping("internalMap") private Map<String, String> externalAttrMap; public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public Map<String, String> getExternalAttrMap() { return externalAttrMap; } public void setExternalAttrMap(Map<String, String> externalAttrMap) { this.externalAttrMap = externalAttrMap; } public static void main(String[] args) { Component component = new Component(); // String -> int component.setDesc("9999"); // int -> String component.setPrice(30); // date -> date component.setDate(new Date()); // Map -> List Component componentpage = new Component(); componentpage.setDesc("hello"); Map<String, String> ext = new HashMap<String, String>(); ext.put("name", "fengjiangtao"); component.setExternalAttrMap(ext); DozerBeanMapper dozerBeanMapper = new DozerBeanMapper(); CopyComponent copy = dozerBeanMapper.map(component, CopyComponent.class); System.out.println(copy); } }
运行结果:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. name: 30, age: 9999, date: Sun Apr 23 23:06:02 CST 2017, list : key: namevalue: fengjiangtao
时间: 2024-10-11 06:29:14