BeanUtils和Cglib的Java Bean屬性copy功能的效能比較__Java

來源:互聯網
上載者:User
以前將Struts中的ActionForm的屬性Copy到對應的Hibernate 實體類時,我用的是BeanUtils,覺得其功能還可以,前幾天學習cglib時發現它也有個bean copy的功能,所以想將他們兩的效能作個比較,就寫了一個較簡單的測試案例,不比不知道,一比嚇一跳,看來cglib的表現確實不凡呀
package com.redbeans.test;

import java.lang.reflect.InvocationTargetException;

import junit.framework.TestCase;
import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.beanutils.BeanUtils;

public class BeanCopyTest extends TestCase {


     class Bean01 {
        /** identifier field */
        private long personId;

        /** persistent field */
        private String username;

        /** persistent field */
        private String password;

        /** nullable persistent field */
        private String gender;

        /** nullable persistent field */
        private String email;

        /** nullable persistent field */
        private String address;

        /** nullable persistent field */
        private String postcode;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public long getPersonId() {
            return personId;
        }

        public void setPersonId(long personId) {
            this.personId = personId;
        }

        public String getPostcode() {
            return postcode;
        }

        public void setPostcode(String postcode) {
            this.postcode = postcode;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
       
    }
   
    class Bean02 {
        /** identifier field */
        private long personId;

        /** persistent field */
        private String username;

        /** persistent field */
        private String password;

        /** nullable persistent field */
        private String realName;

        /** nullable persistent field */
        private String gender;

        /** nullable persistent field */
        private double height;

        /** nullable persistent field */
        private double weight;

        /** nullable persistent field */
        private int status;

        /** nullable persistent field */
        private String telephone;

        /** nullable persistent field */
        private String email;

        /** nullable persistent field */
        private String address;

        /** nullable persistent field */
        private String postcode;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public double getHeight() {
            return height;
        }

        public void setHeight(double height) {
            this.height = height;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public long getPersonId() {
            return personId;
        }

        public void setPersonId(long personId) {
            this.personId = personId;
        }

        public String getPostcode() {
            return postcode;
        }

        public void setPostcode(String postcode) {
            this.postcode = postcode;
        }

        public String getRealName() {
            return realName;
        }

        public void setRealName(String realName) {
            this.realName = realName;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getTelephone() {
            return telephone;
        }

        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public double getWeight() {
            return weight;
        }

        public void setWeight(double weight) {
            this.weight = weight;
        }
       
    }
    public void testCglib() {
        Bean01 bean01 = new Bean01();
        bean01.setAddress("test");
        bean01.setUsername("bean01");
        Bean02 bean02 = new Bean02();
        for (int i = 0; i < 10000; i++) {
            BeanCopier copier = BeanCopier.create(Bean01.class, Bean02.class,
                    false);
            copier.copy(bean01, bean02, null);
        }
    }

     public void testBeanUtils() {
        Bean01 bean01 = new Bean01();
        bean01.setAddress("test");
        bean01.setUsername("bean01");
        Bean02 bean02 = new Bean02();
        for (int i = 0; i < 10000; i++) {
            try {
                BeanUtils.copyProperties(bean02, bean01);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.