BeanUtilsの罠
2614 ワード
BeanUtilsの美名が広まったため、学芸が不精だった.プロジェクトではもちろんBeanUtilsを直接使いたい.結局華麗に罠に落ちた.次のシーンを見てみましょう:1、copyクラスを待ちます
2、copy類
3、copy
4、結果:SiteSupplierTrade[longAttr=0,StringArrayAttr={},integerAttr=0,StringAttr=]SiteSupplierTradeCopy[longAttr=0,StringArrayAttr={},integerAttr=0,StringAttr=]
SiteSupplierTrade[longAttr=,StringArrayAttr=,integerAttr=,StringAttr=]
SiteSupplierTradeCopy[longAttr=,StringArrayAttr=,integerAttr=,StringAttr=]
5、結論:1)、BeanUtils PropertyUtilsをよく知っている人は、この2人の兄弟の違いはBeanUtilsがタイプ変換をすることであり、PropertyUtilsはできず、自然前者の性能は後者に及ばない.2)、実はもう一つの副作用があり、結果が赤くなった部分を観察すると、BeanUtilsはlong、integerを0に初期化し、string[]はcopyのnullではなく空の配列に初期化することが分かった.3)、空の判断に頼る応用にとって悲劇である.
当駅はpay for your wishesをサポートする
public class SiteSupplierTrade {
private Long longAttr;
private String[] StringArrayAttr;
private Integer integerAttr;
private String StringAttr;
//set get
}
2、copy類
public class SiteSupplierTradeCopy {
private Long longAttr;
private String[] StringArrayAttr;
private Integer integerAttr;
private String StringAttr;
// set get
}
3、copy
public static void main(String[] args) {
SiteSupplierTrade siteSupplierTrade = new SiteSupplierTrade();
if (null != siteSupplierTrade) {
SiteSupplierTrade siteSupplierTrade2 = new SiteSupplierTrade();
SiteSupplierTradeCopy SiteSupplierTrade3 = new SiteSupplierTradeCopy();
try {
BeanUtils.copyProperties(siteSupplierTrade2, siteSupplierTrade);
BeanUtils.copyProperties(SiteSupplierTrade3, siteSupplierTrade);
} catch (Exception e) {
}
System.out.println(siteSupplierTrade2);
System.out.println(SiteSupplierTrade3);
try {
PropertyUtils.copyProperties(siteSupplierTrade2, siteSupplierTrade);
PropertyUtils.copyProperties(SiteSupplierTrade3, siteSupplierTrade);
} catch (Exception e) {
}
System.out.println(siteSupplierTrade2);
System.out.println(SiteSupplierTrade3);
}
}
4、結果:SiteSupplierTrade[longAttr=0,StringArrayAttr={},integerAttr=0,StringAttr=
SiteSupplierTrade[longAttr=
SiteSupplierTradeCopy[longAttr=
5、結論:1)、BeanUtils PropertyUtilsをよく知っている人は、この2人の兄弟の違いはBeanUtilsがタイプ変換をすることであり、PropertyUtilsはできず、自然前者の性能は後者に及ばない.2)、実はもう一つの副作用があり、結果が赤くなった部分を観察すると、BeanUtilsはlong、integerを0に初期化し、string[]はcopyのnullではなく空の配列に初期化することが分かった.3)、空の判断に頼る応用にとって悲劇である.
当駅はpay for your wishesをサポートする