apache commons beanutils
一、引入Maven依赖
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency>
二、常用API说明
以下使用到一些BO:
Person.java
public class Person { private String username; private int age; private float stature;//身高 private boolean sex;//性别 private List list = new ArrayList(); private String[] friendsNames; private Map<String, String> maps = new HashMap<String, String>(); private Address address; public Person() { } public Person(String username) { this.username = username; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getStature() { return stature; } public void setStature(float stature) { this.stature = stature; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } @SuppressWarnings("unchecked") public List getList() { return list; } @SuppressWarnings("unchecked") public void setList(List list) { this.list = list; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public String[] getFriendsNames() { return friendsNames; } public void setFriendsNames(String[] friendsNames) { this.friendsNames = friendsNames; } @Override public String toString() { return "Person{" + "username='" + username + '\'' + ", age=" + age + ", address=" + address + '}'; } }
Address.java
public class Address { private String email; private List<String> telephone; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public List<String> getTelephone() { return telephone; } public void setTelephone(List<String> telephone) { this.telephone = telephone; } @Override public String toString() { return "Address{" + "email='" + email + '\'' + ", telephone=" + telephone + '}'; } }
常用的API如下:
import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.BeanMap; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConstructorUtils; import org.apache.commons.beanutils.MethodUtils; import org.apache.commons.beanutils.PropertyUtils; /** * org.apache.commons.beanutils 提供了对于JavaBean进行各种操作,克隆对象,属性等等。 * 常用工具类: * ① PropertyUtils 主要目的是利用反射机制对JavaBean的属性进行处理,减少对JavaBean的处理导致大量get/set代码堆积 * ② BeanUtils 属性设置、复制等 * ③ BeanMap 将Bean转为BeanMap * ④ MethodUtils 与方法相关的反射工具类 * ⑤ ConstructorUtils 与构造器相关的反射工具类 */ public class BeanUtilsDemo { private static void testMethodUtils(){ try { Person person = new Person(); //获取set方法 Method setUsernameMethod = MethodUtils.getAccessibleMethod(Person.class, "setUsername", String.class); // 调用person的setUsername方法 setUsernameMethod.invoke(person, "张三"); //获取get方法 Method getUsernameMethod = MethodUtils.getAccessibleMethod(Person.class, "getUsername", new Class[]{}); Object invoke = getUsernameMethod.invoke(person); System.out.println(invoke); // 直接调用方法 // exact 是"精确的"。 // invokeExact方法在调用时要求严格的类型匹配,方法的返回值类型也在考虑范围之内 // MethodUtils.invokeExactMethod(); // MethodUtils.invokeExactStaticMethod(); // MethodUtils.invokeMethod(); // MethodUtils.invokeStaticMethod(); Object invokeObj = MethodUtils.invokeMethod(person, "getUsername", new Class[]{}); System.out.println(invokeObj); }catch (Exception e){ System.out.println("MethodUtils Error"); e.printStackTrace(); } } private static void testConstructorUtils(){ try { // 这个类中的方法主要分成两种,一种是得到构造方法,一种是创建对象。事实上多数时候得到构造方法的目的就是创建对象 // 直接创建对象 // 调用无参构造器,参数给个空数组即可; 调用有参构造器,则需要按顺序列出参数值 Person person = ConstructorUtils.invokeConstructor(Person.class, new Object[]{}); // Person person = ConstructorUtils.invokeConstructor(Person.class, new Object[]{"李四"}); System.out.println(person); // 获取构造器后,再创建对象 Constructor accessibleConstructor = ConstructorUtils.getAccessibleConstructor(Person.class, new Class[]{String.class}); if(null != accessibleConstructor){ Person newInstance = (Person) accessibleConstructor.newInstance("李四"); System.out.println(newInstance.getClass()); System.out.println(newInstance); } }catch (Exception e){ System.out.println("ConstructorUtils Error"); e.printStackTrace(); } } private static void testBeanMap(){ try { Person person = new Person(); person.setUsername("李四"); person.setAge(22); person.setStature(173.5f); person.setSex(false); Address address = new Address(); address.setEmail("jhlishero@163.com"); person.setAddress(address); // 将Bean转为Map BeanMap beanMap = new BeanMap(person); System.out.println(beanMap.get("username")); // 克隆, 深拷贝 BeanMap copyBeanMap = (BeanMap)beanMap.clone(); System.out.println(copyBeanMap.get("address")); }catch (Exception e){ System.out.println("BeanMap Error"); e.printStackTrace(); } } private static void testBeanUtils(){ Person person = new Person(); try { //simple property BeanUtils.setProperty(person, "username", "李四"); BeanUtils.setProperty(person, "age", 22); BeanUtils.setProperty(person, "stature", 173.5f); BeanUtils.setProperty(person, "sex", new Boolean(false)); //index property //List List list = new ArrayList(); list.add("list value 0"); list.add("list value 1"); BeanUtils.setProperty(person, "list", list); //将list设置到person之后,可以对里面的值进行修改 String listValue2 = "new list value 1"; BeanUtils.setProperty(person, "list[1]", listValue2); //数组 String[] str = {"张三", "王五", "赵钱"}; person.setFriendsNames(str); BeanUtils.setProperty(person, "friendsNames[2]", "new赵钱"); //nest property Address address = new Address(); address.setEmail("jhlishero@163.com"); List<String> telephoneList = new ArrayList<String>(); telephoneList.add("12345678911"); telephoneList.add("92345678911"); address.setTelephone(telephoneList); person.setAddress(address); BeanUtils.setProperty(person, "address.telephone[1]", "nest 11111111"); BeanUtils.setProperty(person, "address.email", "new_jhlishero@163.com"); System.out.println(BeanUtils.getSimpleProperty(person, "username")); System.out.println(BeanUtils.getSimpleProperty(person, "age")); System.out.println(BeanUtils.getSimpleProperty(person, "stature")); System.out.println(BeanUtils.getSimpleProperty(person, "sex")); System.out.println(BeanUtils.getSimpleProperty(person, "list")); //list System.err.println(BeanUtils.getIndexedProperty(person, "list[0]")); System.err.println(BeanUtils.getIndexedProperty(person, "list", 1)); //数组 System.out.println(BeanUtils.getIndexedProperty(person, "friendsNames[0]")); System.out.println(BeanUtils.getIndexedProperty(person, "friendsNames", 1)); System.out.println(BeanUtils.getIndexedProperty(person, "friendsNames[2]")); //nest--嵌套输出 System.out.println(BeanUtils.getNestedProperty(person, "address.email")); System.out.println(BeanUtils.getNestedProperty(person, "address.telephone[0]")); System.out.println(BeanUtils.getNestedProperty(person, "address.telephone[1]")); //也可以使用如下方法获取值 System.out.println(BeanUtils.getProperty(person, "address.telephone[1]")); //复制属性 // 浅拷贝: 只是调用子对象的set方法,并没有将所有属性拷贝。(也就是说,引用的一个内存地址) // 深拷贝: 将子对象的属性也拷贝过去。 // org.springframework.beans.BeanUtils.copyProperties 是浅拷贝 // org.apache.commons.beanutils.BeanUtils是深拷贝 Person copyPerson = new Person(); BeanUtils.copyProperties(copyPerson, person); System.out.println(copyPerson.getAddress()); // 遍历map<key, value>中的key,如果bean中有这个属性,就把这个key对应的value值赋给bean的属性。 Person mpPerson = new Person(); Map<String, String> personMap = new HashMap(); personMap.put("username", "宋江"); personMap.put("sex", "true"); BeanUtils.populate(mpPerson, personMap); System.out.println(mpPerson.getUsername()); } catch (Exception e) { System.out.println("BeanUtils Error"); e.printStackTrace(); } } private static void testPropertyUtils(){ Person person = new Person(); try { //simple property PropertyUtils.setSimpleProperty(person, "username", "李四"); PropertyUtils.setSimpleProperty(person, "age", 22); PropertyUtils.setSimpleProperty(person, "stature", 173.5f); PropertyUtils.setSimpleProperty(person, "sex", new Boolean(false)); //index property //List List list = new ArrayList(); list.add("list value 0"); list.add("list value 1"); PropertyUtils.setSimpleProperty(person, "list", list); //将list设置到person之后,可以对里面的值进行修改 String listValue2 = "new list value 1"; PropertyUtils.setIndexedProperty(person, "list[1]", listValue2); //数组 String[] str = {"张三", "王五", "赵钱"}; person.setFriendsNames(str); PropertyUtils.setIndexedProperty(person, "friendsNames[2]", "new赵钱"); //Map Map<String, String> map = new HashMap(); map.put("key1", "vlaue1"); map.put("key2", "vlaue2"); map.put("key3", "vlaue3"); person.setMaps(map); PropertyUtils.setMappedProperty(person, "maps", "key1", "new value1"); PropertyUtils.setMappedProperty(person, "maps(key2)", "maps(key2) value"); //nest property Address address = new Address(); address.setEmail("jhlishero@163.com"); List<String> telephoneList = new ArrayList<String>(); telephoneList.add("12345678911"); telephoneList.add("92345678911"); address.setTelephone(telephoneList); person.setAddress(address); PropertyUtils.setNestedProperty(person, "address.telephone[1]", "nest 11111111"); PropertyUtils.setNestedProperty(person, "address.email", "new_jhlishero@163.com"); System.out.println(PropertyUtils.getSimpleProperty(person, "username")); System.out.println(PropertyUtils.getSimpleProperty(person, "age")); System.out.println(PropertyUtils.getSimpleProperty(person, "stature")); System.out.println(PropertyUtils.getSimpleProperty(person, "sex")); System.out.println(PropertyUtils.getSimpleProperty(person, "list")); //list System.err.println(PropertyUtils.getIndexedProperty(person, "list[0]")); System.err.println(PropertyUtils.getIndexedProperty(person, "list", 1)); //数组 System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[0]")); System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames", 1)); System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[2]")); //Map System.err.println(PropertyUtils.getMappedProperty(person, "maps(key1)")); System.err.println(PropertyUtils.getMappedProperty(person, "maps", "key2")); System.err.println(PropertyUtils.getMappedProperty(person, "maps(key3)")); //nest--嵌套输出 System.out.println(PropertyUtils.getNestedProperty(person, "address.email")); System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[0]")); System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[1]")); //也可以使用如下方法获取值 System.out.println(PropertyUtils.getProperty(person, "address.telephone[1]")); //复制属性 // 浅拷贝: 只是调用子对象的set方法,并没有将所有属性拷贝。(也就是说,引用的一个内存地址) // 深拷贝: 将子对象的属性也拷贝过去。 // org.apache.commons.beanutils.PropertyUtils Person copyPerson = new Person(); PropertyUtils.copyProperties(copyPerson, person); System.out.println(copyPerson.getList().size()); } catch (Exception e) { System.out.println("PropertyUtils Error"); e.printStackTrace(); } } public static void main(String[] args){ //BeanUtilsDemo.testPropertyUtils(); //BeanUtilsDemo.testBeanUtils(); //BeanUtilsDemo.testBeanMap(); //BeanUtilsDemo.testConstructorUtils(); BeanUtilsDemo.testMethodUtils(); } }
时刻与技术进步,每天一点滴,日久一大步!!!
本博客只为记录,用于学习,如有冒犯,请私信于我。