【简介】
- BeanUtils是一种方便我们对JavaBean进行操作的工具类,是Apache组织下的产品。
- 注意:在实际开发过程中应优先选择spring框架自带的BeanUtils,这样就可以避免过多的引入不必要的maven依赖(同理的还有spring框架自带的StringUtils)
【maven依赖】
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
【API介绍】
public static void copyProperties(Object dest, Object orig) {}
public void populate (Object bean, Map map)
public static Object cloneBean(Object bean) {}
public static Map<String, String> describe(Object bean){}
public static void setProperty(Object bean, String name, Object value) {}
public static String getProperty(Object bean, String name) {}
public static String[] getArrayProperty(Object bean, String name) {}
public static String getSimpleProperty(Object bean, String name) {}
public static String getIndexedProperty(Object bean, String name) {}
public static String getIndexedProperty(Object bean, String name, int index){}
public static String getMappedProperty(Object bean, String name) {}
public static String getMappedProperty(Object bean, String name, String key){}
测试代码
public class BeanUtilsDemo {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {
User user = new User();
user.setFather("zhang jack");
user.setMother("zhao jack");
BeanUtils.setProperty(user, "name", "Jack");
BeanUtils.setProperty(user, "id", 21);
String name = BeanUtils.getProperty(user, "name");
System.out.println("赋值后的对象:" + user);
User user1 = (User) BeanUtils.cloneBean(user);
System.out.println("cloneBean对象" + user1);
Map<String, String> describe = BeanUtils.describe(user);
Set<Map.Entry<String, String>> entries = describe.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
String key = next.getKey();
String value = next.getValue();
System.out.println(key + "=" + value);
}
describe.put("testKey", "testValue");
User user2 = new User();
BeanUtils.populate(user2, describe);
System.out.println("populate后的对象:" + user2);
User user3 = new User();
describe.remove("id");
BeanUtils.populate(user3, describe);
System.out.println(user3);
User user4 = new User();
BeanUtils.copyProperties(user4, user);
System.out.println(user4);
String[] names = BeanUtils.getArrayProperty(user, "name");
for (String item : names) {
System.out.println(item);
}
String[] subject = {"生物", "语文", "数学", "英语", "地理"};
user.setSubject(subject);
String[] subjects = BeanUtils.getArrayProperty(user, "subject");
for (String item : subjects) {
System.out.println(item);
}
String subjectName = BeanUtils.getSimpleProperty(user, "name");
System.out.println(subjectName);
subjectName = BeanUtils.getSimpleProperty(user, "subject");
System.out.println(subjectName);
subjectName = BeanUtils.getIndexedProperty(user, "subject[0]");
System.out.println(subjectName);
subjectName = BeanUtils.getIndexedProperty(user, "subject", 1);
System.out.println(subjectName);
Map<String, String> contactDetails = Maps.newHashMap();
contactDetails.put("1", "13550124568");
contactDetails.put("2", "13550124567");
contactDetails.put("3", "13550124566");
contactDetails.put("4", "13550124565");
user.setContactDetails(contactDetails);
String mappedProperty = BeanUtils.getMappedProperty(user, "contactDetails(2)");
System.out.println(mappedProperty);
mappedProperty = BeanUtils.getMappedProperty(user, "contactDetails", "1");
System.out.println(mappedProperty);
}
}
@Data
@ToString(callSuper = true)
public class User extends Parent{
private Integer id;
private String name;
private String[] subject;
private Map<String,String> contactDetails;
}
@Data
public class Parent {
private String father;
private String mother;
}