BeanUtils.copyProperties用法的测试

来源:互联网 发布:超级玛丽java代码 编辑:程序博客网 时间:2024/06/04 18:51

以下方法主要为测试用,验证抛异常情况,代码如下:

分SourceBean.java、TargetBean.java、CopyPropertiesMainTest.java 其中SourceBean.java和TargetBean.java的区别是属性值width一个为String类型、另一个为long类型

/** *  */package testcopyproperties;/** * @author quyang.ybb * */public class SourceBean {        private String age;        private String name;        private String gender;        private boolean success;        private long length;        private long width;    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public boolean isSuccess() {        return success;    }    public void setSuccess(boolean success) {        this.success = success;    }    public long getLength() {        return length;    }    public void setLength(long length) {        this.length = length;    }    public long getWidth() {        return width;    }    public void setWidth(long width) {        this.width = width;    }   }

/** *  */package testcopyproperties;/** * @author quyang.ybb * */public class TargetBean {        private String age;        private String name;        private String gender;        private boolean success;        private long length;        private String width;    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public boolean isSuccess() {        return success;    }    public void setSuccess(boolean success) {        this.success = success;    }    public long getLength() {        return length;    }    public void setLength(long length) {        this.length = length;    }    public String getWidth() {        return width;    }    public void setWidth(String width) {        this.width = width;    }    }

/** *  */package testcopyproperties;import org.springframework.beans.BeanUtils;/** * @author quyang.ybb * */public class CopyPropertiesMainTest {    /**     * @param args     */    public static void main(String[] args) {SourceBean sourceBean = new SourceBean();TargetBean targetBean = new TargetBean();sourceBean.setAge("18");sourceBean.setWidth(100);try {    BeanUtils.copyProperties(sourceBean, targetBean);} catch (Exception e) {    e.printStackTrace();}System.out.println("The end");    }}

运行后报错提示:

org.springframework.beans.FatalBeanException: Could not copy properties from source to target; nested exception is java.lang.IllegalArgumentException: argument type mismatchat org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:599)at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:509)at testcopyproperties.CopyPropertiesMainTest.main(CopyPropertiesMainTest.java:23)Caused by: java.lang.IllegalArgumentException: argument type mismatchat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:596)... 2 more

附图:


0 0