一、前言
基于spring-beans(4.1.4)的工具类org.springframework.beans.BeanUtils对注入spring对象按照Class实例化instantiateClass、class对象方法名称methodName查找findMethod、属性查找对于class类信息findPropertyType、对象属性复制copyProperties等常用操作,具体如下源码所示。
二、源码说明package org.springframework.beans;@b@@b@import java.beans.PropertyDescriptor;@b@import java.beans.PropertyEditor;@b@import java.lang.reflect.Constructor;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.lang.reflect.Modifier;@b@import java.net.URI;@b@import java.net.URL;@b@import java.util.Arrays;@b@import java.util.Collections;@b@import java.util.Date;@b@import java.util.List;@b@import java.util.Locale;@b@import java.util.Set;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.springframework.core.MethodParameter;@b@import org.springframework.util.Assert;@b@import org.springframework.util.ClassUtils;@b@import org.springframework.util.ConcurrentReferenceHashMap;@b@import org.springframework.util.ReflectionUtils;@b@import org.springframework.util.StringUtils;@b@@b@public abstract class BeanUtils@b@{@b@ private static final Log logger = LogFactory.getLog(BeanUtils.class);@b@ private static final Set> unknownEditorTypes = Collections.newSetFromMap(new ConcurrentReferenceHashMap(64));@b@@b@ public static T instantiate(Class clazz)@b@ throws BeanInstantiationException@b@ {@b@ Assert.notNull(clazz, "Class must not be null");@b@ if (clazz.isInterface())@b@ throw new BeanInstantiationException(clazz, "Specified class is an interface");@b@ try@b@ {@b@ return clazz.newInstance();@b@ }@b@ catch (InstantiationException ex) {@b@ throw new BeanInstantiationException(clazz, "Is it an abstract class?", ex);@b@ }@b@ catch (IllegalAccessException ex) {@b@ throw new BeanInstantiationException(clazz, "Is the constructor accessible?", ex);@b@ }@b@ }@b@@b@ public static T instantiateClass(Class clazz)@b@ throws BeanInstantiationException@b@ {@b@ Assert.notNull(clazz, "Class must not be null");@b@ if (clazz.isInterface())@b@ throw new BeanInstantiationException(clazz, "Specified class is an interface");@b@ try@b@ {@b@ return instantiateClass(clazz.getDeclaredConstructor(new Class[0]), new Object[0]);@b@ }@b@ catch (NoSuchMethodException ex) {@b@ throw new BeanInstantiationException(clazz, "N