commons-beanutils包介绍

BeanUtils类

BeanUtils工具包是由Apache公司所开发,主要是方便程序员对Bean类能够进行简便的操作。

BeanUtils主要提供了对于JavaBean进行各种操作,比如对象,属性复制等等。

BeanUtils设置属性值的时候,如果属性是基本数据类型,那么BeanUtils会自动帮我们进行数据类型的转换,并且BeanUtils设置属性的时候也是依赖于底层的getter和setter方法。如果设置的属性值是其他的引用数据类型,此时必须要注册一个类型转换器才能实现自动的转换(参考下面的ConvertUtils)

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.

即使bean类本身未实现Cloneable,也要根据可用的属性getter和setter克隆bean。

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.

对于所有属性名称相同的情况,将属性值从原始Bean复制到目标Bean。

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.

将指定的属性值复制到指定的目标Bean,执行所需的任何类型转换。

static <K,V> Map<K,V> createCache()

Create a cache.

static Map<String,String> describe(Object bean)

Return the entire set of properties for which the specified bean provides a read method.

返回指定Bean提供读取方法的整个属性集。

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 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 populate(Object bean, Map<String,? extends Object> properties)

Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.

根据指定的名称/值对填充指定Bean的JavaBeans属性。

static void setCacheFast(Map<?,?> map, boolean fast)

Set whether fast on a Map

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.

设置指定的属性值,根据需要执行类型转换以符合目标属性的类型。

注意:

BeanUtils支持的转换类型如下:

java.lang.BigDecimal
java.lang.BigInteger
boolean and java.lang.Boolean
byte and java.lang.Byte
char and java.lang.Character
java.lang.Class
double and java.lang.Double
float and java.lang.Float
int and java.lang.Integer
long and java.lang.Long
short and java.lang.Short
java.lang.String
java.sql.Date
java.sql.Time
java.sql.Timestamp

这里要注意一点,java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,(1)、一定要使用java.sql.Date类型,否则会抛出异常。(2)、自定义一个转换器

我们看一下Converter接口的实现类,如下

org.apache.commons.beanutils

Interface Converter

ConvertUtils类

这个工具类的职能是在字符串和指定类型的实例之间进行转换。 实际上,BeanUtils是依赖ConvertUtils来完成类型转换,但是有时候我们可能需要自定义转换器来完成特殊需求的类型转换;

static String convert(Object value)

Convert the specified value into a String.

static Object convert(Object value, Class<?> targetType)

Convert the value to an object of the specified class (if possible).

static Object convert(String[] values, Class<?> clazz)

Convert an array of specified values to an array of objects of the specified class (if possible).

static Object convert(String value, Class<?> clazz)

Convert the specified value to an object of the specified class (if possible).

static void deregister()

Remove all registered Converters, and re-establish the standard Converters.

static void deregister(Class<?> clazz)

Remove any registered Converter for the specified destination Class.

static Converter lookup(Class<?> clazz)

Look up and return any registered Converter for the specified destination class; if there is no registered Converter, return null.

查找并返回指定目标类的所有已注册的转换器; 如果没有注册的Converter,则返回null。

static Converter lookup(Class<?> sourceType, Class<?> targetType)

Look up and return any registered Converter for the specified source and destination class; if there is no registered Converter, return null.

static <T> Class<T> primitiveToWrapper(Class<T> type)

Change primitive Class types to the associated wrapper class.

static void register(Converter converter, Class<?> clazz)

Register a custom Converter for the specified destination Class, replacing any previously registered Converter.

为指定的目标类别注册自定义转换器,以替换任何先前注册的转换器。

自定义转换器

//注册该转换器
ConvertUtils.register(new Converter() {

	public Object convert(Class clazz, Object value) {
	// 将String转换为Date
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	java.util.Date parse = null;
	try {
		parse = format.parse(value.toString());
	    } catch (ParseException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
	    }
	    return parse;
	}

}, java.util.Date.class);
UserInfoEntry userInfoEntry = new UserInfoEntry();
BeanUtils.setProperty(userInfoEntry, "utilDate", "2019-12-24");
System.out.println(userInfoEntry);

也可以注册使用dozer包下的DateConverter日期转换器,

import org.dozer.converters.DateConverter;
ConvertUtils.register(new DateConverter(DATE_FORMAT), java.util.Date.class);

PropertyUtils类

static void addBeanIntrospector(BeanIntrospector introspector)

Adds a BeanIntrospector.

static void clearDescriptors()

Clear any cached property descriptors information for all classes loaded by any class loaders.

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 (even though the actual getter and setter methods might have been customized via BeanInfo classes).

static Map<String,Object> describe(Object bean)

Return the entire set of properties for which the specified bean provides a read method.

static int getDebug()Deprecated. 

The debug static property is no longer used

static Object getIndexedProperty(Object bean, String name)

Return the value of the specified indexed property of the specified bean, with no type conversions.

static Object getIndexedProperty(Object bean, String name, int index)

Return the value of the specified indexed property of the specified bean, with no type conversions.

static Object getMappedProperty(Object bean, String name)

Return the value of the specified mapped property of the specified bean, with no type conversions.

static Object getMappedProperty(Object bean, String name, String key)

Return the value of the specified mapped property of the specified bean, with no type conversions.

static Object getNestedProperty(Object bean, String name)

Return the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.

static Object getProperty(Object bean, String name)

Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

static PropertyDescriptor getPropertyDescriptor(Object bean, String name)

Retrieve the property descriptor for the specified property of the specified bean, or return null if there is no such descriptor.

static PropertyDescriptor[] getPropertyDescriptors(Class<?> beanClass)

Retrieve the property descriptors for the specified class, introspecting and caching them the first time a particular bean class is encountered.

static PropertyDescriptor[] getPropertyDescriptors(Object bean)

Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.

static Class<?> getPropertyEditorClass(Object bean, String name)

Return the Java Class repesenting the property editor class that has been registered for this property (if any).

static Class<?> getPropertyType(Object bean, String name)

Return the Java Class representing the property type of the specified property, or null if there is no such property for the specified bean.

static Method getReadMethod(PropertyDescriptor descriptor)

Return an accessible property getter method for this property, if there is one; otherwise return null.

static Object getSimpleProperty(Object bean, String name)

Return the value of the specified simple property of the specified bean, with no type conversions.

static Method getWriteMethod(PropertyDescriptor descriptor)

Return an accessible property setter method for this property, if there is one; otherwise return null.

static boolean isReadable(Object bean, String name)

Return true if the specified property name identifies a readable property on the specified bean; otherwise, return false.

static boolean isWriteable(Object bean, String name)

Return true if the specified property name identifies a writeable property on the specified bean; otherwise, return false.

static boolean removeBeanIntrospector(BeanIntrospector introspector)

Removes the specified BeanIntrospector.

static void resetBeanIntrospectors()

Resets the registered BeanIntrospector objects to the initial default state.

static void setIndexedProperty(Object bean, String name, int index, Object value)

Sets the value of the specified indexed property of the specified bean, with no type conversions.

static void setIndexedProperty(Object bean, String name, Object value)

Sets the value of the specified indexed property of the specified bean, with no type conversions.

static void setMappedProperty(Object bean, String name, Object value)

Sets the value of the specified mapped property of the specified bean, with no type conversions.

static void setMappedProperty(Object bean, String name, String key, Object value)

Sets the value of the specified mapped property of the specified bean, with no type conversions.

static void setNestedProperty(Object bean, String name, Object value)

Sets the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.

static void setProperty(Object bean, String name, Object value)

Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

static void setSimpleProperty(Object bean, String name, Object value)

Set the value of the specified simple property of the specified bean, with no type conversions.

相关博文commons-beanutils使用介绍 

发布了212 篇原创文章 · 获赞 135 · 访问量 137万+

猜你喜欢

转载自blog.csdn.net/ystyaoshengting/article/details/103667965