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. SQL&EF优化第一篇 各种情况下的性能测试之count函数篇

    测试环境  mssql 08  +win7    数据 30W条 二〇一六年十月二十九日 09:04:43 结论:1>主键> *>可空列    推测未论证: 根据情况优先选择 顺便提 ...

  2. 开始使用Bootstrap

    bootstrap使用到的图标字体文件格式有 .woff,IIS7下需要添加MIME映射:.woff  application/x-font-woff

  3. Heap 3214 LIS题解

    依据问题转换成最长不降子序列问题. 10^9的输入数据计算起来还是挺花时间的.由于这里仅仅能使用O(nlgn)时间复杂度了. 只是证明是能够算出10^9个数据的. 由于时间限制是5s. #includ ...

  4. html5_storage存取实例

    <script src="jquery-1.8.3.js"></script><script>function set(){    var tt ...

  5. [sh]清理memcached缓存

    #!/bin/bash ###author xxx ###date xxx ###清理内存缓存 used=`free -m | awk 'NR==2' | awk '{print $3}'` free ...

  6. Java中的四种引用类型,强引用,软引用,弱引用,虚引用

    对于Java中的垃圾回收机制来说,对象是否被回收的标准在于该对象是否被引用.因此,引用也是JVM进行内存管理的一个重要概念. Java中对象的引用一般有以下4种类型: 1强引用  2软引用  3弱引用 ...

  7. 关于http ,那些你必须知道的事

    一,HTTP简介 1,HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览 ...

  8. 让一个 csproj 项目指定多个开发框架[转]

    原贴:https://walterlv.gitee.io/post/configure-projects-to-target-multiple-platforms.html 可移植类库.共享项目..N ...

  9. nodejs系列笔记01---Buffer

    纯JavaScript无法处理二进制数据,buffer就是用来处理二进制数据的 原始数据保存在buffer实例中,一个buffer实例类似于数组.buffer的大小在建立时指定的不可更改. buffe ...

  10. How to convert from BufferedImage to JavaFX 2.2 Image

    http://blog.idrsolutions.com/2012/11/convert-bufferedimage-to-javafx-image/ ———————————————————————— ...