1、定义一个二个bean类
(1) 第一个
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("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]"));
} 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
new_jhlishero@163.com
12345678911
nest 11111111
nest 11111111
maps(key2) value
vlaue3
分享到:
相关推荐
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用
NULL 博文链接:https://m635674608.iteye.com/blog/2270455
easymock-propertyutils 注意:此库未维护,可能已被取代 这个库允许你在使用时对参数使用 JavaBeans 风格的属性匹配
4.1 PropertyUtils,当属性为Collection,Map时的动态读取 4.2 PropertyUtils,获取属性的Class类型 4.3 ConstructorUtils,动态创建对象 4.4 MethodUtils,动态调用方法 4.5 动态Bean 见用DynaBean减除不必要的VO...
1:AES 加密解密 2:AuthUtils 授权相关的工具类 3:Base64 base64编码 ...23:PropertyUtils 属性工具类 24:QuerySqlUtil 25:RegexUtils 26:TestClass 28:UploadUtils 文件上传工具 29:UtilTest 30:VideoTool
PropertyUtils Property Unility Class RandomUtils Random Unility Class ResourceUtils Resource Unility Class SHA1Utils Sha1 Unility Class SerializableUtils Serializable Unility Class ...
主要介绍了Java反射获取class对象方式解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
BeanUtils.java CachedIntrospectionResults.java ClassUtils.java ...PropertyUtils.java ReflectionUtils.java ResourceUtils.java SerializationUtils.java StringUtils.java TypeUtils.java Validate.java
1.1,beanutils propertyUtils类副本与反射副本使用及效率比较。 1.2,收藏CollectionUtils从一个列表中查找符合一定条件的对象。 2,太阳 2.1,发送邮件javax.mail完成邮件的发送和接收。 3,MyBatis 3.1,基本...
1.PropertyUtils 对标注NotNull注解的属性,检查其值是否为null 2.JsonUtils 用于获取json字符串中指定key的value 示例 json = {"name":"刘禅","age":"6","father":{"name":"刘备","age":"50","properties": {...