org.springframework.beans.Beanutils copyProperties 原理
来源:互联网 发布:程序员离职交接文档 编辑:程序博客网 时间:2024/05/21 21:02
org.springframework.beans.Beanutils copyProperties 原理
BeanUtils. copyProperties(source,targe)原理:
- 根据source的属性来向target同名属性设值。
- 若target无该属性,则不设
- 名字相同,基本类型和封装类型可以完成映射
- 若target有source无,则target属性值为null(基本类型则为初始值,如int 为 0)
- 调用原理 target.set + source的属性名(source.get + source的属性名):所有source必须有get方法,target必须有set方法
下面是例子
import org.springframework.beans.BeanUtils;
public class BeanUtilsTest {
public static void main(String[] args) {
Source source = new Source() {{
setId(1111111);
}};
Target target = new Target();
BeanUtils.copyProperties(source,target);
System.out.println(source);
System.out.println(target);
}
}
class Source {
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Cat{" +
"id=" + id +
'}';
}
}
class Target {
// id换名 则无法赋值
Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
'}';
}
}
0 1
- org.springframework.beans.Beanutils copyProperties 原理
- org.springframework.beans.Beanutils.copyProperties 原理
- org.springframework.beans.BeanUtils.copyProperties方法的泛型问题。
- org.springframework.beans.BeanUtils
- 重写org.springframework.beans.BeanUtils的copyProperties(Object source,Object target)方法 从model复制属性到pojo中
- 【exception】java.lang.NoSuchMethodError: org.springframework.beans.Beans.BeanUtils
- org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的区别
- java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass
- org.springframework.beans.TypeMismatchException:
- org.springframework.beans.NotWritablePropertyException
- org.springframework.beans.BeanInstantiationException
- org.springframework.beans.ConversionNotSupportedException:
- org.springframework.beans.NotWritablePropertyException
- org.springframework.beans.NotWritablePropertyException
- org.springframework.beans.ConversionNotSupportedException
- org.springframework.beans.NotWritablePropertyException
- org.springframework.beans.NotWritablePropertyException
- org.springframework.beans.NotWritablePropertyException
- 专题一 Problem Q
- [bzoj3720]Gty的妹子树
- 圆角效果实现,圆角效果对性能的影响及优化
- 采用C++的ACE库实现的一个通用的C/S架构通信程序(最终版)
- 编码规范
- org.springframework.beans.Beanutils copyProperties 原理
- H5 - canvas
- 解决tableview下滑隐藏HeaderInSection的问题
- x509和pkcs12以及pkcs7的关系和区别
- 树形选择框(jsp页面样板)
- 各种基本算法实现小结(五)—— 排序算法
- 如何选择 compileSdkVersion, minSdkVersion 和 targetSdkVersion
- 理解Fragment生命周期
- Servlet设置缓存