Beanutils _ apache开发,专门用于操作javabean.
1. 在beanUtils工具包中,记下一个核心类: BeanUtils
最常用的两个静态方法:
static void | setProperty(Object bean, String name, Object value) 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) 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) |
static void | copyProperties(Object dest, Object orig) |
static void | copyProperty(Object bean, String name, Object value) |
static Map | createCache() |
static Map | describe(Object bean) |
static String[] | getArrayProperty(Object bean, String name) |
static boolean | getCacheFast(Map map) |
static int | getDebug() |
static String | getIndexedProperty(Object bean, String name) |
static String | getIndexedProperty(Object bean, String name, int index) |
static String | getMappedProperty(Object bean, String name) |
static String | getMappedProperty(Object bean, String name, String key) |
static String | getNestedProperty(Object bean, String name) |
static String | getProperty(Object bean, String name) |
static String | getSimpleProperty(Object bean, String name) |
static boolean | initCause(Throwable throwable, Throwable cause) |
static void | setCacheFast(Map map, boolean fast) |
static void | setDebug(int newDebug) |
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);
}