[转]PropertyUtils的使用
Posted lp2cx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转]PropertyUtils的使用相关的知识,希望对你有一定的参考价值。
- package com.sunrex.demo02;
- import java.util.List;
- 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;
- }
- }
(2)第二个
- package com.sunrex.demo02;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class Person {
- private String username;
- private int age;
- private float stature;//身高
- private boolean sex;//性别
- @SuppressWarnings("unchecked")
- private List list = new ArrayList();
- private String[] friendsNames;
- private Map<String, String> maps = new HashMap<String, String>();
- private Address address;
- 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;
- }
- }
3、测试类
- package com.sunrex.demo02;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.beanutils.PropertyUtils;
- public class PersonTest {
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- 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");
- String listValue2 = "new list value 1";
- PropertyUtils.setSimpleProperty(person, "list", list);
- //将list设置到person之后,可以对里面的值进行修改
- PropertyUtils.setIndexedProperty(person, "list[1]", listValue2);
- //数组
- String[] str = {"张三", "王五", "赵钱"};
- person.setFriendsNames(str);
- PropertyUtils.setIndexedProperty(person, "friendsNames[2]", "new赵钱");
- //Map
- Map<String, String> map = new HashMap<String, String>();
- 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("[email protected]");
- 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", "[email protected]");
- 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]"));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
4、输出结果
李四
22
173.5
false
[list value 0, new list value 1]
list value 0
new list value 1
张三
王五
new赵钱
new value1
[email protected]
12345678911
nest 11111111
nest 11111111
maps(key2) value
vlaue3
以上是关于[转]PropertyUtils的使用的主要内容,如果未能解决你的问题,请参考以下文章
对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀
BeanUtils.copyProperties VS PropertyUtils.copyProperties
BeanUtils.copyProperties()方法和PropertyUtils.copyProperties()的区别