commons-beanutils是通过内省来完成的。

需要两个包:

  • commons-beanutils-1.8.3.jar
  • commons-logging-1.1.1.jar

JavaBean类:

package cn.zy.study;
/**
* 提供get和set方法
* 并有一个无参的构造方法
* @author zy
*
*/
public class Person { private String name;
private int age;
private String gender;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", gender=" + gender
+ "]";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
} }

  

对bean类的操作

package cn.test;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test; import cn.zy.study.Person; public class Demo3 { @Test
public void fun1() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
//用Class类的forName方法来获取对应的class对象
String className = "cn.zy.study.Person";
Class clazz = Class.forName(className);
//使用clazz对应类的默认构造器创造实例
Object bean = clazz.newInstance();
/**使用beanutils工具设置bean类的属性值,
* 所有的属相值都可以设置成String类型,将会自动转换
* 没有设置属性的值将为null
*/
BeanUtils.setProperty(bean, "name", "Scott");
BeanUtils.setProperty(bean, "age", "17");
System.out.println(bean);
//得到bean类的属性值,注意属值都将变成String类型
String name = BeanUtils.getProperty(bean, "name");
String age = BeanUtils.getProperty(bean, "age"); } @Test
/**
* 将map直接封装到一个bean中
*/
public void fun2() throws InstantiationException, IllegalAccessException, InvocationTargetException{
Map<String,String> map = new HashMap();
map.put("name","Scott");
map.put("age","15");
//利用类的class属性来获取Class对象,并创建
Person bean = Person.class.newInstance();
//将map数据填充到Javabean中
BeanUtils.populate(bean, map);
System.out.println(bean);
} }

  

commons-beanutils的使用的更多相关文章

  1. Apache Commons BeanUtils

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

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

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

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

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

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

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

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

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

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

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

  7. 对于Java Bean的类型转换问题()使用 org.apache.commons.beanutils.ConvertUtils)

    在进行与数据库的交互过程中,由数据库查询到的数据放在 map 中,由 map 到 JavaBean 的过程中可以使用 BeanUtils.populate(map,bean)来进行转换 这里要处理的问 ...

  8. Apache Commons Beanutils 三 (BeanUtils、ConvertUtils、CollectionUtils...)

    前言 前面已经学习了Apache Commons Beanutils包里的PropertyUtils和动态bean,接下来将学习剩下的几个工具类,个人觉得还是非常实用的,特别是CollectionUt ...

  9. Apache Commons Beanutils 二 (动态Bean - DynaBeans)

    相关背景 上一篇介绍了PropertyUtils的用法,PropertyUtils主要是在不修改bean结构的前提下,动态访问bean的属性: 但是有时候,我们会经常希望能够在不定义一个Java类的前 ...

  10. Apache Commons Beanutils 一 (使用PropertyUtils访问Bean属性)

    BeanUtils简要描述 beanutils,顾名思义,是java bean的一个工具类,可以帮助我们方便的读取(get)和设置(set)bean属性值.动态定义和访问bean属性: 细心的话,会发 ...

随机推荐

  1. (笔记)Mysql命令create table:创建数据表

    create table命令用来创建数据表. create table命令格式:create table <表名> (<字段名1> <类型1> [,..<字段 ...

  2. 第三百六十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mget和bulk批量操作

    第三百六十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mget和bulk批量操作 注意:前面讲到的各种操作都是一次http请求操作一条数据,如果想 ...

  3. Mybatis表关联多对一

    在上章的 一对多 中,我们已经学习如何在 Mybatis 中关联多表,但在实际项目中也是经常使用 多对一 的情况,这些查询是如何处理的呢,在这一节中我们来学习它.多表映射的多对一关系要用到 mybit ...

  4. e812. 强制弹出菜单为重组件

    By default, Swing popup menus used by JMenu and JPopupMenu are lightweight. If heavyweight component ...

  5. SecureCRT连接linux,vim颜色显示问题

    为什么使用VIM?由于具有颜色显示的功能.而且还支持很多程序语法.VIM能够帮助你直接进行程序除错.可是在SecureCRT连接Linux后显示就是黑白,感觉非常不爽! ! 怎么办? vim编敲代码也 ...

  6. C# 无法在发送 HTTP 标头之后进行重定向

    在调试中发现错误如下: Response.Redirect引起的“无法在发送HTTP标头之后进行重定向” 跳转失败 解决方案如下: 使用js方法来跳转地址 const string url=" ...

  7. form中的button按钮在IE11中自动提交表单问题导致弹出框关闭之后表单被重置

    最近几天,测试系统,遇到一个兼容性问题,form中有一个button按钮,没有指定type类型,点击按钮弹出框选择值之后回填给form上的一个单行文本框,在IE6.IE7.IE8.IE9.IE10中测 ...

  8. python调用ansible接口API执行命令

    python版本:Python 2.6.6 ansible版本:ansible 2.3.1.0      下载地址:https://releases.ansible.com/ansible/ 调用脚本 ...

  9. R语言ggplot2中的panel. strip 基本概念

    ggplot2 是一套独立的绘图系统,在一个完整的ggplot2的图表中,会有下面几个概念: 1) plot 2) panel 3) strip 4) legend 所有这些元素都会出现在图表中 代码 ...

  10. [转]调试AngularJS应用

    原文链接:Debugging AngularJS Apps from the Console 当我们开发AngularJS应用的时候,我们想在Chrome/FF/IE控制台调试隐藏在应用中的数据和服务 ...