Apache Commons Beanutils 三 (BeanUtils、ConvertUtils、CollectionUtils...)
前言
前面已经学习了Apache Commons Beanutils包里的PropertyUtils和动态bean,接下来将学习剩下的几个工具类,个人觉得还是非常实用的,特别是CollectionUtils;
BeanUtils
简单介绍下两个方法的使用,populate和copyProperties,
populate可以帮助我们把Map里的键值对值拷贝到bean的属性值中;
copyProperties,顾名思义,帮我们拷贝一个bean的属性到另外一个bean中,注意是浅拷贝
如下示例:
/*
* File Name: BeanUtilsTest.java
* Description:
* Author: http://www.cnblogs.com/chenpi/
* Create Date: 2017年5月30日
*/
package apache.commons.beanutils.example.utils; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import apache.commons.beanutils.example.pojo.User; /**
*
* @author http://www.cnblogs.com/chenpi/
* @version 2017年5月30日
*/ public class BeanUtilsTest
{ public static void main(String[] args) throws IllegalAccessException, InvocationTargetException
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "001");
//map.put("address", "hz");
map.put("id", "100");
map.put("state", false);
map.put("others", "others"); User u = new User();
BeanUtils.populate(u, map); System.out.println(u); User u1 = new User();
BeanUtils.copyProperties(u1, u);
System.out.println(u1);
}
}
ConvertUtils
实际上,BeanUtils是依赖ConvertUtils来完成实际山的类型转换,但是有时候我们可能需要自定义转换器来完成特殊需求的类型转换;
自定义类型转换器步骤:
1、定义一个实现类实现Converter接口
2、调用ConvertUtils.register方法,注册该转换器
如下是一个实例,我们会在字符串转换的时候,加上一个前缀:
/*
* File Name: CustomConverters.java
* Description:
* Author: http://www.cnblogs.com/chenpi/
* Create Date: 2017年5月30日
*/
package apache.commons.beanutils.example.utils; import java.lang.reflect.InvocationTargetException;
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 apache.commons.beanutils.example.pojo.User; /**
*
* @author http://www.cnblogs.com/chenpi/
* @version 2017年5月30日
*/ public class CustomConverters
{ public static void main(String[] args) throws IllegalAccessException, InvocationTargetException
{
ConvertUtils.register(new StringConverter(), String.class); Map<String, String> map = new HashMap<String, String>();
map.put("name", "001");
map.put("address", "hz");
map.put("id", "100");
map.put("state", "false"); User u = new User();
BeanUtils.populate(u, map); System.out.println(u);
}
} class StringConverter implements Converter {
/**
*
*
* @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
* @param type
* @param value
* @return
*/
@SuppressWarnings("unchecked")
@Override
public <T> T convert(Class<T> type, Object value)
{ if(String.class.isInstance(value)){
return (T) ("###" + value);
}else{
return (T) value;
} }
}
这里有一点需要注意,像BeanUtils, ConvertUtils 和 PropertyUtils工具类都是共享同一个转换器的,这样子虽然用起来很方便,但有时候显得不够灵活,实际上BeanUtils, ConvertUtils 和 PropertyUtils都有一个对应的可实例化的类,即BeanUtilsBean、ConvertUtilsBean、PropertyUtilsBean;
它们的功能与BeanUtils, ConvertUtils 和 PropertyUtils类似,区别是它们可以实例化,而且每个实例都可以拥有自己的类型转换器;
CollectionUtils
顾名思义,集合工具类,只不过它操作的都是集合里的bean,
利用这个工具类,我们可以批量修改、查询、过滤集合中的bean,甚至还可以拷贝集合中所有bean的某个属性到另外一个集合中,有点Java 8新特性 Streams 的感觉
如下示例:
/*
* File Name: CollectionUtilsTest.java
* Description:
* Author: http://www.cnblogs.com/chenpi/
* Create Date: 2017年5月30日
*/
package apache.commons.beanutils.example.utils; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import org.apache.commons.beanutils.BeanPropertyValueChangeClosure;
import org.apache.commons.beanutils.BeanPropertyValueEqualsPredicate;
import org.apache.commons.beanutils.BeanToPropertyValueTransformer;
import org.apache.commons.collections.CollectionUtils; import apache.commons.beanutils.example.pojo.User; /**
*
* @author http://www.cnblogs.com/chenpi/
* @version 2017年5月30日
*/ public class CollectionUtilsTest
{ public static void main(String[] args)
{
List<User> userList = new ArrayList<User>();
User u1 = new User();
u1.setId(1l);
u1.setName("chenpi1");
u1.setState(true);
User u2 = new User();
u2.setId(2l);
u2.setName("chenpi2");
User u3 = new User();
u2.setId(3l);
u2.setName("chenpi3");
u2.setState(true);
userList.add(u1);
userList.add(u2);
userList.add(u3); //批量修改集合
BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("name",
"updateName"); CollectionUtils.forAllDo(userList, closure); for (User tmp : userList)
{
System.out.println(tmp.getName());
} BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate( "state", Boolean.TRUE ); //过滤集合
CollectionUtils.filter( userList, predicate );
for (User tmp : userList)
{
System.out.println(tmp);
} //创建transformer
BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "id" ); //将集合中所有你user的id传输到另外一个集合上
Collection<?> idList = CollectionUtils.collect( userList, transformer );
for (Object id : idList)
{
System.out.println(id);
}
}
}
参考资料
源码
https://github.com/peterchenhdu/apache-commons-beanutils-example
Apache Commons Beanutils 三 (BeanUtils、ConvertUtils、CollectionUtils...)的更多相关文章
- Spring中的BeanUtils与apache commons中的BeanUtils用法[1]
1. 前言 在开发过程中,经常遇到把要给一个bean的属性赋给另外一个bean.最笨的方法是每个属性都单独写一个,聪明的方法是应用反射写一个工具方法.考虑到这个需求基本每个程序员都会遇到,那么一定已经 ...
- Apache Commons Digester 三(规则注解)
前言 Digester规则的定义除了可以在代码中直接new规则添加到 Digester对象外,还可以用xml配置规则,如下所示: <digester-rules> <pattern ...
- Apache Commons 简述
Apache Commons 是一个关注于可复用的 Java 组件的 Apache 项目.Apache Commons 由三部分构成: Commons Proper - 一个可复用的 Java 组件库 ...
- 对于Java Bean的类型转换问题()使用 org.apache.commons.beanutils.ConvertUtils)
在进行与数据库的交互过程中,由数据库查询到的数据放在 map 中,由 map 到 JavaBean 的过程中可以使用 BeanUtils.populate(map,bean)来进行转换 这里要处理的问 ...
- org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法区别
知识点 org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作 ...
- Java工具类之Apache的Commons Lang和BeanUtils
Apache Commons包估计是Java中使用最广发的工具包了,很多框架都依赖于这组工具包中的一部分,它提供了我们常用的一些编程需要,但是JDK没能提供的机能,最大化的减少重复代码的编写. htt ...
- Apache Commons BeanUtils
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...
- myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入
com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...
- 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*
2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...
随机推荐
- Retrofit 2.0基于OKHttp更高效更快的网络框架 以及自定义转换器
时间关系,本文就 Retrofit 2.0的简单使用 做讲解 至于原理以后有空再去分析 项目全面.简单.易懂 地址: 关于Retrofit 2.0的简单使用如下: https://gitee.c ...
- 选择困难症的福音——团队Scrum冲刺阶段-Day 1领航
选择困难症的福音--团队Scrum冲刺阶段-Day 1领航 各个成员在 Alpha 阶段认领的任务 小组成员 分工 任务量 严域俊 完成小游戏接口部分.小游戏编写部分 21 吴恒佚 决策判断部分.小游 ...
- Subarray Sums Divisible by K LT974
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- [uboot] (番外篇)uboot relocation介绍
http://blog.csdn.net/ooonebook/article/details/53047992 以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为 ...
- 手机端table表格bug
table表格在手机端有一个小小的bug,就是td有一个右边线,解决办法可已给tr加一个背景色就行,或者table都行,完美解决
- delphi 中的浮点数 (转载)
原文地址 Floating point numbers — Sand or dirt Floating point numbers are like piles of sand; every time ...
- APP微信支付报错《商户号该产品权限未开通,请前往商户平台>产品中心检查后重试》
问题 最近项目使用MUI,HBuilder.开发打包H5的app 在开发H5 plus支付的时候,遇到以下问题: App微信支付调官方的统一下单接口返回错误信息 {return_msg=商户号该产品权 ...
- ABP框架系列之十三:(Authorization-授权)
Introduction Almost all enterprise applications use authorization in some level. Authorization is us ...
- 【MarkMark学习笔记学习笔记】javascript/js 学习笔记
1.0, 概述.JavaScript是ECMAScript的实现之一 2.0,在HTML中使用JavaScript. 2.1 3.0,基本概念 3.1,ECMAScript中的一切(变量,函数名,操作 ...
- 关于esp32的系统初始化启动过程及设计学习方法
对于esp32,其开发程序中有且只能有一个app_main函数,该函数是用户程序的入口,这在没有调用FreeRTOS的系统中相当于函数main,但其实在app_main之前,系统还有一段初始化的过程, ...