Apache-commons.BeanUtils浅析
BeanUtils:通过反射控制JavaBean属性的实用方法。
1.cloneBean(Object) Object
克隆一个bean是基于可用属性的get和set方法,即使这个bean类没有实现Cloneable接口方法的实现是有BeanUtilBean的newbean = bean.getClass().newInstance();
对newbean的属性设值采用PropertyUtilsBean.copyProperties内部采用java的内省调用get/set方法进行获取bean属性/设置newbean属性。
API:
Note: this method creates a shallow clone.采用浅复制的方式
实例:
public class Address
{
private String province;
private String city;
//getter/setter
}
public class Person
{
private Integer id;
private String name;
private Address address;
//getter/setter
}
public void test1()
{
Person bean = new Person();
Address address = new Address();
address.setProvince("hunan");
address.setCity("huaihua");
bean.setId(1);
bean.setName("name1");
bean.setAddress(address);
try
{
//----test shadow clone bean----
Person bean2 = (Person) BeanUtils.cloneBean(bean);
bean2.getAddress().setProvince("beijing");
bean2.getAddress().setCity("beijing");
System.out.println(bean.getAddress()+":::"+bean2.getAddress()); }catch(Exception e)
{
e.printStackTrace();
}
}
结果:
Address [province=beijing, city=beijing]:::Address [province=beijing, city=beijing]
2.copyProperties(Object dest, Object orig)
复制属性值从源bean到目标bean对于所有的属性,采用的是shallow copy方式,内部采用内省的方式获取get/set方法来获取源对象的属性和对目标对象的设值。
实例:
@Test
public void testcopyProperties()
{
Person bean = new Person();
Address address = new Address();
address.setProvince("hunan");
address.setCity("huaihua");
bean.setId(1);
bean.setName("name1");
bean.setAddress(address);
Person dest = new Person();
try
{
BeanUtils.copyProperties(dest, bean);
System.out.println("dest "+dest);
}catch(Exception e)
{
e.printStackTrace();
}
}
结果:
dest Person [id=1, name=name1, address=Address [province=hunan, city=huaihua]]
实例:
public class Student
{
private Integer id;
private String name;
private Date date;
//gettter/settter
}
@Test
public void testcopyProperties()
{
Person bean = new Person();
Address address = new Address();
address.setProvince("hunan");
address.setCity("huaihua");
bean.setId(1);
bean.setName("name1");
bean.setAddress(address);
Student dest = new Student();
try
{
BeanUtils.copyProperties(dest, bean);
System.out.println(dest);
}catch(Exception e)
{
e.printStackTrace();
}
}
结果:
Student [id=1, name=name1, date=null]
3.copyProperty(Object bean, String name, Object value)
复制一个具体的属性值到具体的bean中,并执行所需的任何类型转换。采用内省的方式对bean进行设值。
4.Map<String, String> describe(Object bean)
返回指定的实体提供一个读方法的全部属性集。注:任何一个类都是Object的子类,在子类中存在getClass(),会被继承读方法,同时也会被作为集合的属性。
实例:
@Test
public void testDescribe()
{
Person bean = new Person();
bean.setId(1);
bean.setName("world");
try
{
Map<String, String> map = BeanUtils.describe(bean);
System.out.println("map: "+map);
} catch (IllegalAccessException e)
{
e.printStackTrace();
} catch (InvocationTargetException e)
{
e.printStackTrace();
} catch (NoSuchMethodException e)
{
e.printStackTrace();
}
}
结果:
map: {address=null, name=world, id=1, class="class" com.beanutil.px.Person}
5.String getProperty(Object bean, String name)
获取bean指定的属性值,内部采用内省的方式获取调用get方法
6. populate(Object bean, Map<String, ? extends Object> properties)
采用Map作为参数对bean进行设值。内部通过遍历Map调用setProperty(bean, name, value)设值
String[] getArrayProperty(Object bean, String name)
返回一个指定的数组属性,作为String[]返回
String getIndexedProperty(Object bean, String name)
返回bean中属性的索引值
String getMappedProperty(Object bean, String name)
String getMappedProperty(Object bean, String name, String key)
Apache-commons.BeanUtils浅析的更多相关文章
- Apache Commons BeanUtils
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...
- myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入
com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...
- 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*
2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...
- Apache Commons Beanutils对象属性批量复制(pseudo-singleton)
Apache Commons Beanutils为开源软件,可在Apache官网http://commons.apache.org/proper/commons-beanutils/download_ ...
- org.apache.commons.beanutils.BeanMap简单使用例子
一.org.apache.commons.beanutils.BeanMap; 将一个java bean允许通过map的api进行调用, 几个支持的操作接口: Object get(Object ke ...
- 对于Java Bean的类型转换问题()使用 org.apache.commons.beanutils.ConvertUtils)
在进行与数据库的交互过程中,由数据库查询到的数据放在 map 中,由 map 到 JavaBean 的过程中可以使用 BeanUtils.populate(map,bean)来进行转换 这里要处理的问 ...
- Apache Commons Beanutils 三 (BeanUtils、ConvertUtils、CollectionUtils...)
前言 前面已经学习了Apache Commons Beanutils包里的PropertyUtils和动态bean,接下来将学习剩下的几个工具类,个人觉得还是非常实用的,特别是CollectionUt ...
- Apache Commons Beanutils 二 (动态Bean - DynaBeans)
相关背景 上一篇介绍了PropertyUtils的用法,PropertyUtils主要是在不修改bean结构的前提下,动态访问bean的属性: 但是有时候,我们会经常希望能够在不定义一个Java类的前 ...
- Apache Commons Beanutils 一 (使用PropertyUtils访问Bean属性)
BeanUtils简要描述 beanutils,顾名思义,是java bean的一个工具类,可以帮助我们方便的读取(get)和设置(set)bean属性值.动态定义和访问bean属性: 细心的话,会发 ...
- org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法区别
知识点 org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作 ...
随机推荐
- ssm整合-图片上传功能(转)
本文介绍 ssm (Spring+SpringMVC+Mybatis)实现上传功能. 以一个添加用户的案例介绍(主要是将上传文件). 一.需求介绍 我们要实现添加用户的时候上传图片(其实任何文件都可以 ...
- Laravel系列 Web
一.Homestead准备 上一篇文章已经对它的配置进行了说明,下面对Homestead.yaml进行说明 --- ip: "192.168.10.10" memory: 2048 ...
- 学习python第十三天,函数5 装饰器decorator
定义:装饰器本质是函数,(装饰其他函数)就是为其他函数添加附加功能原则:1.不能修改被装饰的函数的源代码 2.不能修改装饰的函数的调用方式 实现装饰器知识储备1函数即变量2.高阶函数,满足2个条件之一 ...
- 进程、线程、协程和GIL(二)
上一篇博客讲了进程.线程.协程和GIL的基本概念,这篇我们来说说在以下三点: 1> python中使用threading库来创建线程的两种方式 2> 使用Event对消来判断线程是否已启动 ...
- Dawson City【道森市】
Dawson City Cities usually have a good reason for being where they are, like a nearby port or river. ...
- 各种Nand的总结
1. 微观 NAND闪存NAND是非易失性存储技术,NAND闪存由多个存放以位(bit)为单位的单元构成,这些位通过电荷被打开或关闭,如何组织这些开关单元来储存在SSD上的数据,也决定了NAND闪存的 ...
- SPOJ1026 概率DP
Favorite Dice BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorit ...
- requests--etree--xpath
# -*- coding: cp936 -*- import requests from lxml import etree url = 'https://weibo.cn/pub/' html = ...
- P1182 数列分段Section II
P1182 数列分段Section II 题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 ...
- FastJson 打Release 包解析失败
debug 的时候,fastJson 解析数据正常.但是打了release 的时候,解析的List 总是null. 找了半天,发现,是fastJson 是对泛型有问题. 解决办法: -keepattr ...