在这篇文章中,我们将为您详细介绍从BeanUtils的一个坑谈起的内容,并且讨论关于beanutils获取数据的相关问题。此外,我们还会涉及一些关于Apache的BeanUtils和PropertyU
在这篇文章中,我们将为您详细介绍从BeanUtils的一个坑谈起的内容,并且讨论关于beanutils获取数据的相关问题。此外,我们还会涉及一些关于Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg、Apache:BeanUtils 和 PropertyUtils 的区别、Bean 复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier、beanutils的知识,以帮助您更全面地了解这个主题。
本文目录一览:- 从BeanUtils的一个坑谈起(beanutils获取数据)
- Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg
- Apache:BeanUtils 和 PropertyUtils 的区别
- Bean 复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier
- beanutils
从BeanUtils的一个坑谈起(beanutils获取数据)
BeanUtils可以将参数封装成一个bean,方法 setProperty,可以对属性进行赋值。但是尤其是要注意的是它会对属性设置默认值,而且默认值会有歧义。
比如说Bean TestBean 的某个属性的类型是Integer,
TestBean testBean = new TestBean();
try {
BeanUtils.setProperty(testBean, "testProperty", null)
} catch (Exception ex) {..... }
System.out.println("testProperty的值为:"+testBean.getTestPropety);
其结果将会输入为0,这无疑是有坑的。
与这个作用类似的Spring MVC有一个BaseCommandController也是可以将请求参数封装成Bean,但是null
的值不会转换为默认值。使用的时候切记。
如果BeanUtils也想不转换为类型(如 Integer)的默认值(如0),可以使用如下:
try {
ConvertUtils.register(new IntegerConverter(null), Integer.class);
BeanUtils.setProperty(testBean,"testProperty",null)
} catch(Exception ex){
......
}
或者
PropertyUtils.getWriteMethod(PropertyUtils.getPropertyDescriptor(this, propertyName)).invoke(this, new Object[]{null});
Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg
作为一个新员工,一个首要的工作就是阅读别人的代码,阅读代码的诸多好处就不说了,我就直奔主题,通过预读代码,发现了几种实现两个不同类型的Bean之间实现值复制的几种方式,上网查询后发现性能上会有差异,于是就萌生自己深入了解几种实现的想法。第一步就是先本着实事求是的原则去探求一下大家总结出来的性能差异是否正确。
比较的是四种复制的方式,分别为Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cglib的BeanCopier。做法是在Eclipse新建了一个Project,专门用于专门测试几种代码的性能。具体的代码如下:
一个FromBean和一个ToBean,两个的代码基本上一样,除了类名称不一样,所以只是贴出来了一份。
public class FromBean {
private String name;
private int age;
private String address;
private String idno;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getIdno() {
return idno;
}
public void setIdno(String idno) {
this.idno = idno;
}
}
一个用于测试的BenchmarkTest类,为了减少重复代码,写了一个策略模式
public class BenchmarkTest {
private int count;
public BenchmarkTest(int count) {
this.count = count;
System.out.println("性能测试" + this.count + "==================");
}
public void benchmark(IMethodCallBack m, FromBean frombean) {
try {
long begin = new java.util.Date().getTime();
ToBean tobean = null;
System.out.println(m.getMethodName() + "开始进行测试");
for (int i = 0; i < count; i++) {
tobean = m.callMethod(frombean);
}
long end = new java.util.Date().getTime();
System.out.println(m.getMethodName() + "耗时" + (end - begin));
System.out.println(tobean.getAddress());
System.out.println(tobean.getAge());
System.out.println(tobean.getIdno());
System.out.println(tobean.getMoney());
System.out.println(tobean.getName());
System.out.println(" ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
策略中使用的接口声明
public interface IMethodCallBack {
String getMethodName();
ToBean callMethod(FromBean frombean) throws Exception;
}
使用的测试类
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
FromBean fb = new FromBean();
fb.setAddress("北京市朝阳区大屯路");
fb.setAge(20);
fb.setMoney(30000.111);
fb.setIdno("110330219879208733");
fb.setName("测试");
IMethodCallBack beanutilCB = new IMethodCallBack() {
@Override
public String getMethodName() {
return "BeanUtil.copyProperties";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
BeanUtils.copyProperties(toBean, frombean);
return toBean;
}
};
IMethodCallBack propertyCB = new IMethodCallBack() {
@Override
public String getMethodName() {
return "PropertyUtils.copyProperties";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
PropertyUtils.copyProperties(toBean, frombean);
return toBean;
}
};
IMethodCallBack springCB = new IMethodCallBack() {
@Override
public String getMethodName() {
return "org.springframework.beans.BeanUtils.copyProperties";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
org.springframework.beans.BeanUtils.copyProperties(frombean, toBean);
return toBean;
}
};
IMethodCallBack cglibCB = new IMethodCallBack() {
BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class, false);
@Override
public String getMethodName() {
return "BeanCopier.create";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
bc.copy(frombean, toBean, null);
return toBean;
}
};
// 数量较少的时候,测试性能
BenchmarkTest bt = new BenchmarkTest(10);
bt.benchmark(beanutilCB, fb);
bt.benchmark(propertyCB, fb);
bt.benchmark(springCB, fb);
bt.benchmark(cglibCB, fb);
// 测试一万次性能测试
BenchmarkTest bt10000 = new BenchmarkTest(10000);
bt10000.benchmark(beanutilCB, fb);
bt10000.benchmark(propertyCB, fb);
bt10000.benchmark(springCB, fb);
bt10000.benchmark(cglibCB, fb);
// 担心因为顺序问题影响测试结果
BenchmarkTest bt1000R = new BenchmarkTest(10000);
bt1000R.benchmark(cglibCB, fb);
bt1000R.benchmark(springCB, fb);
bt1000R.benchmark(propertyCB, fb);
bt1000R.benchmark(beanutilCB, fb);
}
}
进行了三次测试,最后的结果如下:
10次测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 54 | 57 | 50 | 53.66667 | 5.366666667 |
PropertyUtils.copyProperties | 4 | 4 | 4 | 4 | 0.4 |
org.springframework.beans.BeanUtils.copyProperties | 12 | 10 | 11 | 11 | 1.1 |
BeanCopier.create | 0 | 0 | 0 | 0 | 0 |
10000次测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 241 | 222 | 226 | 229.6667 | 0.022966667 |
PropertyUtils.copyProperties | 92 | 90 | 92 | 91.33333 | 0.009133333 |
org.springframework.beans.BeanUtils.copyProperties | 29 | 30 | 32 | 30.33333 | 0.003033333 |
BeanCopier.create | 1 | 1 | 1 | 1 | 0.1 |
10000次反转测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 178 | 174 | 178 | 176.6667 | 0.017666667 |
PropertyUtils.copyProperties | 91 | 87 | 89 | 89 | 0.0089 |
org.springframework.beans.BeanUtils.copyProperties | 21 | 21 | 21 | 21 | 0.0021 |
BeanCopier.create | 0 | 1 | 1 | 0.666667 | 6.66667E-05 |
不过需要注意的是,Cglib在测试的时候,先进行了实例的缓存,这个也是他性能较好的原因之一。如果把缓存去掉的话,性能就会出现了一些的差异,但是整体的性能还是很好,不过奇怪的是10000次反而比10次少,而且后面的反转1万次反而耗时最少,进行多次测试效果也是如此。 从整体的表现来看,Cglib的BeanCopier的性能是最好的无论是数量较大的1万次的测试,还是数量较少10次,几乎都是趋近与零损耗,Spring是在次数增多的情况下,性能较好,在数据较少的时候,性能比PropertyUtils的性能差一些。PropertyUtils的性能相对稳定,表现是呈现线性增长的趋势。而Apache的BeanUtil的性能最差,无论是单次Copy还是大数量的多次Copy性能都不是很好。
10次 | 10000次 | 10000次反转 | |
BeanCopier.create | 41 | 28 | 10 |
性能测试就到这里,数据也展示如上,后续会继续编写剩余两篇文章,这一片关注性能,后面的一篇是就每种方式的使用上的差异进行详解,最后一篇想进行探讨是什么早就了这四种方式的性能差异
Apache:BeanUtils 和 PropertyUtils 的区别
1.BeanUtils 它以字符串的形式对 javabean 进行转换,而 PropertyUtils 是以原本的类型对 javabean 进行操作,如果类型不对,就会有 argument type mismatch 异常。
2.PropertyUtils.getPropety 方法获得的属性值的类型为该属性本来的类型,而 BeanUtils.getProperty 则是将该属性的值转换成字符串后才返回。
3.BeanUtils 不允许对象的属性值为 null (基本类型对应的包装类可),PropertyUtils 可以拷贝属性值 null 的对象。
4. 如果对象属性值为 null,BeanUtils.copyProperties 方法会报 commons.beanutils.ConversionException: No value specified 错误;
5. 但当 Ineger、Boolean、Long 等基本类型包装类为 null 时,会将值复制后转换成 0 或 false。
Beanutils 提供类型转换功能,在支持的数据类型范围内进行转换。
另外:
BeanUtils.copyProperty () 方法,是通过属性对应的 setXxx () 方法来进行注入的。也就是说,可以在 setXxx () 方法处进行数据的校验。
Bean 复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier


使用的测试类

public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
FromBean fb = new FromBean();
fb.setAddress("北京市朝阳区大屯路");
fb.setAge(20);
fb.setMoney(30000.111);
fb.setIdno("110330219879208733");
fb.setName("测试");
IMethodCallBack beanutilCB = new IMethodCallBack() {
@Override
public String getMethodName() {
return "BeanUtil.copyProperties";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
BeanUtils.copyProperties(toBean, frombean);
return toBean;
}
};
IMethodCallBack propertyCB = new IMethodCallBack() {
@Override
public String getMethodName() {
return "PropertyUtils.copyProperties";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
PropertyUtils.copyProperties(toBean, frombean);
return toBean;
}
};
IMethodCallBack springCB = new IMethodCallBack() {
@Override
public String getMethodName() {
return "org.springframework.beans.BeanUtils.copyProperties";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
org.springframework.beans.BeanUtils.copyProperties(frombean,
toBean);
return toBean;
}
};
IMethodCallBack cglibCB = new IMethodCallBack() {
BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class,
false);
@Override
public String getMethodName() {
return "BeanCopier.create";
}
@Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
bc.copy(frombean, toBean, null);
return toBean;
}
};
// 数量较少的时候,测试性能
BenchmarkTest bt = new BenchmarkTest(10);
bt.benchmark(beanutilCB, fb);
bt.benchmark(propertyCB, fb);
bt.benchmark(springCB, fb);
bt.benchmark(cglibCB, fb);
// 测试一万次性能测试
BenchmarkTest bt10000 = new BenchmarkTest(10000);
bt10000.benchmark(beanutilCB, fb);
bt10000.benchmark(propertyCB, fb);
bt10000.benchmark(springCB, fb);
bt10000.benchmark(cglibCB, fb);
// 担心因为顺序问题影响测试结果
BenchmarkTest bt1000R = new BenchmarkTest(10000);
bt1000R.benchmark(cglibCB, fb);
bt1000R.benchmark(springCB, fb);
bt1000R.benchmark(propertyCB, fb);
bt1000R.benchmark(beanutilCB, fb);
}
}

进行了三次测试,最后的结果如下:
10 次测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 54 | 57 | 50 | 53.66667 | 5.366666667 |
PropertyUtils.copyProperties | 4 | 4 | 4 | 4 | 0.4 |
org.springframework.beans.BeanUtils.copyProperties | 12 | 10 | 11 | 11 | 1.1 |
BeanCopier.create | 0 | 0 | 0 | 0 | 0 |
10000 次测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 241 | 222 | 226 | 229.6667 | 0.022966667 |
PropertyUtils.copyProperties | 92 | 90 | 92 | 91.33333 | 0.009133333 |
org.springframework.beans.BeanUtils.copyProperties | 29 | 30 | 32 | 30.33333 | 0.003033333 |
BeanCopier.create | 1 | 1 | 1 | 1 | 0.1 |
10000 次反转测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 178 | 174 | 178 | 176.6667 | 0.017666667 |
PropertyUtils.copyProperties | 91 | 87 | 89 | 89 | 0.0089 |
org.springframework.beans.BeanUtils.copyProperties | 21 | 21 | 21 | 21 | 0.0021 |
BeanCopier.create | 0 | 1 | 1 | 0.666667 | 6.66667E-05 |
不过需要注意的是,Cglib 在测试的时候,先进行了实例的缓存,这个也是他性能较好的原因之一。如果把缓存去掉的话,性能就会出现了一些的差异,但是整体的性能还是很好,不过奇怪的是 10000 次反而比 10 次少,而且后面的反转 1 万次反而耗时最少,进行多次测试效果也是如此。 从整体的表现来看,Cglib 的 BeanCopier 的性能是最好的无论是数量较大的 1 万次的测试,还是数量较少 10 次,几乎都是趋近与零损耗,Spring 是在次数增多的情况下,性能较好,在数据较少的时候,性能比 PropertyUtils 的性能差一些。PropertyUtils 的性能相对稳定,表现是呈现线性增长的趋势。而 Apache 的 BeanUtil 的性能最差,无论是单次 Copy 还是大数量的多次 Copy 性能都不是很好。
10 次 | 10000 次 | 10000 次反转 | |
BeanCopier.create | 41 | 28 | 10 |
性能测试就到这里,数据也展示如上,后续会继续编写剩余两篇文章,这一片关注性能,后面的一篇是就每种方式的使用上的差异进行详解,最后一篇想进行探讨是什么早就了这四种方式的性能差异。
http://www.cnblogs.com/kaka/archive/2013/03/06/2945514.html
beanutils
package fff;
import org.apache.commons.beanutils.BeanUtils;
class position{
public position(int x, int y) {
// super();
this.x = x;
this.y = y;
}
public int x;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int y;
}
public class bean {
public static void main (String[] args) throws Exception{
position p = new position(3,4);
System.out.println(p.getX());
BeanUtils.getProperty(p, "x");
System.out.println(BeanUtils.getProperty(p, "x"));
BeanUtils.setProperty(p, "x", "6");
System.out.println(p.getX());
}
}
关于从BeanUtils的一个坑谈起和beanutils获取数据的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg、Apache:BeanUtils 和 PropertyUtils 的区别、Bean 复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier、beanutils等相关知识的信息别忘了在本站进行查找喔。
本文标签: