Apache Commons BeanUtils 简单入门
Apache Commons BeanUtils 简单入门
?
主要功能: 操作Bean的属性,针对Bean属性排序,Bean和Map的转换,创建动态的Bean等
?
?
Person person = new Person();person.setPid(100);person.setPname("zhangsan");Card card = new Card();card.setCid(1000);card.setCnumber("430221193332422135");//根据反射获取属性值;int pidValue = (Integer) PropertyUtils.getSimpleProperty(person,"pid"); //100String pNameValue = (String) PropertyUtils.getSimpleProperty(person, "pname"); //zhangsan//获取嵌套bean属性值;Integer cid =(Integer) PropertyUtils.getNestedProperty(person, "card.cid"); //1000 String number =(String) PropertyUtils.getNestedProperty(person, "card.cnumber");//430221193332422135//根据反射获取数组,arrIndex代表bean里面定义的数组;String arr = (String) PropertyUtils.getIndexedProperty(person, "arrIndex[0]"); //0//获取属性类型;Class<?> class1 = PropertyUtils.getPropertyType(person, "pname"); //java.lang.String//获取map集合的值;map(name),map是bean里面的集合名词,name是map集合中的key;String value = (String) PropertyUtils.getMappedProperty(person, "map(name)");//把bean里面所有的属性,包括数组,集合、bean等等;Map personMap = PropertyUtils.describe(person);//opyProperty()复制Bean属性,只复制引用,final类型和原始类型(primitive type)PropertyUtils.copyProperties(person2, person1);
?