BeanUtil工具类是apache commons中的项目

使用BeanUtil除了需要 commons-beanutils-1.8.3.jar 外,可能需要记录错误日志信息,再加入 commons-logging-1.1.3.jar(也是apache的) 即可

下面着重看一些例子

// 实体类User Point,这里就省去get,set方法

package com.yangwei.model;

import java.util.Date;

public class User {

    private String name;
    private int age;
    private Date birth;
    private Point point;
}
public class Point {

    private int x;
    private int y;
}
package com.yangwei.test;

import static org.junit.Assert.fail;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import com.yangwei.model.User;

public class TestBeanUtil {

    @Test
    public void test01() {

        try {
            User u=new User();
            //假设要为name设置值"zhangsan"
            String key="name";
            String value="zhangsan";
            //以前我们使用Method 调用它的invoke方法完成操作
            //现在我们使用BeanUtils的copyProperty完成设值操作
            BeanUtils.copyProperty(u, key, value);
            System.out.println(u.getName());//zhangsan

            //拷贝不认识的属性,也不会报错
            BeanUtils.copyProperty(u, "yyy", value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

}
两个转换器类 ,实现Converter接口

package com.yangwei.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class DateConverter implements Converter{

    /**
     * 第一个参数表示要转换的类型, 第二个参数表示要转换的值
     * 比如要拷贝一个字符串到日期中,第一个参数就是日期,第二个参数是字符串值
     */
    SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Object convert(Class clz, Object obj) {
        if(clz!=Date.class){
            return null;
        }
        try {
            if(obj instanceof String){
                return f.parse((String) obj);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

}
package com.yangwei.model;

import org.apache.commons.beanutils.Converter;

public class PointConverter implements Converter {

    /**
     * 将传递过来的值转为Point类
     */
    @Override
    public Object convert(Class clz, Object obj) {

        if(clz!=Point.class){
            return null;
        }
        if(obj instanceof String){
            String value=(String)obj;
            String strArr[]=value.split(",");
            if(strArr!=null && strArr.length==2){
                Point point=new Point();
                point.setX(Integer.parseInt(strArr[0]));
                point.setY(Integer.parseInt(strArr[1]));
                return point;
            }
        }
        return null;
    }

}
package com.yangwei.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.junit.Test;

import com.yangwei.model.DateConverter;
import com.yangwei.model.Point;
import com.yangwei.model.PointConverter;
import com.yangwei.model.User;

public class TestBeanUtil {

    @Test
    public void test01() {

        try {
            User u=new User();
            //假设要为name设置值"zhangsan"
            String key="name";
            String value="zhangsan";
            //以前我们使用Method 调用它的invoke方法完成操作
            //现在我们使用BeanUtils的copyProperty完成设值操作
            BeanUtils.copyProperty(u, key, value);
            System.out.println(u.getName());//zhangsan

            //拷贝不认识的属性,也不会报错
            BeanUtils.copyProperty(u, "yyy", value);
            /**
             * 完整拷贝一个对象,此时会有错误,因为它不知道将Date转为何种类型
             * 日期是有很多中格式情况的,如 1977-10-10 1977/10/10等
             * 此时,如何处理呢???
             * 需要定义转换器  定义转换器的步骤:
             * 1, 创建一个类,实现Converter接口
             * 2,重写convert方法,实现转换
             * 3,在拷贝属性之前,注册转换器
            */
            ConvertUtils.register(new DateConverter(), Date.class);
            BeanUtils.copyProperty(u, "birth", "1988-11-20");
            ConvertUtils.register(new PointConverter(), Point.class);
            BeanUtils.copyProperty(u, "point", "12,23");

            User u2=new User();
            BeanUtils.copyProperties(u2, u);

            System.out.println(u.getName()+u.getBirth()+u.getPoint());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

}

apache-beanutil工具类的使用的更多相关文章

  1. Apache Commons 工具类介绍及简单使用

    转自:http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下 ...

  2. linkin大话数据结构--apache commons工具类

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动. 一.Commons BeanUtils 说明:针对Bean的一个工具集.由于Bean往往是有一堆ge ...

  3. Apache Commons 工具类简单使用

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  4. BeanUtil工具类的使用

    BeanUtils的使用 1.commons-beanutils的介绍 commons-beanutils是Apache组织下的一个基础的开源库,它提供了对Java反射和内省的API的包装,依赖内省, ...

  5. Apache Commons 工具类介绍及简单使用(转载)

    原文链接 http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动 ...

  6. Java:Apache Commons 工具类介绍及简单使用

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. Commons简介 组件 功能介绍 commo ...

  7. JavaWeb基础Day17 (JSP EL表达式 jstl标签库 beanutil工具类)

    JSP jsp的实质就是指在html界面中嵌入Java代码 jsp脚本 <%  Java代码  %>  相当于写在service方法中. <%=java 变量或者表达式 %> ...

  8. apache StringUtils 工具类

    // org.apache.commons.lang3.StringUtils // 1.IsEmpty/IsBlank - checks if a String contains text 检查是否 ...

  9. apache ArrayUtils 工具类

    org.apache.commons.lang3.ArrayUtils // 1.add():将给定的数据添加到指定的数组中,返回一个新的数组. int[] arr = { 1, 2, 3 }; in ...

  10. 03-封装BeanUtil工具类(javabean转map和map转javabean对象)

    package com.oa.test; import java.beans.BeanInfo; import java.beans.IntrospectionException; import ja ...

随机推荐

  1. GoF设计模式 - 概述

    掌握编程语言仅仅意味着掌握了如何给计算机"下命令",而到底要计算机如何去做,怎么指挥,则是另一个问题--如何编程.设计模式是一套程序员的"武功套路",它教我们如 ...

  2. Git使用教程及常用命令大全

     一.git命令名词解释1.添加/跟踪/暂存:添加到本地索引       git add 文件名2.提交:提交到本地仓库       git commit -m '注释'3.推送:将提交到本地仓库的所 ...

  3. [Spring面试] 问题整理

    1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC:Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...

  4. Spring框架——事务处理(编程式和声明式)

     一. 事务概述 ●在JavaEE企业级开发的应用领域,为了保证数据的完整性和一致性,必须引入数据库事务的概念,所以事务管理是企业级应用程序开发中必不可少的技术. ●事务就是一组由于逻辑上紧密关联而合 ...

  5. flume 搭建

    Flume使用 1安装及修改初始环境 安装见:http://blog.csdn.net/qianshangding0708/article/details/48088611 配置在conf目录下配置如 ...

  6. Linux io Model

    socket阻塞与非阻塞,同步与异步 作者:huangguisu 1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调 ...

  7. h5新增html标签语义

    H5新增常用标签<body> <header>...</header> <nav>...</nav> <article> < ...

  8. webservice时间类型XMLGregorianCalendar和Date的转换

    //ISO日期转换为UTC日期 public XMLGregorianCalendar xmlToDate(Date date){ GregorianCalendar cal = new Gregor ...

  9. RSA算法介绍

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt279 2.1.1     算法实现 首先, 找出三个数, p, q, r,其 ...

  10. Windows环境下最新OpenCV和Contribute代码的联合编译

    解决这个问题,目的在于获得并使用最新的完全版本的代码,主要方法是对CMake能够熟练使用,并且对编译等基础支持有所了解. 一.工具的准备 1 tortoisegit www.tortoisegit.o ...