一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

BeanUtils的copyProperties的效率问题

时间:2008-04-28 编辑:简简单单 来源:一聚教程网


BeanUtils的copyProperties的效率问题实际上来说应该是反射的效率问题,不过copyProperties方法里面的那些判断也带来了一些效率问题,下面来测试一下copyProperties的效率问题。

首先建一个类User,代表一个用户,有用户名和密码属性,代码如下:

public class User {
private String name;
private String password;
/**
    * @return the name
    */
public String getName() {
    return name;
}
/**
    * @param name the name to set
    */
public void setName(String name) {
    this.name = name;
}
/**
    * @return the password
    */
public String getPassword() {
    return password;
}
/**
    * @param password the password to set
    */
public void setPassword(String password) {
    this.password = password;
}
}

建一个类,名为AbstractService,代码如下:

public abstract class AbstractService {
public static int userCount = 100000;

public User srcUsers[] = new User[userCount];

public User destUsers[] = new User[userCount];

public AbstractService() {
    for (int i = 0; i < userCount; i++) {
     User user = new User();
     user.setName("" + i);
     user.setPassword("" + i);
     srcUsers[i]=user;
   
     User user1 = new User();
     destUsers[i]=user1;
    }
}

public abstract void process() throws Throwable;

public long service() throws Throwable{
    long beginTime = System.currentTimeMillis();
    process();
    long endTime = System.currentTimeMillis();
    return endTime - beginTime;
}
}

建立一类BeanUtilsService,代码如下:

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class BeanUtilsService extends AbstractService {
@Override
public void process() throws IllegalAccessException,

<