国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

32_使用BeanUtils工具包操作JavaBean

2019-11-14 22:46:49
字體:
供稿:網(wǎng)友
32_使用BeanUtils工具包操作javaBean

 

由于對屬性設(shè)置值和得到值的需求很多,使用頻率很高,所以有一些開源勇士 不滿足于JavaBean API 中IntroSpector來操作bean,

寫出來了通用的BeanUtils工具,來進(jìn)一步簡化對java bean的操作,并開源放在apache網(wǎng)站上提供免費下載。

 

Beanutils工具包

  • 演示用eclipse如何加入jar包,先只是引入beanutils包,等程序運行出錯后再引入logging包。
1 commons-beanutils-1.9.2-bin.zip  http://u2l.info/3VN80n 2 commons-beanutils-1.9.2-src.zip  http://u2l.info/3o99D3 3 commons-logging-1.1.3-bin.zip   http://u2l.info/2D1d0m 4 commons-logging-1.1.3-src.zip   http://u2l.info/nKLKp

 

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 缺少logging的jar包

這個日志包,很多框架都在用。

  • 在前面內(nèi)省例子的基礎(chǔ)上,用BeanUtils類先get原來設(shè)置好的屬性,再將其set為一個新值。
    • get屬性時返回的結(jié)果為字符串,set屬性時可以接受任意類型的對象,通常使用字符串。

                  這非常適合瀏覽器傳過來的字符串對對象進(jìn)行set。

  • PRopertyUtils類先get原來設(shè)置好的屬性,再將其set為一個新值。
    • get屬性時返回的結(jié)果為該屬性本來的類型,set屬性時只接受該屬性本來的類型。

java bean

package com.itcast.day1;import java.util.Date;public class ReflectPoint {    private Date birthday=new Date();        private int x;    public int y;    public ReflectPoint(int x, int y) {        super();        this.x = x;        this.y = y;    }    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + x;        result = prime * result + y;        return result;    }    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        ReflectPoint other = (ReflectPoint) obj;        if (x != other.x)            return false;        if (y != other.y)            return false;        return true;    }    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    @Override    public String toString() {        return "ReflectPoint [birthday=" + birthday + ", x=" + x + ", y=" + y                + "]";    }    }

測試類:

package com.itcast.day2;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;import com.itcast.day1.ReflectPoint;public class IntroSpectorTest {    public static void main(String[] args) throws Exception{                ReflectPoint rf1=new ReflectPoint(3,4);                    Object value=6;        System.out.println(BeanUtils.getProperty(rf1, "x").getClass().getName());// x是int,但是用beanutils設(shè)值時為java.lang.String        System.out.println(rf1.getX());        BeanUtils.setProperty(rf1, "x",1); //原來類型 設(shè)值         BeanUtils.setProperty(rf1, "x","2"); //支持String類型 設(shè)值         System.out.println(rf1.getX());                BeanUtils.setProperty(rf1, "birthday.time", "111");//支持屬性級聯(lián)操作         System.out.println(BeanUtils.getProperty(rf1, "birthday.time").getClass().getName());//java.lang.String 111        Map mm=BeanUtils.describe(rf1);//javabean轉(zhuǎn)成map        System.out.println(rf1);//ReflectPoint [birthday=Thu Jan 01 08:00:00 GMT+08:00 1970, x=2, y=4]                Map map=new HashMap();        map.put("x", 1);        map.put("y",1);        BeanUtils.populate(rf1, map);//把map轉(zhuǎn)換成javabean         System.out.println(rf1);//ReflectPoint [birthday=Thu Jan 01 08:00:00 GMT+08:00 1970, x=1, y=1]                //PropertyUtils.setProperty(rf1, "x", "9");//運行出錯!因為PropertyUtils只支持原來的類型,這點沒有BeanUtils強大!         PropertyUtils.setProperty(rf1, "x", 9);        System.out.println(rf1.getX());//9    }}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金昌市| 凤山县| 兴宁市| 张掖市| 苏州市| 固始县| 怀来县| 若羌县| 井冈山市| 吉木萨尔县| 马边| 甘泉县| 泽普县| 伊吾县| 枣庄市| 行唐县| 通道| 屯昌县| 阜新市| 阜新| 区。| 嫩江县| 和政县| 阿拉善左旗| 丰宁| 霍林郭勒市| 饶平县| 洛扎县| 河北区| 响水县| 句容市| 南宫市| 奉化市| 务川| 深圳市| 九寨沟县| 沂源县| 贵溪市| 平和县| 平南县| 东安县|