package com.test.beanutils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
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.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
public class TestBeanUtils {
    /**BeanUtils.setProperty设置属性*/
    @Test
    public void test1() throws IllegalAccessException, InvocationTargetException{
        Person person = new Person();
        BeanUtils.setProperty(person, "age", 23);
        System.out.println(person.getAge());
    }
    /**BeanUtils.cloneBean克隆一个bean*/
    @Test
    public void test2() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException{
        Person person = new Person("lucy", 18, "xxxxx@qq.com");
        Person cloneBean = (Person) BeanUtils.cloneBean(person);
        System.out.println(cloneBean.toString());
    }
    /**注册转换器*/
    @Test
    public void test3() throws IllegalAccessException, InvocationTargetException{
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                String str = (String) value;
                if(null == value){
                    return null;
                }
                if(!(value instanceof String)){
                    System.out.println("格式不正确");
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date parse = sdf.parse(str);
                    return parse;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
        Person person = new Person();
        BeanUtils.setProperty(person, "birthday", "2016-07-01");
        System.out.println(person.getBirthday());
    }
    /**将map转换成bean并注册转换器*/
    @Test
    public void test4() throws IllegalAccessException, InvocationTargetException{
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "test");
        map.put("birthday", "2016-07-01");
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        Person person = new Person();
        BeanUtils.populate(person, map);
        System.out.println(person.getBirthday());
    }
    /**转换器*/
    @Test
    public void test5(){
        Integer num = (Integer) ConvertUtils.convert("123", Integer.class);
        System.out.println(num);
    }
}

160704、commons-beanutils.jar常用方法的更多相关文章

  1. Beanutils工具常用方法

      BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品.其主要目的是利用反射机制对JavaBean的属性进行处理. BeanUtils工具一般可以方便ja ...

  2. myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入

    com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...

  3. 再续前缘-apache.commons.beanutils的补充

    title: 再续前缘-apache.commons.beanutils的补充 toc: true date: 2016-05-32 02:29:32 categories: 实在技巧 tags: 插 ...

  4. 报错处理 java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean

    java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean at org.apache.catalina.loade ...

  5. Apache Commons BeanUtils

    http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...

  6. 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*

    2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...

  7. Apache Commons Beanutils对象属性批量复制(pseudo-singleton)

    Apache Commons Beanutils为开源软件,可在Apache官网http://commons.apache.org/proper/commons-beanutils/download_ ...

  8. Commons Beanutils使用setProperty() - 就是爱Java

    有时不能只依靠getter/setter操作bean,如:需要名字动态取得的,或是访问bean内的field,甚至是集合或数组内bean的field,利用反射机制对bean的field进行处理,这时候 ...

  9. org.apache.commons.beanutils.BeanMap简单使用例子

    一.org.apache.commons.beanutils.BeanMap; 将一个java bean允许通过map的api进行调用, 几个支持的操作接口: Object get(Object ke ...

随机推荐

  1. java基础讲解09-----接口,继承,多态

    还有什么包装类,数字类,这些简单的我就不想过去介绍,前面也大概的介绍了下,继承,多态 1.类的继承 继承的思想:基于某个父类的扩展,制定一个新的子类.子类可以继承父类原有的属性,方法,也可以重写父类的 ...

  2. web开发中常见的安全漏洞及避免方法

    1.安全攻击 1.SQL.HTML.JS.OS命令注入 2.XSS跨站脚本攻击,利用站内信任的用户,在web页面插入恶意script代码 3.CSRF跨站请求伪造,通过伪装来自信任用户的请求来利用受信 ...

  3. mysql事务块处理

    begin ,rollback,commit .当然有的人用begin /begin work .推荐用START TRANSACTION 是SQL-99标准启动一个事务. start transac ...

  4. Lintcode---统计比给定整数小的数的个数

    给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表.对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量. ...

  5. 设计模式_CallBack

    一.基本概念 if you call me, i will call back 什么是回调函数  回调函数(callback Function),顾名思义,用于回调的函数. 回调函数只是一个功能片段, ...

  6. 【ZT】NUMA架构的CPU -- 你真的用好了么?

    本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...

  7. java 发送带Basic Auth认证的http post请求实例代码

    构造http header private static final String URL = "url"; private static final String APP_KEY ...

  8. 无需序列号安装Photoshop CS6

    在百度下载Photoshop CS6破解版,解压后,如图所示   进入Photoshop_CS6.3510481888\Adobe CS6目录,双击Set-up.exe开始安装   点击接受协议,进入 ...

  9. hdu 1213 (How Many Tables)(简单的并查集,纯模板)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. mysql数据库表修改某一列的类型

    下面列出:1.增加一个字段alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空alter table use ...