BeanUtils

 Beanutils _ apache开发,专门用于操作javabean.

1.   在beanUtils工具包中,记下一个核心类:  BeanUtils

最常用的两个静态方法:

static void

setProperty(Object bean, String name, Object value)
Set the specified property value, performing type conversions as required to conform to the type of the destination property.

Parameters:
bean - Bean on which setting is to be performed
name - Property name (can be nested/indexed/mapped/combo)
value - Value to be set

static void

populate(Object bean, Map properties)
Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.

Parameters:
bean - JavaBean whose properties are being populated
properties - Map keyed by property name, with the corresponding (String or String[]) value(s) to be set


剩下的方法:

static Object

cloneBean(Object bean)
Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.

static void

copyProperties(Object dest, Object orig)
Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

static void

copyProperty(Object bean, String name, Object value)
Copy the specified property value to the specified destination bean, performing any type conversion that is required.

static Map

createCache()
Create a cache.

static Map

describe(Object bean)
Return the entire set of properties for which the specified bean provides a read method.

static String[]

getArrayProperty(Object bean, String name)
Return the value of the specified array property of the specified bean, as a String array.

static boolean

getCacheFast(Map map)
Return whether a Map is fast

static int

getDebug()
Deprecated. BeanUtils now uses commons-logging for all log messages. Use your favorite logging tool to configure logging for this class.

static String

getIndexedProperty(Object bean, String name)
Return the value of the specified indexed property of the specified bean, as a String.

static String

getIndexedProperty(Object bean, String name, int index)
Return the value of the specified indexed property of the specified bean, as a String.

static String

getMappedProperty(Object bean, String name)
Return the value of the specified indexed property of the specified bean, as a String.

static String

getMappedProperty(Object bean, String name, String key)
Return the value of the specified mapped property of the specified bean, as a String.

static String

getNestedProperty(Object bean, String name)
Return the value of the (possibly nested) property of the specified name, for the specified bean, as a String.

static String

getProperty(Object bean, String name)
Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String.

static String

getSimpleProperty(Object bean, String name)
Return the value of the specified simple property of the specified bean, converted to a String.

static boolean

initCause(Throwable throwable, Throwable cause)
If we're running on JDK 1.4 or later, initialize the cause for the given throwable.

static void

setCacheFast(Map map, boolean fast)
Set whether fast on a Map

static void

setDebug(int newDebug)
Deprecated. BeanUtils now uses commons-logging for all log messages. Use your favorite logging tool to configure logging for this class.


2.Beanutils特点:

       1:提供了String到其他各种的自动的转换

              String-> int,Integer,double,- 到所以基本及它的对象类的转换


2:JavaBean可以只提供setXxx方法而不提供getXxx方法也可以设置成功。

//以下是深入源码

       Object newValue =null;

        if (type.isArray() && (index < 0)) {// Scalar value into array

          . . .

        } else if (type.isArray()) {         // Indexed value into array

           . . .

        } else {                             // Value into scalar

            if (value instanceof String) {

                newValue = getConvertUtils().convert((String) value, type);

            } else if (value instanceof String[]) {

                newValue =getConvertUtils().convert(((String[])value)[0],

                                               type);

            } else {

                newValue = convert(value, type);

            }

       }

 

protected Object convert(Objectvalue, Class type) {

        Converter converter =getConvertUtils().lookup(type);

        if (converter != null) {

            log.trace("        USING CONVERTER " + converter);

            return converter.convert(type, value);

        } else {

            return value;

        }

}

 

public Converter lookup(Classclazz) {

        return ((Converter) converters.get(clazz));

}

 

public Object get(Objectkey) {

               return(map.get(key));

}

 3:级联设置

             

   4:批设置

              可以直接接收一个Map

      

 

  5:兼容数组到基本类型的转换



3.在beanutils中还有一个核心类:转换核心类ConvertUtils

    此类需要设置一次,在任意的地方,设置一次,对所有beantuisl有效:

    @Test

    public void testBeanUtils() throws Exception{

       //声明可以从String->Date转换

       ConvertUtils.register(new Converter() {

           @Override

           public Object convert(Class type, Object value) {

              System.err.println("开始转换:"+type+","+value);

              if(type==Date.class){

                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                  try {

                     return sdf.parse(""+value);

                  } catch (ParseException e) {

                     e.printStackTrace();

                  }

              }

              return null;

           }

       }, Date.class);

       //----

        Object o = new Emp();

        BeanUtils.setProperty(o,"brith","2008-09-12 12:56:23");

        System.err.println(o);

      

    }








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值