使用beanUtils操纵javabean
Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils,在Beanutil中可以直接进行类型的自动转换。
BeanUtil工具包下载:
1,登录http://commons.apache.org/beanutils/
2, 点击Download
3, 点击commons-beanutils-1.9.2-bin.zip(commons-logging-1.1.3-src)进行下载就OK了
使用BeanUtil
在项目中导入commons-beanutils-1.9.2.jar包即可(PS:把此jar包复制到项目的lib文件夹,右击包,选build path==>add to build path 显示奶瓶即可)
另外:commons-beanutils-1.9.2.jar 要与 commons-logging-1.1.3.Jar共用。
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 Demo { /**
*
* @throws Exception
*
*/
@Test
public void test1() throws Exception{ Person p = new Person(); BeanUtils.setProperty(p, "name", "zero"); System.out.println(p.getName()); } @Test
public void test2() throws Exception{ String name = "aaaa";
String password = "123";
String age = "34"; Person p = new Person(); BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age); System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()); } @Test
public void test3() throws Exception{ String name = "aaaa";
String password = "123";
String age = "34";
String birthday = "1900-1-1"; //为了让日期赋到bean的birthday属性上,给beanUtils注册一个日期转换
ConvertUtils.register(new Converter(){ @Override
public Object convert(Class type, Object value) { if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("only support String");
} String str = (String) value; if(str.trim().equals("")){
return null;
} SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); try {
return df.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);
} } },Date.class); Person p = new Person(); BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday()); } @Test
public void test4() throws Exception{ String name = "aaaa";
String password = "123";
String age = "34";
String birthday = "1998-1-1"; ConvertUtils.register(new DateLocaleConverter(), Date.class); Person p = new Person(); BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday); Date date = p.getBirthday(); System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+date.toLocaleString()); } @Test
public void test5() throws Exception{ Map map = new HashMap();
map.put("name", "zero");
map.put("password", "521212");
map.put("age", "33");
map.put("birthday", "1998-1-1"); ConvertUtils.register(new DateLocaleConverter(), Date.class); Person p = new Person(); BeanUtils.populate(p, map);//用map集合的值,填充bean的属性值 System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday()); } }
import java.util.Date;
public class Person {// javabean
private String name;
private String password;
private int age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAb(){
return null;
}
}
使用beanUtils操纵javabean的更多相关文章
- 2016年8月17日 内省(1)18_黑马程序员_使用beanUtils操纵javabean
8.内省(1):18_黑马程序员_使用beanUtils操纵javabean 1.导入两个包: 2.调用静态方法. 9.泛型 map.entrySet() :取出map集合的键值对组成一个set集合. ...
- [新手学Java]使用beanUtils控制javabean
使用BeanUtils设置/读取属性的值以及默认支持的自动转化: @Test //使用BeanUtils设置/读取属性的值以及自动转化 public void test1() throws Illeg ...
- beanUtils操作bean的属性
beanUtils操纵bean属性: 需要jar包commons-beanutils-x.x.x.jar 同时commons-beanutils-x.x.x.jar需要commons-loggi ...
- java-内省与javabean
JavaBean 是一种JAVA语言写成的可重用组件.为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器.JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性 ...
- 几种封装javaBean的方法
开发框架时,经常需要使用java对象(javaBean)的属性来封装程序的数据,封装javaBean的方法有很多,比如反射,内省,以及使用工具类.下面从反射开始介绍. 1.javaBean介绍: 简介 ...
- BeanUtils基本使用方法与原理
使用BeanUtils的原因 因为setProperty是JSP中的标签,因此使用model 2模式JSP+Servlet+JavaBean的时候,JSP将form提交给Servlet程序,而Serv ...
- JavaBean内省与BeanInfo
Java的BeanInfo在工作中并不怎么用到,我也是在学习spring源码的时候,发现SpringBoot启动时候会设置一个属叫"spring.beaninfo.ignore", ...
- java反射之-Javabean与Map的互转
1.BeanUntils工具类的准备 /** * @ClassName: BeanUtils * @Description: * @Author: songwp * @Date: 9:02 2022/ ...
- Java内省详解
内省和反射有什么区别: 反射式在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态. 内省机制是通过反射来实现的,BeanIn ...
随机推荐
- css整理-06 表和列表
表格式化 表布局 table, display:table caption, display: table-caption thead, display: table-header-group tbo ...
- 前端学PHP之文件操作(认真读读)
前面的话 在程序运行时,程序本身和数据一般都存在内存中,当程序运行结束后,存放在内存中的数据被释放.如果需要长期保存程序运行所需的原始数据,或程序运行产生的结果,就需要把数据存储在文件或数据库.一般地 ...
- Python小例子(判断质数)
只能被自己或者1整除的数为质数 num = int(input('请输入一个数:')) if num > 1: # 查看因子 for i in range(2, num): if (num % ...
- UI中经常出现的下拉框下拉自动筛选效果的实现
小需求是当你在第一个下拉框选择了国家时,会自动更新第二个省份的下拉框,效果如下 两个下拉选择Html如下: <select id="country_select"> & ...
- 并查集(加权) LA 4487 Exclusive-OR
题目传送门 题意:训练指南P245 分析:首先这道是经典的并查集题目,利用异或的性质.异或性质:x ^ 0 = x -> a ^ a = 0 -> x ^ a ^ a = x,即一个数对某 ...
- HDOJ-1002
用java写大数,感觉就是BUG import java.math.*; import java.io.*; import java.util.*; public class Main{ public ...
- Linux下使用vim的tips
1.如果用户不能确定vi所处的状态,可以按Esc键两次返回初始状态. 2.Fedora 17下安装与配置ssh:http://blog.csdn.net/ashuai81/article/detail ...
- Universal JS module loader
With dependency ;(function (root, factory) { if (typeof define === 'function' && define.amd) ...
- 争夺 & KM思想
题意: 给一张二分图,每个点与两个特定点又一条边相连,边权非负,让你给这个二分图每个点一个顶标,让每一条边两端顶标和大于等于这条边.求出最小顶标和. 这当然是翻译过的题目... 原题: 小Y和小P无聊 ...
- Android作业分组与选题
期末大作业 序号 题目 组员分工 完成度 1 基于安卓系统的游戏开发 2 设计一个安卓手机小游戏 3 Android平台应用——音乐播放器设计 4 基于Android技术的个人博客 5 电子阅读器 6 ...