BeanUtils基本简介

15 篇文章 0 订阅

1.什么是BeanUtils:

BeanUtils主要提供了对于JavaBean进行各种操作。 

2.BeanUtils的作用:

在一般的写bean组件的时候,都必须要写setter和getter方法,当然假如我们事先已经知道bean的相关属性和方法,

写bean是比较简单的,但是组件太多的时候,重复编写经常是枯燥乏味令人厌烦的。但当有些时候我么需要调用动

态对象的属性的时候,我们应该怎么来设定和获取对象的属性呢?BeanUtils就可以帮助我们来解决这个问题。它需

要Jakarta-Commons -Collections包和Jakarta-Commons -logging包的支持。


3. org.apache.commons.beanutils:

这个包主要提供用于操作JavaBean的工具类,Jakarta-Common-BeanUtils的主要功能都在这个包里实现。

BeanUtils可以直接get和set一个属性的值。它将property分成3种类型:

       1Simple——简单类型,如Stirng、Int……

     (对于Simple类型,第二个参数直接是属性名即可,详见代码)

       2Indexed——索引类型,如 数组、arrayList……

     (对于Indexed,则为“属性名[索引值]”,注意这里对于ArrayList和数组都可以用一样的方式进行操作,详见代码)


       3Maped——这个不用说也该知道,就是指Map,比如HashMap……

     (对于Map类型,则需要以“属性名(key值)”的形式,详见代码)

    访问不同类型的数据可以直接调用函数getProperty和setProperty。它们都只有2个参数,第一个是JavaBean对象,第二个是要操作的属性名。

4.Converter 把Request或ResultSet中的字符串绑定到对象的属性 

   经常要从request,resultSet等对象取出值来赋入bean中,如果不用MVC框架的绑定功能的话,下面的代码谁都写腻了。

String a = request.getParameter("a");   bean.setA(a);  
String b = request.getParameter("b");  bean.setB(b);

不妨写一个Binder自动绑定所有属性:

MyBean bean = ;

        HashMap map = new HashMap();

        Enumeration names = request.getParameterNames();

        while (names.hasMoreElements())
        {

          String name = (String) names.nextElement();

          map.put(name, request.getParameterValues(name));

        }

     BeanUtils.populate(bean, map);
 

 其中BeanUtils的populate方法(Struts用于将一个map的值填充到一个bean中)或者getProperty,setProperty方法其

实都会调用convert进行转换。

     但Converter只支持一些基本的类型,甚至连java.util.Date类型也不支持。而且它比较笨的一个地方是当遇到不认

识的类型时,居然会抛出异常来。 对于Date类型,我参考它的sqldate类型实现了一个Converter,而且添加了一个设

置日期格式的函数。

要把这个Converter注册,需要如下语句:

//因为要注册converter,所以不能再使用BeanUtils的静态方法了,必须创建BeanUtilsBean实例:
BeanUtilsBean beanUtils =
            new BeanUtilsBean( convertUtils, new PropertyUtilsBean() ) ;

            beanUtils.setProperty( bean, name, value ) ;



5.BeanUtils在Struts中的应用:

下面的代码是我们经常在Struts中为了获得浏览器带来的参数的方法:

DynaActionForm daf = (DynaActionForm)form ;

        User user = new User() ;
        user.setId( daf.getString( "id" ) ) ;
        user.setPassword( daf.getString( "password" ) ) ;
        user.setUsername( daf.getString( "password" ) ) ;
        …
是不是觉得每次写这些都是在重复造轮子呢?

在看看用BeanUtils写的:

BeanUtils.copyProperties( user, daf ) ;

就是这么简单

6.实例

public class Test {

    private String name; 

    private HashMap address = new HashMap(); 

    private String[] otherInfo; 

    private ArrayList product; 

    private ArrayList employee; 

    private HashMap telephone;

省略get/set

}
 
MainClass:

Jakarta-Commons- BeanUtils1.8.jar+JDK1.5


package com.cache.domain ;


import java.util.HashMap ;
import org.apache.commons.beanutils.BeanUtils ;

public class MainClass {


    public static void main( String[] args ) throws Exception{

        Test test = new Test();

        test.setName( "zhangyi" );

        // BeanUtils.getProperty( bean, name )

        // 动态取得

        System.out.println(BeanUtils.getProperty( test, "name" ));


        HashMap map = new HashMap();

        map.put( "1", "13880808080" );

        map.put( "2", "13550505050" );

        // BeanUtils.setProperty( bean, name, value )

        BeanUtils.setProperty( test, "telephone", map );

        // 动态取得

        System.out.println(BeanUtils.getProperty( test, "telephone(1)" ));
       

        /**
         * 直接进行Bean之间的clone 复制后的2个Bean的同一个属性可能拥有同一个对象的ref,
         * 这个在使用时要小心,特别是对于属性为类的情况。

         */


        Test test2 = new Test();
        BeanUtils.copyProperties( test2, test );
        System.out.println(BeanUtils.getProperty( test2, "name" ));
        
    }


}

注:以上实例使用其余的功能大家继续挖掘吧,可以参考官方API的说明

ConvertUtilsBean convertUtils = new ConvertUtilsBean();

        DateConverter dateConverter = new DateConverter();

        convertUtils.register(dateConverter,Date.class);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值