Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写。

BeanUtils的作用:

1)支持String到8种基本数据类型的转换;

2)其他引用数据类型都需要注册转换器:ConvertUtils.register(Converter,Class);

public class Person {
private String name;
private String password;
private Integer age;
private Date birthday;
getter/setter
}

采用beanutils设置属性

@Test
public void test1() throws Exception {
Person person = new Person();
BeanUtils.setProperty(person, "name", "zhangsan");
System.out.println(person.getName());
}

采用beanutils拷贝对象的属性(基本类型)

@Test
public void test2() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34"; Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换,只支持8种基本数据类型 System.out.println(person);
}

采用beanutils拷贝对象的属性(非基本类型)

@Test
public void test3() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34";
String birthday = "1980-09-05"; Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换
BeanUtils.setProperty(person, "birthday", birthday); // 类型转换失败
System.out.println(person);
}

为让Date能够赋值到birthday属性上,我们给beanutils注册一个日期转换器。

改进代码如下:

@Test
public void test4() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34";
String birthday = "1980-09-05"; ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
if (value == null) {
return null;
} if (!(value instanceof String)) {
throw new ConversionException("只支持String类型的转换!");
} String str = (String) value;
if (null == str || "".equals(str.trim())) {
return null;
} SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(str);
} catch (ParseException e) {
throw new ConversionException(e);
}
} }, Date.class
); Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换
BeanUtils.setProperty(person, "birthday", birthday);
System.out.println(person);
}

采用beanutils自带的日期类型转换器

@Test
public void test5() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34";
String birthday = "1980-09-05"; ConvertUtils.register(new DateLocaleConverter(), Date.class); Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换
BeanUtils.setProperty(person, "birthday", birthday);
System.out.println(person);
}

收集数据到map中,并通过beanutils拷贝到指定的对象中去

@Test
public void test6() throws Exception { Person person = new Person(); ConvertUtils.register(new DateLocaleConverter(), Date.class); Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "aa");
map.put("password", "123");
map.put("age", 23);
map.put("birthday", "1980-09-05"); // 用map集合中的值,填充bean的属性,
// 注意:属性名称必须要一致
BeanUtils.populate(person, map); System.out.println(person);
}

内省—beanutils工具包的更多相关文章

  1. JavaWeb -- 内省—beanutils工具包 的使用

    Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写. Beanu ...

  2. 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性

      本文转载自:http://blog.sina.com.cn/s/blog_5d65a16901011kom.html 关键字:内省.JavaBean.PropertyDescriptor类.Int ...

  3. java 内省综合案例和Beanutils工具包

    演示用eclipse自动生成 ReflectPoint类的setter和getter方法. 直接new一个PropertyDescriptor对象的方式来让大家了解JavaBean API的价值,先用 ...

  4. 32_使用BeanUtils工具包操作JavaBean

      由于对属性设置值和得到值的需求很多,使用频率很高,所以有一些开源勇士 不满足于JavaBean API 中IntroSpector来操作bean, 写出来了通用的BeanUtils工具,来进一步简 ...

  5. Commons BeanUtils工具包

    简介: BeanUtils工具包是由Apache公司所开发,提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理. 我们知道,一个JavaBean通常包含了大 ...

  6. javaweb学习总结五(内省、beanUtils工具包)

    一:内省的概念 1:内省是反射的一种特例,由于在反射中频繁的操作javabean,所以为了方便反射 javabean,sun公司开发出一套API提高效率. 2:javaBean,就是用来封装客户端请求 ...

  7. JAVAWEB开发之Session的追踪创建和销毁、JSP具体解释(指令,标签,内置对象,动作即转发和包括)、JavaBean及内省技术以及EL表达式获取内容的使用

    Session的追踪技术 已知Session是利用cookie机制的server端技术.当client第一次訪问资源时 假设调用request.getSession() 就会在server端创建一个由 ...

  8. javase(14)_java基础增强

    一.Eclipse的使用 1.在eclipse下Java程序的编写和run as,debug as,及java运行环境的配置. 2.快捷键的配置,常用快捷键: •内容提示:Alt + / •快速修复: ...

  9. JavaBean 内省API BeanUtils工具 泛型 xml xml约束

    1 什么是JavaBean?有何特征? 1)符合特定规则的类    2)JavaBean分二类:     a)侠义的JavaBean         .私有的字段(Field)         .对私 ...

随机推荐

  1. jsoncpp用法通俗易懂之将数据合成json格式

    void *upload(void *pParam) { CUpSender *s = (CUpSender*)pParam; map<string, string> mx; char t ...

  2. Python(正则 Time datatime os sys random json pickle模块)

    正则表达式: import re #导入模块名 p = re.compile(-]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表 ...

  3. FFTW简介及使用

    http://fftw.org/ FFTW is a C subroutine library for computing the discrete Fourier transform (DFT) i ...

  4. php计算字符串长度:utf8编码,包含中文

    php计算字符串长度:utf8编码 中文当作1个字符处理(strlen默认当作两个字符) 上函数: /** * 计算 UTF-8 字符串长度 * * @param string $str * @ret ...

  5. JavaScript权威指南 第二章 词法结构

    这章主要把保留字说一下 JavaScript 把一些标识符拿出来用做自己的关键字.因此,就不能再在程序中把这些关键字用做标识符了: break delete function return typeo ...

  6. 黄聪:百度知道中对HTML字符实体、字符编号,&开头字符的使用

    http://www.w3school.com.cn/tags/html_ref_entities.html 带有实体名称的 ASCII 实体 结果 描述 实体名称 实体编号 " quota ...

  7. Tomcat插件与Jetty插件在MyEclipse中的配置

    -Djetty.port=8101 jetty:run tomcat6:run <plugin> <groupId>org.apache.tomcat.maven</gr ...

  8. adb 工具学习

    adb (android debug bridge)简单介绍: 1.adb 是 Android SDK中所带工具.使用adb,可以在PC上操作Android设备或者模拟器 2.主要功能有: 将本地ap ...

  9. redis批量删除

    ./redis-cli -p 6379 -a password keys "*_icp" | xargs redis-cli -p 6379-a password del 删除所有 ...

  10. 启动mongoDB 以及常用操作命令

    nonsql    关系数据库 集合  表 文档 行 启动mongoDB之前首先手动创建存放MongoDB数据文件的目录,如e:\mongo_data 执行命令  mongod --dbpath=e: ...