Student s1 = new Student(); s1.setName("zhangsan"); s1.setAge(26); s1.setBirthDay(new Date()); Student s2 = new Student(); long start = System.nanoTime(); s2.setAge(s1.getAge()); s2.setName(s1.getName()); long end = System.nanoTime(); System.out.println("set方法用时:" + (end - start)); Student s3 = new Student(); start = System.nanoTime(); try{ PropertyUtils.copyProperties(s3, s1); } catch (Exception e){ } end = System.nanoTime(); System.out.println("name=" + s3.getName() + ",age=" + s3.getAge() + ",date=" + s3.getBirthDay()); System.out.println("propertyCopy方法用时:" + (end - start)); Student s4 = new Student(); start = System.nanoTime(); try{ BeanUtils.copyProperties(s4, s1); } catch (Exception e){ } end = System.nanoTime(); System.out.println("name=" + s4.getName() + ",age=" + s4.getAge() + ",date=" + s4.getBirthDay()); System.out.println("beanUtils方法用时:" + (end - start)); Student s5 = new Student(); start = System.nanoTime(); try{ PropertyUtilsBean.class.newInstance().copyProperties(s5, s1); } catch (Exception e){ } end = System.nanoTime(); System.out.println("name=" + s5.getName() + ",age=" + s5.getAge() + ",date=" + s5.getBirthDay()); System.out.println("PropertyUtilsBean方法用时:" + (end - start)); Student s6 = new Student(); start = System.nanoTime(); try{ copyProperties(s6, s1); } catch (Exception e){ } end = System.nanoTime(); System.out.println("name=" + s6.getName() + ",age=" + s6.getAge() + ",date=" + s6.getBirthDay()); System.out.println("自己实现方法用时:" + (end - start));自己实现copy代码
public static Object copyProperties(Object dbInstance, Object bean) { Class klass = bean.getClass(); Object obj = null; Method methods[] = klass.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; String name = method.getName(); String key = ""; if (name.startsWith("get") && !name.equals("getClass")) { key = name.substring(3); } else { if (!name.startsWith("is")) { continue; } key = name.substring(2); } try { obj = method.invoke(bean, new Object[]{}); } catch (Exception e) { e.printStackTrace(); } if (obj != null && !obj.getClass().getName().equals("java.util.HashSet")) { try { for (int j = 0; j < methods.length; j++) { if (!methods[j].getName().equals( (new StringBuilder("set")).append(key) .toString())) { continue; } Object objs[] = new Object[1]; objs[0] = obj; methods[j].invoke(dbInstance, objs); break; } } catch (Exception e) { key = name.substring(3); for (int j = 0; j < methods.length; j++) { if (!methods[j].getName().equals( (new StringBuilder("set")).append(key) .toString())) { continue; } Object objs[] = new Object[1]; objs[0] = obj; try { methods[j].invoke(dbInstance, objs); } catch (Exception ex) { ex.printStackTrace(); } break; } } } } return dbInstance; }
用时输出如下:
set方法用时:3642纳秒
INFO 2013-11-28 21:43:26,015 [main][] [cn.com.carsmart.util.configmanagement.ConfigUtil] - config reader type is set to default classpath loading way.自己实现方法用时:60255纳秒
结论:对性能要求特别高的用set,get方法;只有简单属性的自己反射实现;对性能要求不太高,代码又便于维护的用PropertyUtilsBean的copyProperties方法。