查看网站用什么语言做的,建站公司最喜欢的网站,教育加盟培训网站建设,wordpress添加主栏目1. 背景
之前在专栏中讲过“不推荐使用属性拷贝工具”#xff0c;推荐直接定义转换类和方法使用 IDEA 插件自动填充 get / set 函数。
不推荐的主要理由是#xff1a;
有些属性拷贝工具性能有点差有些属性拷贝工具有“BUG”使用属性拷贝工具容易存在一些隐患#xff08…1. 背景
之前在专栏中讲过“不推荐使用属性拷贝工具”推荐直接定义转换类和方法使用 IDEA 插件自动填充 get / set 函数。
不推荐的主要理由是
有些属性拷贝工具性能有点差有些属性拷贝工具有“BUG”使用属性拷贝工具容易存在一些隐患后面例子会讲到
2. 示例
首先公司内部就遇到过 commons 包的 BeanUtils 进行属性拷贝性能较差的真实案例然后该同事换成了 Spring 的 BeanUtils 性能好了很多感兴趣大家可以使用性能测试框架或者基准测试框架去对比这里就不对比了。
接下来我们看 Spring 的 BeanUtils 的属性拷贝会存在啥问题
import lombok.Data;import java.util.List;Data
public class A {private String name;private ListInteger ids;
}
Data
public class B {private String name;private ListString ids;
}
import org.springframework.beans.BeanUtils;import java.util.Arrays;public class BeanUtilDemo {public static void main(String[] args) {A first new A();first.setName(demo);first.setIds(Arrays.asList(1, 2, 3));B second new B();BeanUtils.copyProperties(first, second);for (String each : second.getIds()) {// 类型转换异常System.out.println(each);}}
}
大家运行上述示例时会发生类型转换异常。
打断点可以看到属性拷贝之后 B 类型的 second 对象中 ids 仍然为 Integer 类型 如果不转换为字符串直接进行打印并不会报错。 使用CGlib 在不定义Converter 的情况下也会遇到类似问题
import org.easymock.cglib.beans.BeanCopier;import java.util.Arrays;public class BeanUtilDemo {public static void main(String[] args) {A first new A();first.setName(demo);first.setIds(Arrays.asList(1, 2, 3));B second new B();final BeanCopier beanCopier BeanCopier.create(A.class, B.class, false);beanCopier.copy(first,second,null);for (String each : second.getIds()) {// 类型转换异常System.out.println(each);}}
}
同样问题在运行时才暴露出来。 接下来我们看下 mapstruct
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;Mapper
public interface Converter {Converter INSTANCE Mappers.getMapper(Converter.class);B aToB(A car);
}
import java.util.Arrays;public class BeanUtilDemo {public static void main(String[] args) {A first new A();first.setName(demo);first.setIds(Arrays.asList(1, 2, 3));B second Converter.INSTANCE.aToB(first);for (String each : second.getIds()) {// 正常System.out.println(each);}}
}
可以成功的将 A 中 ListInteger 转为 B 中的 ListString 类型。
我们看下编译生成的 Converter 实现类
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;Generated(value org.mapstruct.ap.MappingProcessor,comments version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_202 (Oracle Corporation)
)
Component
public class ConverterImpl implements Converter {Overridepublic B aToB(A car) {if ( car null ) {return null;}B b new B();b.setName( car.getName() );b.setIds( integerListToStringList( car.getIds() ) );return b;}protected ListString integerListToStringList(ListInteger list) {if ( list null ) {return null;}ListString list1 new ArrayListString( list.size() );for ( Integer integer : list ) {list1.add( String.valueOf( integer ) );}return list1;}
}
自动帮我们进行了转换我们可能没有意识到类型并不一致。
如果我们在 A 类中添加一个 String number 属性在 B 类中添加一个 Long number 属性使用 mapstruect 当 number 设置为非数字类型时就会报 .NumberFormatException 。
Overridepublic B aToB(A car) {if ( car null ) {return null;}B b new B();b.setName( car.getName() );if ( car.getNumber() ! null ) { // 问题出在这里b.setNumber( Long.parseLong( car.getNumber() ) );}b.setIds( integerListToStringList( car.getIds() ) );return b;}
使用 cglib 默认则不会映射 number 属性B 中的 number 为 null。 如果手动定义转换器使用 IDEA 插件(如 generateO2O)自动转换
public final class A2BConverter {public static B from(A first) {B b new B();b.setName(first.getName());b.setIds(first.getIds());return b;}
}
在编码阶段就可以非常明确地发现这个问题 3. 结论
由于 Java 的泛型其实是编译期检查编译后泛型擦除导致运行时 ListInteger 和 ListString 都是 List 类型可以正常赋值。这就导致在使用很多属性映射工具时编译时不容易明显的错误。
mapstruct 自定义了注解处理器在编译阶段可以读取映射双方的泛型类型进而进行映射。但是这种映射也很可怕有时候我们由于粗心等原因定义错了类型自动帮助我们进行了转换会带了很多副作用。
之前对各种属性映射工具的性能进行了简单的对比结果如下 因此慎用属性转换工具如果可能建议自定义转换类使用 IDEA插件自动填充效率也挺高 A 或 B 中任何属性类型不匹配甚至删除一个属性编译阶段即可报错而且直接调用 get set 的效率也是非常高的。
文章转载自: http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.yppln.cn.gov.cn.yppln.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.pwqyd.cn.gov.cn.pwqyd.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.mzhgf.cn.gov.cn.mzhgf.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.rqgjr.cn.gov.cn.rqgjr.cn http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.jrlgz.cn.gov.cn.jrlgz.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.htbbp.cn.gov.cn.htbbp.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn