使用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 ...
随机推荐
- 《DSP using MATLAB》示例Example5.3
- vim使用02
编辑 剪切光标所在的字符: <x>; 剪切并插入: <s> 撤销操作:撤销至上一个命令之间的修改: <u> 恢复上一次撤销操作: <C r> 剪切光标所 ...
- POJ1201 Intervals差分约束系统(最短路)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- js小例子(简单模糊匹配输入信息)
该例子实现的是用户输入信息或者字母时可以搜索出来,鼠标点击选择 <!DOCTYPE html> <html> <style> p{ width:200px; hei ...
- CSS3实现动画
CSS3实现一个简单的动画 可以改变任意多的样式任意多的次数,用百分比来规定变化发生的时间,或用关键词 "from" 和 "to",等同于 0% 和 100%. ...
- http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html
http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html
- 内网配置DNS服务器,无域名,只有主机名
Hadoop集群中,使用DNS而不是hosts来访问服务器. 1. 安装bind软件 用root用户运行: yum -y install bind* 2. 配置named.conf文件 vi /etc ...
- 提高安全性而在HTTP响应头中可以使用的各种响应头字段
本文介绍在Web服务器做出响应时,为了提高安全性而在HTTP响应头中可以使用的各种响应头字段.由于部分浏览器中有可能对某些字段或选项不提供支持,所以在使用这些字段时请先确认客户端环境. X-Frame ...
- HTTPS, SPDY和 HTTP/2性能的简单对比
中文原文:HTTPS, SPDY和 HTTP/2性能的简单对比 整理自:A Simple Performance Comparison of HTTPS, SPDY and HTTP/2 请尊重版权, ...
- 偶然的发现(与Code无关)
最近做后台用户注册, 在考虑不使用验证码, 百度搜了一下看了看一些相关技术, 发现了个小说——[万恶的验证码], 看了挺搞笑分享一下:原文链接 万恶的验证码 前言: 传说中,它是最为邪恶的吸血鬼,它是 ...