由于对属性设置值和得到值的需求很多,使用频率很高,所以有一些开源勇士 不满足于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. Maven项目打包成可执行Jar文件

    在使用Maven完成项目以后,如果需要打包成可执行的Jar文件,我们通过eclipse的导出很麻烦,还得指定入口文件的位置,还得说明依赖的jar包,既然都使用Maven了,很重要的一个目的就是让这些繁 ...

  2. Delphi常用快捷键

    delphi是我学编程时的入门语言,用过一年多的时光,个人对它还是挺喜欢的.现在用的少了,一些快捷键和语法也有些遗忘了,这里对delphi的快捷键做个总结,留个纪念.嘿嘿,不知道还有多少人还用着这门语 ...

  3. PM2来部署nodejs服务器永久开启

    pm2 日常使用   1. pm2 是什么? 日常开发中需要启动一个node项目,需要用npm run …,,如果终端被关掉,程序也就自动停止,有时候几个项目一起跑起来,好几个终端开着,个人不太喜欢, ...

  4. OpenCV两种畸变校正模型源代码分析以及CUDA实现

    图像算法中会经常用到摄像机的畸变校正,有必要总结分析OpenCV中畸变校正方法,其中包括普通针孔相机模型和鱼眼相机模型fisheye两种畸变校正方法. 普通相机模型畸变校正函数针对OpenCV中的cv ...

  5. java界面设计(swing)

    1.Swing基本组件 窗体控件 JFrame.容器控件 JPanel .标签控件 JLabe.按钮控件 JButton.文本框控件 JTextField 与 JTextArea(注意JScrollP ...

  6. windows BLE编程 net winform 连接蓝牙4.0

    winform 程序调用Windows.Devices.Bluetoot API 实现windows下BLE蓝牙设备自动连接,收发数据功能.不需要使用win10的UWP开发. 先贴图,回头来完善代码 ...

  7. dotnet 从入门到放弃的 500 篇文章合集

    本文是记录我从入门到放弃写的博客 博客包括 C#.WPF.UWP.dotnet core .git 和 VisualStudio 和一些算法,所有博客使用 docx 保存 下载:dotnet 从入门到 ...

  8. groovy使用范型的坑

    java的范型 Map<String, Integer> map = new HashMap<>(); map.put("a", 100); map.put ...

  9. Matlab 根号的输入

    二次根号: sqrt(a)或a^0.5 三次根号: x^(1/3)或者x.^(1/3) 根据x的数据结构类型 矩阵.数组需要.^

  10. IDEA maven 项目如何上传到私服仓库

    前言:idea maven 发布版本到私服(快照和正式版).我有个项目( jar 包源码),其他 maven 项目能直接引入依赖就最好了,所以必须将这个 jar 包源码发布到 maven 私服仓库里去 ...