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. android UI 适配小节

    一.   像素密度表 像素密度表 比如UE 给了三张切图分别对应: mdpi,     xhdpi,     xxdpi 10 * 10,     20 * 20,     30 * 30 上面的值都 ...

  2. 创建cocos2d-x+lua项目

    1>     创建cocos2d-x+lua项目 进入到cocos2d-x-2.1.5\tools\project-creator文件夹运行下面命令: python create_project ...

  3. 盘点SEO和SEM的优劣势

    如果你不知如何分配你的搜索营销预算,或是和客户提案的时候不知道怎么样去解释搜索营销产品(SEO和SEM)的区别,又或者不了解网站/企业在当前阶段应该优先施行哪种搜索营销策略,本文可以帮助你深入了解SE ...

  4. 图片onerror事件,为图片加载指定默认图片

    为图片指定加载失败时显示默认图片,js输出的img对象,onerror是事件,不是属性,所以这样写是不起作用的: var img = $(document.createElement("IM ...

  5. SQL基础之聚合与排序

    聚合函数是用来求和,平均值,最大最小值一类的函数. 常用的有COUNT.SUM.MAX.MIN.AVG. count() 参数为列名,也可以使用*,表示全部列. 默认*会统计所有行的数据,如果想过滤掉 ...

  6. iOS开发多线程篇 07 —GCD的基本使用

    iOS开发多线程篇—GCD的基本使用 一.主队列介绍 主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行. 提示:如果把任务放到主队列中进 ...

  7. 解决eclipse无法打开:Failed to load the JNI shared library

    64位系统安装了64位的eclipse,但是jdk是32位的 总之就是eclipse跟jdk搭配不上,所以解决的时候注意这一点

  8. 有了 tldr,妈妈再也不用担心我记不住命令了

    引言 有一次我在培训时说「程序员要善于使用 Terminal 以提高开发效率」,一位程序员反驳道:「这是 21 世纪,我们为什么要用落后的命令行,而不是先进的 GUI?」 是的,在一些人眼里,这个黑黑 ...

  9. DIV的内容自动换行

    word-break:break-all和word-wrap:break-word都是能使其容器如DIV的内容自动换行. 它们的区别就在于: 1,word-break:break-all 例如div宽 ...

  10. shell变量/环境变量和set/env/export用法_转

    转自:shell环境变量以及set,env,export的区别 一.shell环境变量的分类以及set env export的区别: set:显示(设置)shell变量,包括的私有变量以及用户变量.不 ...