一.BeanUtils 概述
     BeanUtils 是阿帕奇提供的一套专门用于将一些数据封装到java对象中的工具类;
    
     名词:javaBean:特定格式的java类称为javaBean;
    
     要求:
         1:javaBean这个类要实现Serializable接口;(在实际开发中,通常省略了)
         2:javaBean必须有public权限的空参数的构造方法;
         3:javaBean必须有属性对应的getXxx与setter方法;

二.BeanUtils 的使用
     Beanutils 有2个依赖jar包;commons-beanutils-1.8.3.jar和commons-logging-1.1.1.jar;
     BeanUtils 有2个核心类:BeanUtils,ConvertUtils 类;
     使用步骤:
         1:下载解压;
         2:复制核心jar包到工程中;(有2个)
         3:添加到本地;(add to build path)
         4:使用核心类;

三.BeanUtils 常用方法
     public static void setProperty(Object bean,String name,Object value)
                             throws IllegalAccessException,InvocationTargetException{}:向bean对象的name属性中保存value值;
     public static String getProperty(Object bean,String name)
                               throws IllegalAccessException,InvocationTargetException,NoSuchMethodException{}:从bean对象中获取name属性的值;
     public static String[] getArrayProperty(Object bean,String name)
                                      throws IllegalAccessException,InvocationTargetException,NoSuchMethodException{}:从bean对象中获取name属性的数组类型的值;
     [注:getProperty方法就只认String类型和String[]数组类型,其它类型它会自动帮你转成这两个类型,使用时需时刻想到String类型,用""包裹属性]
     public static void populate(Object bean,Map properties)
                                     throws IllegalAccessException,InvocationTargetException{}:将properties集合中的数据,根据key与bean的属性名(实际上是匹配setXxx方法)    匹配,匹配成功,则赋值,匹配失败不操作;                                                   

代码演示1:(以下代码全在Eclipse中实现)

  1 //创建beanUtilsDemo01包
2 package beanUtilsDemo01;
3
4 import java.util.Arrays;
5
6 public class Person {
7 // 属性
8 private String name;
9 private int age;
10 private String[] hobby;
11
12 // 构造方法
13 public Person() {
14 super();
15 }
16
17 public Person(String name, int age, String[] hobby) {
18 super();
19 this.name = name;
20 this.age = age;
21 this.hobby = hobby;
22 }
23
24 // getter/setter
25 public String getName() {
26 return name;
27 }
28
29 public void setName(String name) {
30 this.name = name;
31 }
32
33 public int getAge() {
34 return age;
35 }
36
37 public void setAge(int age) {
38 this.age = age;
39 }
40
41 public String[] getHobby() {
42 return hobby;
43 }
44
45 public void setHobby(String[] hobby) {
46 this.hobby = hobby;
47 }
48
49 // 覆写toString/equal/hashcode
50 @Override
51 public String toString() {
52 return "Person [name=" + name + ", age=" + age + ", hobby="
53 + Arrays.toString(hobby) + "]";
54 }
55
56 @Override
57 public int hashCode() {
58 final int prime = 31;
59 int result = 1;
60 result = prime * result + age;
61 result = prime * result + Arrays.hashCode(hobby);
62 result = prime * result + ((name == null) ? 0 : name.hashCode());
63 return result;
64 }
65
66 @Override
67 public boolean equals(Object obj) {
68 if (this == obj) {
69 return true;
70 }
71 if (obj == null) {
72 return false;
73 }
74 if (!(obj instanceof Person)) {
75 return false;
76 }
77 Person other = (Person) obj;
78 if (age != other.age) {
79 return false;
80 }
81 if (!Arrays.equals(hobby, other.hobby)) {
82 return false;
83 }
84 if (name == null) {
85 if (other.name != null) {
86 return false;
87 }
88 } else if (!name.equals(other.name)) {
89 return false;
90 }
91 return true;
92 }
93
94 }
95 //创建beanUtilsDemo01包
96 package beanUtilsDemo01;
97
98 import java.util.Arrays;
99
100 import org.apache.commons.beanutils.BeanUtils;
101
102 //BeanUtils常用方法练习
103
104 public class Demo01BeanUtils {
105
106 public static void main(String[] args) throws Exception {
107 // 实例化对象
108 Person p = new Person();
109 // 借用BeanUtils工具类向Person对象赋值
110 BeanUtils.setProperty(p, "name", "Rose");
111 BeanUtils.setProperty(p, "age", 22);
112 BeanUtils.setProperty(p, "hobby", new String[] { "eating", "sleeping",
113 "kissing" });
114 // 打印对象
115 System.out.println(p);
116 // 获取各属性值
117 String[] hobby = BeanUtils.getArrayProperty(p, "hobby");
118 System.out.println(Arrays.toString(hobby));
119 String name = BeanUtils.getProperty(p, "name");
120 System.out.println(name);
121 String age = BeanUtils.getProperty(p, "age");
122 System.out.println(age);
123 }
124
125 }
126

代码演示2:封装map集合中的数据

  1 package beanUtilsDemo01;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.apache.commons.beanutils.BeanUtils;
8
9 //借用BeanUtils将Map中的数据封装到javabean中
10
11 public class Demo02BeanUtils {
12
13 public static void main(String[] args) throws IllegalAccessException,
14 InvocationTargetException {
15 // 实例化对象
16 Person p = new Person();
17 // 准备MAP集合
18 Map<String, Object> map = new HashMap<>();
19 // 向map中添加数据
20 map.put("name", "jack");
21 map.put("age", 23);
22 map.put("hobbyy", new String[] { "eating", "sleeping", "painting" });
23 // 将map集合中的数据封装到javabean中
24 BeanUtils.populate(p, map);
25 System.out.println(p);
26 }
27 }
28

代码演示3:与以上利用同一个Person类????????????????????????

  1 package beanUtilsDemo01;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.apache.commons.beanutils.BeanUtils;
7
8 //利用BeanUtils工具类自定义工具类:要求传入任一类型的字节码文件 和 属性的map集合,返回实例化对象
9 class MyBeanUtils {
10 public static <T> T popu(Class<T> c, Map map) throws Exception { //泛型
11 Object obj = c.newInstance();
12 BeanUtils.populate(obj, map);
13 return (T) obj; //向下转型
14 }
15 }
16
17 public class MyTest {
18 public static void main(String[] args) throws Exception {
19 Map<String, Object> map = new HashMap<>();
20 map.put("name", "rose");
21 map.put("age", "18");
22 Person p = MyBeanUtils.popu(Person.class, map);
23 System.out.println(p);
24 }
25
26 }
27

代码演示4:需准备一个User类,和以上的Person类,及data.xml文件

  1 package beanutilcase;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.apache.commons.beanutils.BeanUtils;
8 import org.dom4j.Document;
9 import org.dom4j.Element;
10 import org.dom4j.io.SAXReader;
11
12 public class Demo {
13
14 public static void main(String[] args) throws Exception {
15 Person p = new Person();
16 User u = new User();
17 // 创建解析器对象
18 SAXReader sax = new SAXReader();
19 // 读取文档,并获取根节点
20 Document doc = sax.read("data.xml");
21 Element root = doc.getRootElement();
22 // 获取根节点下的一级子元素
23 List<Element> listFirst = root.elements();
24 // 迭代
25 for (Element e : listFirst) {
26 // 获取一级子元素的属性值
27 String path = e.attributeValue("className");
28 // 根据路径(属性)获取字节码文件
29 Class c = Class.forName(path);
30 // 获取二级子元素
31 List<Element> listSecond = e.elements();
32 // 定义map集合装属性值
33 Map<String, Object> map = new HashMap<>();
34 for (Element es : listSecond) {
35 // 获取二级子元素的两个属性值
36 String name = es.attributeValue("name");
37 String value = es.attributeValue("value");
38 map.put(name, value);
39 }
40 // 利用beanutils工具类进行封装
41 // 判断是否为person
42 if (path.matches(".*Person$")) {
43 BeanUtils.populate(p, map);
44 } else {
45 BeanUtils.populate(u, map);
46 }
47 }
48 System.out.println(p);
49 System.out.println(u);
50 }
51
52 }
53

BeanUtils 工具类的更多相关文章

  1. 第13天 JSTL标签、MVC设计模式、BeanUtils工具类

    第13天 JSTL标签.MVC设计模式.BeanUtils工具类 目录 1.    JSTL的核心标签库使用必须会使用    1 1.1.    c:if标签    1 1.2.    c:choos ...

  2. 利用BeanUtils工具类封装表单数据

    一.BeanUtils工具类的使用 1.首先导入BeanUtils工具类的jar包 commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar 2.se ...

  3. JavaWeb 之 BeanUtils 工具类

    在上一个用户登录案例中,当从浏览器接收参数后,还需要创建 JavaBean 类,对其的属性每一项赋值,如果属性少,可以手动完成,但是当属性非常多,这时就发现非常不方便,在这里提供一个可以封装 Java ...

  4. JDBC--使用beanutils工具类操作JavaBean

    1.在JavaEE中,Java类的属性通过getter,setter来定义: 2.可使用BeanUtils工具包来操作Java类的属性: --Beanutils是由Apache公司开发,能够方便对Be ...

  5. BeanUtils工具类copyProperties方法缺点及解决

    使用类为spring-beans:4.3.13release包中的 org.springframework.beans.BeanUtils BeanUtils.copyProperties(Objec ...

  6. 丢弃掉那些BeanUtils工具类吧,MapStruct真香!!!

    在前几天的文章<为什么阿里巴巴禁止使用Apache Beanutils进行属性的copy?>中,我曾经对几款属性拷贝的工具类进行了对比. 然后在评论区有些读者反馈说MapStruct才是真 ...

  7. 使用BeanUtils工具类操作Java bean

    1.类的属性: 1).在Java EE中,类的属性通过setter和getter定义:类中的setter(getter)方法去除set(get)后剩余的部分就是类的属性 2).而之前叫的类的属性,即成 ...

  8. 内省(二)之BeanUtils工具类

    上一篇内省(Introspector)讲到的是采用JavaAPI中的类来操作bean及其属性,而Apache也开源了第三方框架来简化和丰富了对bean属性的操作,这个框架就是BeanUtils. 使用 ...

  9. BeanUtils工具类

    用对象传参,用JavaBean传参. BeanUtils可以优化传参过程. 学习框架之后,BeanUtils的功能都由框架来完成. 一.为什么用BeanUtils? 每次我们的函数都要传递很多参数很麻 ...

随机推荐

  1. ubuntu bcompare 安装

    Terminal Install wget http://www.scootersoftware.com/bcompare-4.2.8.23479_amd64.deb sudo apt-get upd ...

  2. 查看Ubuntu的版本

    方法一: cat /etc/issue 方法二: lsb_release -a

  3. hibernate学习三 精解Hibernate之核心文件

    一 hibernate.cfg.xml详解 1 JDBC连接: 2 配置C3P0连接池: 3 配置JNDI数据源: 4 可选的配置属性: 5 hibernate二级缓存属性 6 hibernate事务 ...

  4. 3.19 YARN HA架构及(RM/NM) Restart讲解

    一.ResourceManager HA ResourceManager(RM)负责跟踪集群中的资源,以及调度应用程序(例如,MapReduce作业). 在Hadoop 2.4之前,ResourceM ...

  5. SPOJ CIRU The area of the union of circles (计算几何)

    题意:求 m 个圆的并的面积. 析:就是一个板子题,还有要注意圆的半径为0的情况. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...

  6. 使用pip安装第三方插件

    1. 下载Settools和pip,并安装 a. 下载地址: setuptools : https://pypi.python.org/pypi/setuptools#downloadspip: ht ...

  7. iOS 中使用 MJExtension 遇到 关键字(id) 怎么办

    MJExtension 是个人比较喜欢用的json 转model 的软件,当遇到系统关键字时就会出现崩溃,解决方式如下 1.建立Modle 解析类,服务返回数据中带有id,这个时候用字典转Mode(m ...

  8. layui常用功能

    包含的主要样式: 验证不通过时的弹窗 弹窗修改信息 询问框(是否删除之类的) 操作成功提示.操作失败提示 加载样式(显示加载层) 文件下载请前往github over!over!over!

  9. 数据结构关于AOV与AOE网的区别

    AOV网,顶点表示活动,弧表示活动间的优先关系的有向图. 即如果a->b,那么a是b的先决条件. AOE网,边表示活动,是一个带权的有向无环图, 其中顶点表示事件,弧表示活动,权表示活动持续时间 ...

  10. 719. Find K-th Smallest Pair Distance

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...