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共用。

  1 import java.lang.reflect.InvocationTargetException;
  2 import java.text.ParseException;
  3 import java.text.SimpleDateFormat;
  4 import java.util.Date;
  5 import java.util.HashMap;
  6 import java.util.Map;
  7
  8 import org.apache.commons.beanutils.BeanUtils;
  9 import org.apache.commons.beanutils.ConversionException;
 10 import org.apache.commons.beanutils.ConvertUtils;
 11 import org.apache.commons.beanutils.Converter;
 12 import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
 13 import org.junit.Test;
 14
 15
 16
 17 //使用beanUtils操纵bean属性(第三方)
 18 public class Demo {
 19
 20     /**
 21      *
 22      * @throws Exception
 23      *
 24      */
 25     @Test
 26     public void test1() throws  Exception{
 27
 28         Person p = new Person();
 29
 30         BeanUtils.setProperty(p, "name", "zero");
 31
 32         System.out.println(p.getName());
 33
 34     }
 35
 36     @Test
 37     public void test2() throws  Exception{
 38
 39         String name = "aaaa";
 40         String password = "123";
 41         String age = "34";
 42
 43         Person p = new Person();
 44
 45         BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
 46         BeanUtils.setProperty(p, "password", password);
 47         BeanUtils.setProperty(p, "age", age);
 48
 49         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge());
 50
 51     }
 52
 53     @Test
 54     public void test3() throws  Exception{
 55
 56         String name = "aaaa";
 57         String password = "123";
 58         String age = "34";
 59         String birthday = "1900-1-1";
 60
 61         //为了让日期赋到bean的birthday属性上,给beanUtils注册一个日期转换
 62         ConvertUtils.register(new Converter(){
 63
 64             @Override
 65             public Object convert(Class type, Object value) {
 66
 67                 if(value==null){
 68                     return null;
 69                 }
 70                 if(!(value instanceof String)){
 71                     throw new ConversionException("only support String");
 72                 }
 73
 74                 String str = (String) value;
 75
 76                 if(str.trim().equals("")){
 77                     return null;
 78                 }
 79
 80                 SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
 81
 82                 try {
 83                     return df.parse(str);
 84                 } catch (ParseException e) {
 85                     throw new RuntimeException(e);
 86                 }
 87
 88             }
 89
 90         },Date.class);
 91
 92         Person p = new Person();
 93
 94         BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
 95         BeanUtils.setProperty(p, "password", password);
 96         BeanUtils.setProperty(p, "age", age);
 97         BeanUtils.setProperty(p, "birthday", birthday);
 98
 99         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday());
100
101     }
102
103     @Test
104     public void test4() throws  Exception{
105
106         String name = "aaaa";
107         String password = "123";
108         String age = "34";
109         String birthday = "1998-1-1";
110
111         ConvertUtils.register(new DateLocaleConverter(), Date.class);
112
113         Person p = new Person();
114
115         BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
116         BeanUtils.setProperty(p, "password", password);
117         BeanUtils.setProperty(p, "age", age);
118         BeanUtils.setProperty(p, "birthday", birthday);
119
120         Date date = p.getBirthday();
121
122         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+date.toLocaleString());
123
124     }
125
126     @Test
127     public void test5() throws Exception{
128
129         Map map = new HashMap();
130         map.put("name", "zero");
131         map.put("password", "521212");
132         map.put("age", "33");
133         map.put("birthday", "1998-1-1");
134
135         ConvertUtils.register(new DateLocaleConverter(), Date.class);
136
137         Person p = new Person();
138
139         BeanUtils.populate(p, map);//用map集合的值,填充bean的属性值
140
141         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday());
142
143     }
144
145 }

View Code

 1 import java.util.Date;
 2
 3
 4 public class Person {// javabean
 5
 6     private String name;
 7     private String password;
 8     private int age;
 9     private Date birthday;
10
11     public Date getBirthday() {
12         return birthday;
13     }
14
15     public void setBirthday(Date birthday) {
16         this.birthday = birthday;
17     }
18
19     public String getName() {
20         return name;
21     }
22
23     public void setName(String name) {
24         this.name = name;
25     }
26
27     public String getPassword() {
28         return password;
29     }
30
31     public void setPassword(String password) {
32         this.password = password;
33     }
34
35     public int getAge() {
36         return age;
37     }
38
39     public void setAge(int age) {
40         this.age = age;
41     }
42
43     public String getAb(){
44         return null;
45     }
46
47
48 }

View Code

转载于:https://www.cnblogs.com/aineko/p/3793489.html

使用beanUtils操纵javabean相关推荐

  1. 使用beanUtils操纵bean的属性

    注意:使用beanUtils操纵bean的属性时需要使用到的包有:commons-beanutils-1.8.3.jar     commons-logging-1.1.1.jar 创建Demo1类来 ...

  2. [新手学Java]使用beanUtils控制javabean

    使用BeanUtils设置/读取属性的值以及默认支持的自动转化: @Test //使用BeanUtils设置/读取属性的值以及自动转化 public void test1() throws Illeg ...

  3. beanUtils操作bean的属性

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

  4. javaweb笔记(方立勋)

    1 Day01 02-eclipse使用和程序的断点调试 1.1 Eclipse的使用 工作空间目录是纯英文不带空格的路径 在eclipse下Java程序的编写和运行,及java运行环境的配置. 新建 ...

  5. java内省_java内省机制

    一.内省是什么.实现方式: 内省(Introspector)是Java语言对Bean类属性.事件的一种缺省处理方法. 例如类A中有属性name,那我们可以通过getName,setName来得到其值或 ...

  6. java内省的意思,java内省机制 + 内省是什么 + 内省实现方式 + 和反射的区别

    见:https://zhidao.baidu.com/question/434288330.html.http://blog.csdn.net/u014394715/article/details/5 ...

  7. spring beans源码解读之--总结篇

    spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类. org.springframework.beans.ann ...

  8. java 反射 proper_JAVA提高四:反射基本应用

    在前面一节<http://www.cnblogs.com/pony1223/p/7659210.html>,我们学习了JAVA的反射的相关知识,那么本节我们对前面所学习的知识做一个应用相关 ...

  9. java内省的意思_java内省和反射的区别

    展开全部 经过多方面的资料搜集整理,写下了这篇文章,本文主要讲解java的反射和内e68a843231313335323631343130323136353331333363366237省机制,希望对 ...

最新文章

  1. 余承东没有吹牛:华为首次超越三星,成为全球最大手机供应商
  2. Android开发中adb命令的常用方法
  3. 建立唯一索引后mysql策略_【MySQL】MySQL索引背后的之使用策略及优化【转】
  4. “我用 72 小时复刻了一个 ClubHouse”
  5. 人工智能中国专利技术分析报告发布,百度三年蝉联榜首
  6. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结
  7. JSK-23 计数和数数【数列】
  8. 155款安卓开源项目源码整理,总有你要找的(精心收集)
  9. IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
  10. 周星驰vs韩寒vs宁浩…Python告诉你春节该看哪部电影
  11. Day3 函数 参数 变量 递归——python学习之路
  12. RGB渐变色与HSL渐变色
  13. LTE网络中的无线安全的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  14. 智源首席科学家孙茂松当选欧洲科学院外籍院士
  15. 用Python求解线性规划问题
  16. 寄存器、缓存、内存、硬盘、存储器的理解
  17. 第十三届蓝桥杯(Web 应用开发)线上模拟赛【第三题】(封装函数实现个人所得税计算器)
  18. 抓包工具httpbuger的使用问题
  19. js中appendChild()用法
  20. 蓝桥杯 ADV-201 算法提高 我们的征途是星辰大海

热门文章

  1. 校园送礼风为何难刹住 “送了没个完,不送没个底”
  2. RS232串口交叉直连
  3. 利用vc的mfc做的Excel表格处理工具
  4. Java三大特性: 封装、继承、多态
  5. 038_JDK的Iterable接口
  6. eclipse未能识别我的手机
  7. 算术运算符举例java_Java的算术运算符简介
  8. jq ajax异步上传图片插件,jQuery异步上传文件插件ajaxFileUpload详细介绍
  9. php 清除数据表中所有数据库,清除一个数据库里所有表的数据
  10. Spring的AOP思想和实现AOP思想的框架AspectJ