BeanUtils.copyProperties() 用法
转载
第一步: BeanUtils.copyProperties()与PropertyUtils.copyProperties()
1、 通过反射将一个对象的值赋值个另外一个对象(前提是对象中属性的名字相同)。
2、 BeanUtils.copyProperties(obj1,obj2); 经常闹混不知道是谁给谁赋值,无意中先到"前面复制给后面"这个词来帮助自己记忆这个功能。即将obj2的值赋值给obj1。
3、 如果2中实例obj2为空对象,即值new了他的实例并没有赋值的话obj1对应的属性值也会被设置为空置。
4、BeanUtils与PropertyUtils对比(这里对比copyProperties方法)
PropertyUtils的copyProperties()方法几乎与BeanUtils.copyProperties()相同,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,BeanUtils 不支持这个功能,但是BeanUtils速度会更快一些。
主要支持转换类型如下:
* java.lang.BigDecimal
* java.lang.BigInteger
* boolean and java.lang.Boolean
* byte and java.lang.Byte
* char and java.lang.Character
* java.lang.Class
* double and java.lang.Double
* float and java.lang.Float
* int and java.lang.Integer
* long and java.lang.Long
* short and java.lang.Short
* java.lang.String
* java.sql.Date
* java.sql.Time
* java.sql.Timestamp
不支持java.util.Date转换,但支持java.sql.Date。如果开发中Date类型采用util而非sql.Date程序会抛出argument mistype异常。
第二步:扩展BeanUtils支持时间类型转换
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
/**
* 重写BeanUtils.copyProperties
*
* @author monkey
*/
public class BeanUtilsExtends extends BeanUtils {
static {
ConvertUtils.register(new DateConvert(), java.util.Date.class);
ConvertUtils.register(new DateConvert(), java.sql.Date.class);
}
public static void copyProperties(Object dest, Object orig) {
try {
BeanUtils.copyProperties(dest, orig);
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
}
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.Converter;
/**
* 重写日期转换
*
* @author houzhiqing
*/
public class DateConvert implements Converter {
public Object convert(Class arg0, Object arg1) {
String p = (String) arg1;
if (p == null || p.trim().length() == 0) {
return null;
}
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.parse(p.trim());
} catch (Exception e) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(p.trim());
} catch (ParseException ex) {
return null;
}
}
}
}
———————————————————————————————————————------------------------------
今天开发中遇到一个问题,其实也算不上是问题,只是本猿比较懒而已!目前本猿主要做的是接口开发,现在需要将接口提供方的一个类中的部分字段挪到我自己的项目来,然而奈何本猿太懒,不想一个一个set、get……
要不然说“懒”是促进社会科技进步的最大动力呢!!!鉴于这一情况,本猿果断使用了Spring神器的一个工具包——BeansUtils,简直帅出了宇宙!下面就让本猿来带你们见识见识这个逆天的工具。
Spring这里不多做说明了,简而言之言而简之就是一个大容器,至于容器中有什么东西以后有时间再细说,这个大容器呢在我们开发中经常用来作为一个管家管理我们的bean,既然管理bean,那我这个类到类的属性应该也可以管的咯!
上demo:
这是一个类,暂且定他为远程的,
package com.fcloud.mobile.common.demo;
/**
* Created by Promonkey on 2017/3/14.
*/
public class LongDistanceBean{
/**
* 用户ID
*/
private String userId;
/**
* 居住城市
*/
private String liveCity;
/**
* 工作城市
*/
private String workCity;
/**
* 工作电话
*/
private String workPhone;
/**
* 构造器
*/
public LongDistanceBean(String userId, String liveCity, String workCity, String workPhone) {
this.userId = userId;
this.liveCity = liveCity;
this.workCity = workCity;
this.workPhone= workPhone;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLiveCity() {
return liveCity;
}
public void setLiveCity(String liveCity) {
this.liveCity = liveCity;
}
public String getWorkCity() {
return workCity;
}
public void setWorkCity(String workCity) {
this.workCity = workCity;
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
@Override
public String toString() {
return "LongDistanceBean{" +
"userId='" + userId + '\'' +
", liveCity='" + liveCity + '\'' +
", workCity='" + workCity + '\'' +
", workPhone='" + workPhone + '\'' +
'}';
}
}
本地的类:
package com.fcloud.mobile.common.demo;
/**
* Created by Promonkey on 2017/3/14.
*/
public class LocalBean{
/**
* 用户ID
*/
private String userId;
/**
* 居住城市
*/
private String liveCity;
/**
* 工作城市
*/
private String workCity;
/**
* 工作电话
*/
private String workPhone;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLiveCity() {
return liveCity;
}
public void setLiveCity(String liveCity) {
this.liveCity = liveCity;
}
public String getWorkCity() {
return workCity;
}
public void setWorkCity(String workCity) {
this.workCity = workCity;
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
@Override
public String toString() {
return "LocalBean{" +
"userId='" + userId + '\'' +
", liveCity='" + liveCity + '\'' +
", workCity='" + workCity + '\'' +
", workPhone='" + workPhone + '\'' +
'}';
}
}
现在假设我调用某个接口获得了远程端Bean的对象及他的数据:
LongDistanceBeand longDistance= new LongDistanceBean("10001", "江西", "上海", "100-110-111");
但是我需要用本地的一个类去封装这些数据,而且不能通过继承(子类父类)的关系去实现,实际开发中经常会遇到这种情况。怎么办呢?别告诉我你想一个一个 localBean.setUserId(longDistance.getUserId())……, 那我告诉你我要封装的数据其实有二十几个字段,你给我去一个一个set!
这时候我们就需要Spring神器了:
public static void main(String[] args) {
LongDistanceBean longDistance= new LongDistanceBean("10001", "江西", "上海", "100-110-111");
System.out.println(longDistance);
LocalBean localBean= new LocalBean();
System.out.println(LocalBean);
System.out.println("开始复制");
BeanUtils.copyProperties(longDistance, localBean);
System.out.println(longDistance);
System.out.println(localBean);
}
Copy结束:
BeanUtils.copyProperties(source, target);
source —— 复制源
target —— 赋值目标
建议自己去体会一下,需要的jar包,:
maven项目:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>