澳门威尼人斯:Java开源工具使用介绍(二)Apache Commons BeanUtils
在笔者之前的文章提到过,在BeanUtils中经由过程PropertyUtils取代了FieldUtils,PropertyUtils是一套很轻易操作类属性的API对象类,应用异常简单而且维持了对类的封装,应用get和set进行存取
public class TestMain {
public static void main(String[] args) throws IllegalAccessException,InvocationTargetException, NoSuchMethodException {
// 初始化信息Test test = new Test();
Integer[] testArray = new Integer[10];Map testMap = new HashMap();
// 以下措施名为set开始的措施均有get措施与其对应
// 设置username,testArray,testMap属性
PropertyUtils.setProperty(test, "username", "Hello");PropertyUtils.setProperty(test, "testArray", testArray);
PropertyUtils.setProperty(test, "testMap", testMap);
// 设置testArray属性下标为0的值为10
PropertyUtils.setIndexedProperty(test, "testArray[0]", 10);
// 设置testMap属性的值为参数三,遗憾是 key参数只支持字符串
PropertyUtils.setMappedProperty(test, "testMap", "Hello", "10");
// 在类中有嵌套类的时刻,就不能应用简单的setProperty来对引用类设置属性,
// 要应用nested,address为test类中属性名引用PropertyUtils.setNestedProperty(test, "address.id", 1);
// 判断属性是否有get措施或set措施PropertyUtils.isReadable(test, "username");
PropertyUtils.isWriteable(test, "username");
// 返回该实例整个属性,属性名为key,值为value
Map map = PropertyUtils.describe(test);
// 把test工具的所有属性复制到test2工具傍边,
// 经由过程反射字段名匹配进行复制,但要留意将会覆盖原值Test test2 = new Test();
PropertyUtils.copyProperties(test2, test);
// 获取属性的Class类型
PropertyUtils.getPropertyType(test, "username");}
}
PropertyUtils和BeanUtils的措施异常相似,但有一些小细节上可以差别它们,例如都应用copyProperty,BeanUtils只要属性名同等就算类型不合都可以相互兼容赋值,但PropertyUtils则会报错
public class TestMain {
public static void main(String[] args) throws IllegalAccessException,InvocationTargetException, NoSuchMethodException,
InstantiationException {Test test1 = new Test();
Test test2 = new Test();
// 复制单个属性
BeanUtils.copyProperty(test1, "username"澳门威尼人斯, test2);
// 克隆工具,留意是浅克隆
Test test3 = (Test) BeanUtils.cloneBean(test1);
/*
* 在设置光阴时,读者必要留意一个小问题,* 用-->setProperty(test1, "date", new Date());
* 这种要领设置光阴时没有问题的,但若然应用字符串的形式2013-10-3* 就会呈现问题,这是由于BeanUtils无法识别字符串类型和光阴类型的关系,
* 以是我们必要应用ConvertUtils来帮助BeanUtils,* 但应用如下要领进行转换后,PropertyUtils仍旧是力所不及
*/
//用此种要领没任何问题BeanUtils.setProperty(test1, "date", new Date());
//此种要领无法差别字符串照样日期BeanUtils.setProperty(test1, "date", "2013-10-01");
// 自定义一个转换器ConvertUtils.register(new Converter() {
@Override澳门威尼人斯
public Object convert(Class type, Object value) {if (value == null) {
throw new RuntimeException("value is null");}
if (value.getClass() == Date.class) {return value;
}if (value.getClass() != String.class) {
throw new RuntimeException("type not match");}
String valueStr = (String) value;if (StringUtils.isBlank(valueStr)) {
throw new RuntimeException("string is empty");}
try {retu澳门威尼人斯rn new SimpleDateFormat("yyyy-MM-dd").parse(valueStr);
} catch (ParseException e) {e.printStackTrace();
return null;}
}}, Date.class);
// 此时应用字符串的日期形式都可以成功转换
BeanUtils.setProperty(test1, "date", "2013-10-1");}
}
从上面光阴转换的例子就能看出,为什么同属性名不合类型PropertyUtils会转澳门威尼人斯换掉败,由于PropertyUtils并没有应用ConvertUtils来进行转换,导致类型不应时没发相互转换,因为Apache commons成长历史的缘故原由,导致某些Utils功能大年夜致相同,请读者自行选择
在某些营业环境下,我们的Bean是不确定的,可能随某些地方触发而改变,可能必要某个属性,又可能不必要某个属性,这种动态的Bean是编译性说话的Java无法支持的,但BeanUtils中的自由Bean(动态Bean)将动态创建Bean成为了可能
public class TestMain {
public static void main(String[] args) throws IllegalAccessException,InstantiationException {
// 创建动态Bean所拥有的字段
DynaProperty[] props = new DynaProperty[] {new DynaProperty("id", String.class),
new DynaProperty("testMap", Map.class) };
// 创建dynaClass的BasicDynaClass实现,传入定义的props澳门威尼人斯
DynaClass dynaClass = new BasicDynaClass("bean", null, props);
// 经由过程dynaClass创建Bean
DynaBean bean = dynaClass.newInstance();
// 可以按通俗bean的要领对其进行应用
bean.set("id", "hello");bean.set("testMap", new HashMap());
bean.set("testMap", "Hello", "World");
bean.get("id");bean.get("testMap", "Hello");
/** BasicDynaClass是DynaClass接口的实现类,
* 此中还有LazyDynaClass的实现可以赞助我们更方便* 的应用动态bean,lazy,懒嘛!方便
*/
// 不必要再定义dynaProperty,直接赋值时将会自动声明属性DynaClass dynaClass = new LazyDynaClass();
DynaBean dynaBean = dynaClass.newInstance();
dynaBean.set("username", "Hello");
dynaBean.set("testArray", 0, "Hello");dynaBean.set("testMap", "Hello", "World");
dynaBean.get("username");
dynaBean.get("testArray", 0);dynaBean.get("testMap", "Hello");
}
}