Commons-BeanUtils 基于Java 反射来完成对Bean的一些操作。常用于对于JavaBean进行各种操作,克隆对象,属性等等。比较简单。
###包介绍~~~~
- org.apache.commons.beanutils – 核心包,定义一组Utils类和需要用到的接口规范
- org.apache.commons.beanutils.converters – 转换String到需要类型的类,实现Converter接口
- org.apache.commons.beanutils.locale – beanutils的locale敏感版本
- org.apache.commons.beanutils.locale.converters – converters的locale敏感版本
- org.apache.commons.collections – beanutils使用到的Collection类
###API分类
-
复制一个JavaBean的实例-- BeanUtils.cloneBean()
-
在一个JavaBean的两个实例之间复制属性-- BeanUtils.copyProperties()
BeanUtils.copyProperty() -
为一个JavaBean的实例设置成员变量(属性)值-- BeanUtils.populate()
BeanUtils.setProperty()
PropertyUtils.setSimpleProperty -
从 一个一个JavaBean的实例中读取成员变量(属性)的值 -- BeanUtils.getArrayProperty()
BeanUtils.getIndexedProperty()
BeanUtils.getMappedProperty()
BeanUtils.getNestedProperty()
BeanUtils.getSimpleProperty()
BeanUtils.getProperty()
BeanUtils.describe()
PropertyUtils.getSimpleProperty
PropertyUtils.getIndexedProperty
###依赖:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
###示例 对象定义
public class BeanutilCopyVo {
private long id;
private int age;
private boolean sex;
private float weight;
private double height;
private short classes;
private String name;
private Set<String> nicks;
private Date birthDay;
...
}
对象设值.
private static BeanutilCopyVo getVo() {
BeanutilCopyVo vo = new BeanutilCopyVo();
vo.setAge(1);
vo.setClasses((short) 2);
vo.setHeight(3d);
vo.setWeight(4f);
vo.setName("name");
vo.setId(0l);
vo.setSex(true);
vo.setBirthDay(new Date());
Set<String> nicks = new HashSet<String>();
nicks.add("1");
nicks.add("2");
nicks.add("3");
vo.setNicks(nicks);
return vo;
}
测试
@Test
public void testCopySame() {
BeanutilCopyVo vo2 = new BeanutilCopyVo();
try {
BeanUtils.copyProperties(vo2, vo); //拷贝整个对象
assertEquals(1, vo2.getAge());
assertEquals("1", BeanUtils.getProperty(vo2, "age")); //获取一个属性
BeanUtils.setProperty(vo2, "age", 2); //设置一个属性
assertEquals("2", BeanUtils.getProperty(vo2, "age"));
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//LogUtil.get(this.getClass()).error(e);
}
}
@Test
public void testCopyProperty() {
BeanutilCopyVo vo2 = new BeanutilCopyVo();
try {
BeanUtils.copyProperty(vo2, "age", "22");
assertEquals("22", BeanUtils.getProperty(vo2, "age"));
ConvertUtils.register(new BeanutilDateConverter(), Date.class); //特殊属性时间的处理
BeanUtils.copyProperty(vo2, "birthDay", "2016-09-11");
assertNotNull(BeanUtils.getProperty(vo2, "birthDay"));
// assertEquals("2016-09-11", BeanUtils.getProperty(vo2, "birthDay"));
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//LogUtil.get(this.getClass()).error(e);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
import com.zx.common.log.LogUtil;
public class BeanutilDateConverter implements Converter {
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
/**
* 要将字符串的value转换为java.util.Date类型的值
* @param targetClass 第一个参数 是你的目标类型
* @param value 要转换的值,
* @return 把要转回的值,返回出去就ok了
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public Date convert(Class targetClass, Object value) {
if (targetClass != Date.class) {
return null;
}
try {
if (value instanceof String) {
String v = (String) value;
return format.parse(v);
}
} catch (ParseException e) {
LogUtil.get(this.getClass()).error(e);
}
return null;
}
}
2016-11-06 09:29:52 星期日
来源:oschina
链接:https://my.oschina.net/u/1261935/blog/782462