以下方法主要为测试用,验证抛异常情况,代码如下:
分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 mismatch
at 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 mismatch
at 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
附图:
本文详细介绍了在Java编程中使用BeanUtils类进行对象属性复制时遇到的异常情况,以及如何通过测试用例进行验证。重点探讨了属性类型不匹配导致的IllegalArgumentException,并提供了解决方案。
1649

被折叠的 条评论
为什么被折叠?



