使用BeanUtils操作Bean属性
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属性的更多相关文章
- beanUtils操作bean的属性
beanUtils操纵bean属性: 需要jar包commons-beanutils-x.x.x.jar 同时commons-beanutils-x.x.x.jar需要commons-loggi ...
- Apache Commons Beanutils 一 (使用PropertyUtils访问Bean属性)
BeanUtils简要描述 beanutils,顾名思义,是java bean的一个工具类,可以帮助我们方便的读取(get)和设置(set)bean属性值.动态定义和访问bean属性: 细心的话,会发 ...
- sql查询化繁为简 告别rs.getString("XX"),bean属性赋值setXX("XX")
一.在执行sql语句查询时候,查询的结果是set的map集合(ResultSet): 结果使用rs.getString("XX")获得对应属性的值,赋值到bean对象的相应的属性中 ...
- 一个Bean属性拷贝的工具类
package com.fpi.spring.qaepb.cps.util; import java.beans.IntrospectionException; import java.beans.P ...
- 【框架】利用Spring的BeanPostProcessor来修改bean属性
一.BeanPostProcessor是什么?什么时候触发?可以用来做什么? 1.它是什么? 首先它是一个接口,定义了两个方法: public interface BeanPostProcessor ...
- 在Spring Bean实例过程中,如何使用反射和递归处理的Bean属性填充?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! <Spring 手撸专栏>目录 [x] 第 1 章:开篇介绍,我要带你撸 Spri ...
- spring实战一:装配bean之注入Bean属性
内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...
- 依赖注入Bean属性
一.Bean属性依赖注入 对于类成员变量,注入方式有三种 •构造函数注入 •属性setter方法注入 •接口注入 Spring支持前两种 1.构造函数 属性注入 使用构造方法注入,在Spring配置文 ...
- JavaScript 节点操作Dom属性和方法(转)
JavaScript 节点操作Dom属性和方法 一些常用的dom属性和方法,列出来作为手册用. 属性: 1.Attributes 存储节点的属性列表(只读) 2.childNodes 存储 ...
随机推荐
- exceptions-in-java
http://www.javaworld.com/article/2076721/core-java/designing-with-exceptions.html http://www.javawor ...
- Qt QTreeWidget 树形结构实现(转)
Qt中实现树形结构可以使用QTreeWidget类,也可以使用QTreeView类,QTreeWidget继承自QTreeView类.树形效果如下图所示: 这是怎么实现的呢?还有点击节点时会有相应的事 ...
- Android Studio导入GitHub上的项目常见问题(有例子)
前言:github对开发者而言无疑是个宝藏,但想利用它可不是件简单的事,用Android studio导入开源项目会遇到各种问题,今天我就以github上的一个图片轮播项目为例,解决导入过程中的常见问 ...
- 如何用ABBYY把PDF如何转换成HTML
将PDF转换成HTML网页格式,是快速打造专业级网站的方法之一.当用户找到了非常详实的PDF资料,打算将之制作成为网页格式时,如果重新开发往往需要耗费大量的时间,可是又不知道怎么样才可以将PDF文件转 ...
- Code First 数据注释
Code First 数据注释 Julie Lerman http://thedatafarm.com 通过实体框架 Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数 ...
- 001. 启动Visual Studio 2010报错
错误内容: 外接程序"VMDebugger"未能加载或导致了异常. 是否希望移除该外接程序? 错误号: 80004005 如下图: 解决办法一: 1. 点击 是 打开VS2010, ...
- c语言编程中%g是什么格式
%g用来输出实数,它根据数值的大小,自动选f格式或e格式(选择输出时占宽度较小的一种),且不输出无意义的0.即%g是根据结果自动选择科学记数法还是一般的小数记数法 printf("%g\n& ...
- Ajax编程中,经常要能动态的改变界面元素的样式
在Ajax编程中,经常要能动态的改变界面元素的样式,可以通过对象的style属性来改变,比如要改变背景色为红色,可以这样写:element.style.backgroundColor=”#ff0000 ...
- Python 2.x与3.x共存
(1)检查在Path环境变量中是否有以下4个变量(没有则添加): 1.c:\Python27 2.c:\Python27\Scripts 3.c:\Python35 4.c:\Python35\Scr ...
- MySQL中Group By,distinct使用注意事项
mysql> select * from test; +----+-------+------+-------+ | id | name | age | class | +----+------ ...