32_使用BeanUtils工具包操作JavaBean

 

由于对属性设置值和得到值的需求很多,使用频率很高,所以有一些开源勇士 不满足于JavaBean API 中IntroSpector来操作bean,

写出来了通用的BeanUtils工具,来进一步简化对java bean的操作,并开源放在apache网站上提供免费下载。

 

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包

这个日志包,很多框架都在用。

  • 在前面内省例子的基础上,用BeanUtils类先get原来设置好的属性,再将其set为一个新值。
    • get属性时返回的结果为字符串,set属性时可以接受任意类型的对象,通常使用字符串。

                  这非常适合浏览器传过来的字符串对对象进行set。

  • 用PropertyUtils类先get原来设置好的属性,再将其set为一个新值。
    • get属性时返回的结果为该属性本来的类型,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设值时为java.lang.String
        System.out.println(rf1.getX());
        BeanUtils.setProperty(rf1, "x",1); //原来类型 设值
         BeanUtils.setProperty(rf1, "x","2"); //支持String类型 设值
         System.out.println(rf1.getX());

        BeanUtils.setProperty(rf1, "birthday.time", "111");//支持属性级联操作
         System.out.println(BeanUtils.getProperty(rf1, "birthday.time").getClass().getName());//java.lang.String 111
        Map mm=BeanUtils.describe(rf1);//javabean转成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转换成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
    }

}

时间: 2024-08-26 14:32:04

32_使用BeanUtils工具包操作JavaBean的相关文章

java高新技术-操作javaBean

1. 对javaBean的简单内省操作 public class IntroSpectorTest { public static void main(String[] args) throws Exception{ ReflectPoint pt1 = new ReflectPoint(3, 5); String propertyName = "x"; //"x" --> "X" -->"getX" -->

内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性

内省.JavaBean.PropertyDescriptor类.Introspector类.BeanUtils工具包.注解.Rentention.Target.注解的基本属性和高级属性 本文转载自:http://blog.sina.com.cn/s/blog_5d65a16901011kom.html 关键字:内省.JavaBean.PropertyDescriptor类.Introspector类.BeanUtils工具包.注解.Rentention.Target.注解的基本属性和高级属性 内

JDBC--使用beanutils工具类操作JavaBean

1.在JavaEE中,Java类的属性通过getter,setter来定义: 2.可使用BeanUtils工具包来操作Java类的属性: --Beanutils是由Apache公司开发,能够方便对Bean类进行简便的操作 --涉及到的包: (1)   BeanUtils相关包 commons-beanutils-1.8.3.jar commons-beanutils-1.8.3-javadoc.jar commons-beanutils-1.8.3-javadoc.jar commons-bea

内省 操作javaBean

一.什么是javaBean 二. 操作javaBean的意义 三.如何操作javaBean 四.使用BeanUtils 操作javaBean

内省—beanutils工具包

Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写. BeanUtils的作用: 1)支持String到8种基本数据类型的转换: 2)其他引用数据类型都需要注册转换器:ConvertUtils.register(Converter,Class); public class Person { private String name; private String

JSP简单练习-EL表达式操作JavaBean

/* * javaBean代码 */ package bean; public class Box { double length; double width; double height; public Box() { length=0; width=0; height=0; } public double getLength() { return length; } public void setLength(double length) { this.length = length; }

使用内省的方式操作JavaBean

[java] view plain copy import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * 使用内省的方式操作JavaBean */ public class IntroSpectorTest { publi

内省操作javabean的属性

1 import java.beans.BeanInfo; 2 import java.beans.IntrospectionException; 3 import java.beans.Introspector; 4 import java.beans.PropertyDescriptor; 5 import java.lang.reflect.Method; 6 7 import org.junit.Test; 8 9 //使用内省api操作bean的属性 10 public class D

[新手学Java]使用内省(Introspector)操作JavaBean属性

获取类bean中的所有属性: @Test //获取类bean中的所有属性 public void test1() throws Exception{ BeanInfo info = Introspector.getBeanInfo(Person.class); PropertyDescriptor[] decriptors = info.getPropertyDescriptors(); for(PropertyDescriptor decriptor : decriptors){ //输出属性