优化Spring的BeanUtils,让代码更加优雅
背景:Spring的BeanUtils比较常用,但是有一个缺点就是需要两行代码
TestEntity source = new TestEntity();
TestEntity target = new TestEntity();
BeanUtils.copyProperties(source, target);
//...
因此写一个简单的工具减少代码行数
public class SpringBeanUtils {
public static <T> T copyProperties(Object source, T target) throws BeansException{
BeanUtils.copyProperties(source, target);
return target;
}
调用
TestEntity source = new TestEntity();
TestEntity target = BeanUtils.copyProperties(source, new TestEntity());
//...
文件打包: https://gitee.com/bignpc/tool
如果觉得还行,文章点个赞,谢谢🕊️🕊️🕊️