zoukankan      html  css  js  c++  java
  • Beanutils.copyProperties( )使用详情总结

    Beanutils.copyProperties( )

    一、简介:

    BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。

    二、详情:

    ​ 如果你有两个具有很多相同属性的JavaBean,需要对象之间的赋值,这时候就可以使用这个方法,避免代码中全是get/set之类的代码,可以让代码简洁明朗更优雅。

    源码中的核心代码:

    private static void copyProperties(Object source, Object target, @Nullable Class<?> editable,
    			@Nullable 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);
    
    		for (PropertyDescriptor targetPd : targetPds) {
    			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);
    							if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
    								writeMethod.setAccessible(true);
    							}
    							writeMethod.invoke(target, value);
    						}
    						catch (Throwable ex) {
    							throw new FatalBeanException(
    									"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
    						}
    					}
    				}
    			}
    		}
    	}
    

    source是源对象,target是需要赋值的对象

    所需的maven依赖(spring-boot里面已经集成了spring-bean,所以不需要单独导入):

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
    

    说明:

    	赋值成功的属性对应的属性名和属性类型必须相同,否则对应的属性值不会从一个对象赋值给另一个对象,但是此时不影响其他属性值的拷贝。 
    

    三、使用

    将符合条件的属性值全部从一个对象赋值给另一个对象

    copyProperties(Object source, Object target)

    BeanUtils.copyProperties(model01,model02);
    

    将对象model01里面的值赋给model02

    如果有些对象没有属性,则需要我们手动添加

    如果有些对象不想赋值,需要我们手动设置

    忽略某些属性的赋值

    copyProperties(Object source, Object target, String... ignoreProperties)

    //定义name不赋值
    String[] ignoreProperties = {"name"};
    BeanUtils.copyProperties(model01,model02,ignoreProperties);
    



    最后:1024节日快乐,加班让我快乐...

  • 相关阅读:
    匿名方法、Lambda表达式、Func<>委托
    日期相关
    6个重要的概念:栈,堆,值类型,引用类型,装箱,拆箱
    linux 命令行cd dvd iso操作
    wkhtmltopdf错误解决办法
    lxml包引入错误
    安装python-ldap fatal error: lber.h: No such file or directory
    用pyenv和virtualenv搭建单机多版本python虚拟开发环境
    在Ubuntu上安装pyenv 相关问题Common build problems
    在Ubuntu上安装pyenv
  • 原文地址:https://www.cnblogs.com/mengw/p/11734374.html
Copyright © 2011-2022 走看看