以下是本人实现的一个利用Java反射实现的Bean拷贝类,可以实现对JavaBean的映射拷贝以及属性值获取: package com.sunfairs.util; import java.lang.reflect.Method; import java.util.Arrays; public class BeanUtil { public static void copyFields(Object src,Object des,boolean copyNull,String[]fieldNames,boolean include){ Class desClass = des.getClass(); Arrays.sort(fieldNames); Method[]methods = src.getClass().getMethods(); for(int i=0;i<methods.length;i++){ Method method = methods[i]; String methodName = method.getName(); if(methodName.startsWith("get")){ String attrName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4, methodName.length()); boolean inAttr = Arrays.binarySearch(fieldNames, attrName) > 0 ? true:false; try { if(inAttr == include){ Class returnType = method.getReturnType(); Object returnValue = method.invoke(src, new Object[]{}); if(!copyNull && returnValue == null){ continue; } String invokeMethodName = "set" + methodName.substring(3, methodName.length()); Method invokeMethod = desClass.getMethod(invokeMethodName, new Class[]{returnType}); invokeMethod.invoke(des, new Object[]{returnValue}); } } catch (Exception e) { e.printStackTrace(); } } } } public static void copyFields(Object src,Object des,boolean copyNull){ final String get = "get"; final String set = "set"; final String is = "is"; Class srcClass = src.getClass(); Class desClass = des.getClass(); Method[]methods = srcClass.getMethods(); for(int i=0;i<methods.length;i++){ Method method = methods[i]; String methodName = method.getName(); if(methodName.startsWith(get) || methodName.startsWith(is)){ String invokeMethodName = set + methodName.substring(3); try { Method invokeMethod = desClass.getMethod(invokeMethodName, new Class[]{method.getReturnType()}); Object result = method.invoke(src, new Object[]{}); if(!copyNull && result == null){ continue; } invokeMethod.invoke(des, new Object[]{result}); } catch (Exception e) { e.printStackTrace(); } } } } public static void copyFields(Object src,Object des,boolean copyNull,boolean copyEmpty){ final String get = "get"; final String set = "set"; final String is = "is"; Class srcClass = src.getClass(); Class desClass = des.getClass(); Method[]methods = srcClass.getMethods(); for(int i=0;i<methods.length;i++){ Method method = methods[i]; String methodName = method.getName(); if(methodName.startsWith(get) || methodName.startsWith(set) || methodName.startsWith(is)){ String invokerMethodName = set + methodName.substring(3); try{ Method invokerMethod = desClass.getMethod(invokerMethodName, new Class[]{method.getReturnType()}); Object result = method.invoke(src, new Object[]{}); if(!copyNull && result == null){ continue; } if(!copyEmpty && result.toString().trim().equals("")){ continue; } invokerMethod.invoke(des, new Object[]{result}); } catch(Exception e){ e.printStackTrace(); } } } } public static Object getValue(Object object,String name){ try { Method method = object.getClass().getMethod("get" + formatCase(name), new Class[]{}); Object result = method.invoke(object, new Object[]{}); if(result != null){ return result; } } catch (Exception e) { e.printStackTrace(); } return null; } protected static String formatCase(String name){ StringBuffer buffer = new StringBuffer(name); char c = Character.toUpperCase(name.charAt(0)); buffer.deleteCharAt(0); buffer.insert(0, c); return buffer.toString(); } public static void main(String[] args) { String methodName = formatCase("customerName"); System.out.println(methodName); } }