如何使用反射将属性从Pojo复制到其他Java Bean

有时我们需要将属性从Java类复制到其他类,我们可以手动执行此操作,也可以使用自己的反射实现,但在这种情况下,请使用apache的实用工具将反射自动执行

要求

  1. commons-beanutils,您可以从这里下载http://commons.apache.org/beanutils/
  2. commons-loging,您可以从此处下载http://commons.apache.org/logging/
  3. 日食
  4. JDK 1.6

2.动手

  1. 在eclipse中创建一个“ Java项目”。
  2. 项目名称:CopyProperties,然后单击“完成”按钮。
  3. 解压缩common-beanutils-xxx.zipcommons-logging-xxx.zip
  4. commons-beanutils-xxx.jarcommons-logging-xxx.jar到项目的类路径。

在包pojo.from创建一个新的类“ Person”。

package pojo.from;

public class Person {

        private String name;
        private int age;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                this.age = age;
        }

}

pojo.to创建具有相同字段的类“ OthePerson”

package pojo.to;

public class OthePerson {

        private String name;
        private int age;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                this.age = age;
        }
}

3.测试

使用main方法在pojo.test包中创建一个测试类,并测试commons-beanutils

package pojo.test;

import org.apache.commons.beanutils.BeanUtils;

import pojo.from.Person;
import pojo.to.OthePerson;

/**
 * Class for test copy properties
 *
 * @author Rene Enriquez
 * @date 23/07/2012
 *
 */
public class Test {

        /**
         * Main method
         *
         * @param args
         */
        public static void main(String[] args) throws Exception {
		Person person = new Person();
		person.setAge(15);
		person.setName("rene");

		OtherPerson othePerson = new OtherPerson();

		System.out.println("*** Before BeanUtils.copyProperties ***");

		System.out.println("Person");
		System.out.println(person.getAge());
		System.out.println(person.getName());

		System.out.println("othePerson");
		System.out.println(othePerson.getAge());
		System.out.println(othePerson.getName());

		//copy properties from (target, source)
		BeanUtils.copyProperties(othePerson, person);

		System.out.println("\n*** After BeanUtils.copyProperties ***");

		System.out.println("Person");
		System.out.println(person.getAge());
		System.out.println(person.getName());

		System.out.println("othePerson");
		System.out.println(othePerson.getAge());
		System.out.println(othePerson.getName());

	}
}

输出量

*** Before BeanUtils.copyProperties ***
Person
15
rene
othePerson
0
null

*** After BeanUtils.copyProperties ***
Person
15
rene
othePerson
15
rene

下载源代码

下载它– CopyProperties.zip (6 KB)

参考文献

  1. commons-beanutils官方网站
  2. 公地登录官方网站

翻译自: https://mkyong.com/java/how-to-use-reflection-to-copy-properties-from-pojo-to-other-java-beans/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值