package com.beanUtil;
public class TestConvertUtil extends org.apache.commons.beanutils.ConvertUtils {
//将object转化成int型
public static int intConvert(Object object){
String str = object==null?null:object.toString();
Object obj = org.apache.commons.beanutils.ConvertUtils.convert(str, Integer.class);
Integer i= (Integer)obj;
return i;
}
//将任意的Object转换成string
public static String stringConvert(Object object){
Object obj = org.apache.commons.beanutils.ConvertUtils.convert(object);
String string = (String) obj;
return string;
}
public static boolean booleanConvert(Object object){
String str = object == null?null:object.toString();
Object obj = org.apache.commons.beanutils.ConvertUtils.convert(str, Boolean.class);
boolean b =(Boolean)obj;
return b;
}
public static void main(String[] arg) {
System.out.println(intConvert("123"));
System.out.println(stringConvert(456.2));
System.out.println(booleanConvert("true"));
}
}
======================================================================================
使用PropertyUtils 将javaBean转化成Map
需要导入commons-beanutils.jar 和 commons-logging.jar
下面是两个javaBean
Author.java
package com.beanUtil;
public class Author {
private String name;
public Author() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Book.java
package com.beanUtil;
public class Book {
private String name;
private Author author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
}
Bean2Map.java
package com.beanUtil;
import java.util.Map;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.PropertyUtils;
public class Bean2Map {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Author author = new Author();
author.setName("wenliang.li");
Book book = new Book();
book.setName("pro spring");
book.setAuthor(author);
// 取得book中的所有可读属性,返回到map中
Map<String,Book> bookMap = null;
Map<String,Author> authorMap = null;
try {
bookMap = PropertyUtils.describe(book);
// Book类中包含一个Author类型的属性
// 取bookMap中的author bean,并将其所有可读属性返回到authorMap中
authorMap = PropertyUtils.describe(bookMap.get("author"));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//输出
//Book Name: pro spring
//Author Name: wenliang.li
System.out.println("Book Name: " + bookMap.get("name"));
System.out.println("Author Name: " + authorMap.get("name"));
}
}
==============================================================================================================
LazyDynaBean动态加载属性类,配合LazyDynaClass一起使用
//用法1
DynaProperty[] props=new DynaProperty[]{
new DynaProperty("name",String.class),
new DynaProperty("age",Integer.class),
new DynaProperty("birth",Date.class)
};
LazyDynaClass dynaClass=new LazyDynaClass("user",LazyDynaBean.class,props);
try {
LazyDynaBean dynaBean = (LazyDynaBean) dynaClass.newInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
dynaBean.set("name", "章子怡");
dynaBean.set("age", 16);
dynaBean.set("birth", sdf.parse("1986-05-01"));
StringBuilder sb=new StringBuilder();
sb.append("name:").append(dynaBean.get("name"))
.append(",age:").append(dynaBean.get("age"))
.append(",birth:").append(sdf.format(dynaBean.get("birth")));
System.out.println(sb.toString());
dynaBean.set("email", "[email protected]");
System.out.println(dynaBean.get("email"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//用法2
DynaBean bean=new LazyDynaBean();
bean.set("name", "孙晓磊");
System.out.println(bean.get("name"));
MutableDynaClass dc=(MutableDynaClass)bean.getDynaClass(); //获取可以动态添加属性的DynaClass类
System.out.println("----开始测试数组属性----");
dc.add("indexProp", int[].class); //添加整型数组属性
bean.set("indexProp", 0, 1); //给数组属性添加值
bean.set("indexProp", 1, 2);
int[] indexProp=(int[]) bean.get("indexProp");
System.out.println(indexProp[1]);
System.out.println("----开始测试LIST属性----");
bean.set("indexProp2", 0, "str1"); //直接添加属性,此时属性的类型是LIST或数组,此处是LIST
bean.set("indexProp2", 1, "str2");
List list=(List) bean.get("indexProp2");
System.out.println(list.get(0));
System.out.println("----开始测试MAP属性----");
bean.set("mapProp","key1","value1"); //直接添加键值对,默认属性类型是MAP
Map map=(Map)bean.get("mapProp");
System.out.println(map.get("key1"));
dc.add("mapProp2", HashMap.class); //或者先设置属性类型,再添加
bean.set("mapProp2","mapProp2-key1","mapProp2-value1");
Map map2=(Map)bean.get("mapProp2");
System.out.println(map2.get("mapProp2-key1"));