其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个javabean之间的相互拷贝(两个javabean之间相同的字段才可以赋值)
导入的jar包
import org.springframework.beans.BeanUtils;
test1 a = new test1();
test2 b = new test2();
//a为有值的对象,b为将要赋值的对象
BeanUtils.copyProperties(a, b);