package com.wzh.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.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test; //使用BeanUtils操作Bean属性(第三方)
public class Demo1 { @Test
public void test1() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();
BeanUtils.setProperty(p, "name", "victor"); System.out.println(p.getName());
} @Test
public void test2() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();
// 自动将String转为int 支持8种基本数据类型
BeanUtils.setProperty(p, "age", "23");
// 默认不支持时间转换
BeanUtils.setProperty(p, "Birthday", "2012-3-1");
System.out.println(p.getAge());
System.out.println(p.getBirthday());
} @Test
public void test3() throws IllegalAccessException,
InvocationTargetException { // 为了让日期赋到Bean的Birthday属性上,我们给BeanUtils注册一个日期转换器(敲入代码未起作用,需检查)
// 方法1.
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
if (value == null)
return null; if (!(value instanceof String)) {
System.out.println("不是日期类型");
throw new ConversionException("不是日期类型");
} String str = (String) value;
if (str.trim().equals(str)) {
return null;
} SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);// 异常链不能断
}
}
}, Date.class); // ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p = new Person();
// 自动将String转为int 支持8种基本数据类型 BeanUtils.setProperty(p, "birthday", "2012-12-12");
System.out.println(p.getBirthday());
} @Test
public void test4() throws IllegalAccessException,
InvocationTargetException { Map map = new HashMap();
map.put("name", "aa");
map.put("birthday", "2012-1-1"); ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p = new Person();
// 自动将String转为int 支持8种基本数据类型 BeanUtils.populate(p, map);//用Map集合中的值填充到Bean的属性
System.out.println(p.getBirthday());
}
}
package com.wzh.test.beanutils;

import java.util.Date;

public class Person {

	public Person() {
super();
// TODO Auto-generated constructor stub
}
private String name;
private String age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

  

  

使用BeanUtils操作Bean属性的更多相关文章

  1. beanUtils操作bean的属性

    beanUtils操纵bean属性: 需要jar包commons-beanutils-x.x.x.jar    同时commons-beanutils-x.x.x.jar需要commons-loggi ...

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

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

  3. sql查询化繁为简 告别rs.getString("XX"),bean属性赋值setXX("XX")

    一.在执行sql语句查询时候,查询的结果是set的map集合(ResultSet): 结果使用rs.getString("XX")获得对应属性的值,赋值到bean对象的相应的属性中 ...

  4. 一个Bean属性拷贝的工具类

    package com.fpi.spring.qaepb.cps.util; import java.beans.IntrospectionException; import java.beans.P ...

  5. 【框架】利用Spring的BeanPostProcessor来修改bean属性

    一.BeanPostProcessor是什么?什么时候触发?可以用来做什么? 1.它是什么? 首先它是一个接口,定义了两个方法: public interface BeanPostProcessor ...

  6. 在Spring Bean实例过程中,如何使用反射和递归处理的Bean属性填充?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! <Spring 手撸专栏>目录 [x] 第 1 章:开篇介绍,我要带你撸 Spri ...

  7. spring实战一:装配bean之注入Bean属性

    内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...

  8. 依赖注入Bean属性

    一.Bean属性依赖注入 对于类成员变量,注入方式有三种 •构造函数注入 •属性setter方法注入 •接口注入 Spring支持前两种 1.构造函数 属性注入 使用构造方法注入,在Spring配置文 ...

  9. JavaScript 节点操作Dom属性和方法(转)

    JavaScript 节点操作Dom属性和方法   一些常用的dom属性和方法,列出来作为手册用. 属性:   1.Attributes 存储节点的属性列表(只读)   2.childNodes 存储 ...

随机推荐

  1. Linux dbg debugging

    http://h41379.www4.hpe.com/doc/84final/4538/4538pro_contents.html https://kgdb.wiki.kernel.org/index ...

  2. mysql学习之-三种安装方式与版本介绍

    MYSQL版本介绍 mysql分alpha,beta,rc,GA四个版本. alpha  暗示这是一个以展示新特性为目的的版本,存在比较多的不稳定因素,还会向代码中添加新新特性beta 以后的beta ...

  3. C# 正则表达式类 Match类和Group类

    @"\b(\S+)://(\S+)\b"; //匹配URL的模式foreach (Match match in mc){ Console.WriteLine(match.Value ...

  4. css @语法,@规则 @import @charset @font-face @fontdef @media @page

    CSS At-Rules Reference    样式表规则 At-Rules 样式表规则 CSS Version 版本 Compatibility 兼容性 Description 简介 @impo ...

  5. linux概念之cpu分析

    http://ilinuxkernel.com/?cat=4 Linux CPU占用率原理与精确度分析1  CPU占用率计算原理在Linux/Unix 下,CPU 利用率分为用户态.系统态和空闲态,分 ...

  6. wikioi 1202 求和(求n个数的和)

    /*============================================================= 1202 求和 题目描述 Description 求n个数的和 输入描述 ...

  7. php 生成短URL的算法

    function code62($x){     $show='';     while($x>0){         $s=$x % 62;         if ($s>35){    ...

  8. 【性能诊断】四、单功能场景的性能分析(RedGate,找到同一个客户端的并发请求被串行化问题)

    问题描述: 客户端js连续发起两个异步http请求,请求地址相同,但参数不同:POST http://*.*.*.*/*****/webservice/RESTFulWebService/RESTFu ...

  9. [mysql] linux 下mysql 5.7.12 安装

    1.下载mysql wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar ...

  10. 并行计算之OpenMP入门简介

    在上一篇文章中介绍了并行计算的基础概念,也顺便介绍了OpenMP. OpenMp提供了对于并行描述的高层抽象,降低了并行编程的难度和复杂度,这样程序员可以把更多的精力投入到并行算法本身,而非其具体实现 ...