当前位置: 首页 > news >正文

32_使用BeanUtils工具包操作JavaBean

 

由于对属性设置值和得到值的需求很多,使用频率很高,所以有一些开源勇士 不满足于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设值时为
        System.out.println(rf1.getX());
        BeanUtils.setProperty(rf1, "x",1); //原来类型 设值
         BeanUtils.(rf1, "x","2"); //支持String类型 设值
         System.out.println(rf1.getX());
        
        BeanUtils.setProperty(rf1, "birthday.time", "111");//支持属性级联操作
         System.out.println(BeanUtils.(rf1, "birthday.time").getClass().getName());//java.lang.String 111
        Map mm=BeanUtils.(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.(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
    }

}
开始做,坚持做,重复做

相关文章:

  • 常用HTTP状态码
  • 怎样将U盘设置成只读属性
  • Sum、if、mod隔列求和
  • 有关android 应用的plugin框架调研
  • 数据结构之查找(php代码实现)
  • redis常用命令
  • (太强大了) - Linux 性能监控、测试、优化工具
  • Centos 源代码以及编译过程
  • PHP_NoteBook
  • 利用mongo profile 分析mongo慢查询
  • GlusterFS源代码解析 —— GlusterFS 简单介绍
  • rem是什么?
  • SYSOPER和SYSDBA特权的区别与联系
  • 混沌的艺术--- YChaos通过数学公式生成混沌图像
  • 高维数据的重要属性
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • 「译」Node.js Streams 基础
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 0x05 Python数据分析,Anaconda八斩刀
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Android Volley源码解析
  • centos安装java运行环境jdk+tomcat
  • C学习-枚举(九)
  • exif信息对照
  • Java程序员幽默爆笑锦集
  • Python3爬取英雄联盟英雄皮肤大图
  • python学习笔记-类对象的信息
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • Vultr 教程目录
  • 爱情 北京女病人
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 给新手的新浪微博 SDK 集成教程【一】
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 你真的知道 == 和 equals 的区别吗?
  • 前端性能优化——回流与重绘
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • !$boo在php中什么意思,php前戏
  • ###项目技术发展史
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (1)常见O(n^2)排序算法解析
  • (WSI分类)WSI分类文献小综述 2024
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (独孤九剑)--文件系统
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (数据结构)顺序表的定义
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (正则)提取页面里的img标签
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • .bashrc在哪里,alias妙用