网银网站模板百度信息流广告
BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。
BeanUtils是这个包里比较常用的一个工具类,这里只介绍它的copyProperties()方法。该方法源码如下:
public static void copyProperties(Object source, Object target) throws BeansException {copyProperties(source, target, (Class)null, (String[])null);}public static void copyProperties(Object source, Object target, Class<?> editable) throws BeansException {copyProperties(source, target, editable, (String[])null);}public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {copyProperties(source, target, (Class)null, ignoreProperties);}private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) throws BeansException {Assert.notNull(source, "Source must not be null");Assert.notNull(target, "Target must not be null");Class<?> actualEditable = target.getClass();if(editable != null) {if(!editable.isInstance(target)) {throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");}actualEditable = editable;}PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);List<String> ignoreList = ignoreProperties != null?Arrays.asList(ignoreProperties):null;PropertyDescriptor[] var7 = targetPds;int var8 = targetPds.length;for(int var9 = 0; var9 < var8; ++var9) {PropertyDescriptor targetPd = var7[var9];Method writeMethod = targetPd.getWriteMethod();if(writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());if(sourcePd != null) {Method readMethod = sourcePd.getReadMethod();if(readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {try {if(!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {readMethod.setAccessible(true);}Object value = readMethod.invoke(source, new Object[0]);if(!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {writeMethod.setAccessible(true);}writeMethod.invoke(target, new Object[]{value});} catch (Throwable var15) {throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);}}}}}}
如果你有两个具有很多相同属性的JavaBean,就可以试用该方法将sourse中的属性copy到target中,如果sourse和target间存在名称不相同的属性,则BeanUtils不对这些属性进行处理,需要程序员手动处理。
怎么样,很方便吧!除BeanUtils外还有一个名为PropertyUtils的工具类,它也提供copyProperties()方法,作用与 BeanUtils的同名方法十分相似,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,而前者不支持这个功能,但是速度会更快一些。BeanUtils支持的转换类型如下:
* java.lang.BigDecimal * java.lang.BigInteger * boolean and java.lang.Boolean * byte and java.lang.Byte * char and java.lang.Character * java.lang.Class * double and java.lang.Double * float and java.lang.Float * int and java.lang.Integer * long and java.lang.Long * short and java.lang.Short * java.lang.String * java.sql.Date * java.sql.Time * java.sql.Timestamp
这里要注意一点,java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。
现在,还有一个坏消息:使用BeanUtils的成本惊人地昂贵!我做了一个简单的测试,BeanUtils所花费的时间要超过取数 据、将其复制到对应的 value对象(通过手动调用get和set方法),以及通过串行化将其返回到远程的客户机的时间总和。所以要小心使用。
注意点一:
apache和spring的工具包中都有BeanUtils,使用其中的copyProperties方法可以非常方便的进行这些工作,但在实际应用中发现,对于null的处理不太符合个人的需要,例如在进行修改操作中只需要对model中某一项进行修改,那么一般我们在页面上只提交model的ID及需要修改项的值,这个时候使用BeanUtils.copyProperties会将其他的null绑定到pojo中去。
大家可以直接调用我们加工类的copyPropertiesIgnoreNull()方法即可忽略null值,避免老数据被null覆盖的尴尬。具体代码如下:
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;import java.util.HashSet;
import java.util.Set;public class SpringUtil implements ApplicationContextAware {/** * 当前IOC * */ private static ApplicationContext applicationContext; /** * * 设置当前上下文环境,此方法由spring自动装配 * */ @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { applicationContext = arg0; } /** * 从当前IOC获取bean * * @param id * bean的id * @return **/ public static Object getObject(String id) { Object object = null; object = applicationContext.getBean(id); return object; } public static String[] getNullPropertyNames (Object source) {final BeanWrapper src = new BeanWrapperImpl(source);java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();Set<String> emptyNames = new HashSet<String>();for(java.beans.PropertyDescriptor pd : pds) {Object srcValue = src.getPropertyValue(pd.getName());if (srcValue == null) emptyNames.add(pd.getName());}String[] result = new String[emptyNames.size()];return emptyNames.toArray(result);}public static void copyPropertiesIgnoreNull(Object src, Object target){BeanUtils.copyProperties(src, target, getNullPropertyNames(src));}}
调用:copyPropertiesIgnoreNull
public class TestBeanUtiles {public static void main(String[] args) {NewPerson newPerson = new NewPerson();newPerson.setName("bifuguo");//前台用户更新过的数据,例如前台只修改了用户名//下面我们假设是从数据库加载出来的老数据OldPerson oldPerson = new OldPerson();oldPerson.setSex("nv");oldPerson.setAge(5);//如果我们想把新数据更新到老数据这个对象里面,我们就可以借助BeanUtils.copyProperties()的方法如下://BeanUtils.copyProperties(newPerson, oldPerson);SpringUtil.copyPropertiesIgnoreNull(newPerson, oldPerson);System.out.println(newPerson.toString());System.out.println(oldPerson.toString());}
}
打印结果:
NewPerson{name='bifuguo', sex='null', age=0}
OldPerson{name='bifuguo', sex='nv', age=0}
现在就可以看出老数据没有被null覆盖
注意点二:
1.Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法;
2.如果存在属性完全相同的内部类,但是不是同一个内部类,即分别属于各自的内部类,则spring会认为属性不同,不会copy;
3.泛型只在编译期起作用,不能依靠泛型来做运行期的限制;
4.最后,spring和apache的copy属性的方法源和目的参数的位置正好相反,所以导包和调用的时候都要注意一下。