BeanUtils\PropertyUtils工具包操作JavaBean

来源:互联网 发布:unity3d ios 录屏处理 编辑:程序博客网 时间:2024/04/29 18:50


BeanUtils

package com.in;import java.lang.reflect.InvocationTargetException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;public class BeanUtilsDeo {private Date birthDay = new Date();private int x;public int getX() {return x;}public void setX(int x) {this.x = x;}public Date getBirthDay() {return birthDay;}public void setBirthDay(Date birthDay) {this.birthDay = birthDay;}public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {BeanUtilsDeo bu = new BeanUtilsDeo(); //设置单个属性String x = BeanUtils.getProperty(bu, "x");System.out.println(x);BeanUtils.setProperty(bu, "x", "2");System.out.println(bu.getX());//设置级联属性BeanUtils.setProperty(bu, "birthDay.time", "2012-10-21");System.out.println(bu.getBirthDay().getTime());//设置map的属性Map map = new HashMap();map.put("name", "s");BeanUtils.setProperty(map, "name", "yy");System.out.println(map.get("name"));//设置单个属性PropertyUtils.setProperty(bu, "x", 9);System.out.println(bu.getX());PropertyUtils.setProperty(bu, "birthDay", new Date());System.out.println(bu.getBirthDay());}}


0 0