由于对属性设置值和得到值的需求很多,使用频率很高,所以有一些开源勇士 不满足于JavaBean API 中IntroSpector来操作bean,

写出来了通用的BeanUtils工具,来进一步简化对java bean的操作,并开源放在apache网站上提供免费下载。

 

Beanutils工具包

  • 演示用eclipse如何加入jar包,先只是引入beanutils包,等程序运行出错后再引入logging包。
1 commons-beanutils-1.9.2-bin.zip  http://u2l.info/3VN80n
2 commons-beanutils-1.9.2-src.zip  http://u2l.info/3o99D3
3 commons-logging-1.1.3-bin.zip   http://u2l.info/2D1d0m
4 commons-logging-1.1.3-src.zip   http://u2l.info/nKLKp

 

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 缺少logging的jar包

这个日志包,很多框架都在用。

  • 在前面内省例子的基础上,用BeanUtils类先get原来设置好的属性,再将其set为一个新值。
    • get属性时返回的结果为字符串,set属性时可以接受任意类型的对象,通常使用字符串。

                  这非常适合浏览器传过来的字符串对对象进行set。

  • 用PropertyUtils类先get原来设置好的属性,再将其set为一个新值。
    • get属性时返回的结果为该属性本来的类型,set属性时只接受该属性本来的类型。

java bean

package com.itcast.day1;

import java.util.Date;

public class ReflectPoint {
private Date birthday=new Date(); private int x;
public int y;
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} @Override
public String toString() {
return "ReflectPoint [birthday=" + birthday + ", x=" + x + ", y=" + y
+ "]";
} }

测试类:

package com.itcast.day2;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils; import com.itcast.day1.ReflectPoint;
public class IntroSpectorTest { public static void main(String[] args) throws Exception{ ReflectPoint rf1=new ReflectPoint(3,4); Object value=6;
System.out.println(BeanUtils.getProperty(rf1, "x").getClass().getName());// x是int,但是用beanutils设值时为java.lang.String
System.out.println(rf1.getX());
BeanUtils.setProperty(rf1, "x",1); //原来类型 设值
BeanUtils.setProperty(rf1, "x","2"); //支持String类型 设值
System.out.println(rf1.getX()); BeanUtils.setProperty(rf1, "birthday.time", "111");//支持属性级联操作
System.out.println(BeanUtils.getProperty(rf1, "birthday.time").getClass().getName());//java.lang.String 111
Map mm=BeanUtils.describe(rf1);//javabean转成map
System.out.println(rf1);//ReflectPoint [birthday=Thu Jan 01 08:00:00 GMT+08:00 1970, x=2, y=4] Map map=new HashMap();
map.put("x", 1);
map.put("y",1);
BeanUtils.populate(rf1, map);//把map转换成javabean
System.out.println(rf1);//ReflectPoint [birthday=Thu Jan 01 08:00:00 GMT+08:00 1970, x=1, y=1] //PropertyUtils.setProperty(rf1, "x", "9");//运行出错!因为PropertyUtils只支持原来的类型,这点没有BeanUtils强大!
PropertyUtils.setProperty(rf1, "x", 9);
System.out.println(rf1.getX());//9
} }

32_使用BeanUtils工具包操作JavaBean的更多相关文章

  1. java高新技术-操作javaBean

    1. 对javaBean的简单内省操作 public class IntroSpectorTest { public static void main(String[] args) throws Ex ...

  2. 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性

      本文转载自:http://blog.sina.com.cn/s/blog_5d65a16901011kom.html 关键字:内省.JavaBean.PropertyDescriptor类.Int ...

  3. java 内省综合案例和Beanutils工具包

    演示用eclipse自动生成 ReflectPoint类的setter和getter方法. 直接new一个PropertyDescriptor对象的方式来让大家了解JavaBean API的价值,先用 ...

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

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

  5. JavaWeb -- 内省—beanutils工具包 的使用

    Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写. Beanu ...

  6. Commons BeanUtils工具包

    简介: BeanUtils工具包是由Apache公司所开发,提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理. 我们知道,一个JavaBean通常包含了大 ...

  7. 内省—beanutils工具包

    Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写. BeanU ...

  8. javaweb学习总结五(内省、beanUtils工具包)

    一:内省的概念 1:内省是反射的一种特例,由于在反射中频繁的操作javabean,所以为了方便反射 javabean,sun公司开发出一套API提高效率. 2:javaBean,就是用来封装客户端请求 ...

  9. 使用内省的方式操作JavaBean

    import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; im ...

随机推荐

  1. CentOS部署pyspider

    0x00 环境 阿里云ECS云服务器 CPU:1核 内存:2G 操作系统:Centos 7.3 x64 地域:华北 2(华北 2 可用区 A) 系统盘:40G 0x01 安装依赖 yum instal ...

  2. 从零开始学 Web 之 jQuery(八)each,多库共存,包装集,插件

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  3. byte数组转float 以及byte转其他类型时为什么要&0xff

    static final char[] HEX_CHARS = "0123456789abcdef".toCharArray(); //转换为十六进制 public static ...

  4. nginx介绍(四) - 反向代理

    前言 前面虚拟主机的部分, 发现我所有的修改, 都是对 nginx 目录下, nginx.conf 和 html 文件夹的操作. 我的最终目的, 是映射到不同电脑的tomcat里面去啊, 操作这里的h ...

  5. mysql计算两个日期相差的天数

    DATEDIFF() 函数可以返回两个日期之间的天数. 如下: SELECT DATEDIFF('2015-06-29','2015-06-12') AS DiffDate 结果得17 SELECT ...

  6. Tomcat8源码笔记(二)Bootstrap启动

    TOMCAT源码调试入口是Bootstrap类的main方法,我的启动参数VM: -Dcatalina.home=E:/Tomcat_Source_Code/apache-tomcat-8.0.53- ...

  7. python——文件读写

    open()函数用法: file-object = open(file_name, access_mode = ' ', buffering = -1) filename可以是相对路径或绝对路径,ac ...

  8. Spark新手入门——3.Spark集群(standalone模式)安装

    主要包括以下三部分,本文为第三部分: 一. Scala环境准备 查看二. Hadoop集群(伪分布模式)安装 查看三. Spark集群(standalone模式)安装 Spark集群(standalo ...

  9. Oracle 连接到RMAN

    set oracle_sid=orcl rman connect target sys/password@orcl;

  10. 【Tomcat】详解tomcat的连接数与线程池

    前言 在使用tomcat时,经常会遇到连接数.线程数之类的配置问题,要真正理解这些概念,必须先了解Tomcat的连接器(Connector). Connector的主要功能,是接收连接请求,创建Req ...