无法使用 BeanUtils 将一个 class 的属性复制到另一个
Cant copy properities of one class to another using BeanUtils
我有一段代码,我使用 BeanUtils.copyProperities(dest, orig)
将一个 class 的相似属性复制到另一个。然而。这是行不通的。我收到错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
我正在使用 BeanUtils 1.9.2,Java8,Windows10,Eclipse。
import org.apache.commons.beanutils.*;
public class Main{
public Main(){
Entity entity = new Entity();
AbstractGameObject aEntity = new AbstractGameObject();
try {
BeanUtils.copyProperties(aEntity, entity);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(aEntity.similar); // Should print out 10, No?;
}
public static void main(String[] args) {
Main main = new Main();
}
private class Entity{
int similar = 10;
int differentE = 9;
public Entity(){
}
}
private class AbstractGameObject{
int similar = 2;
int differentA = 1;
public AbstractGameObject(){
}
}
}
重要提示:类 必须是 public 并且 copyProperties 使用 setter 和 getter。
尝试:
public class Main {
public Main() {
Entity entity = new Entity();
AbstractGameObject aEntity = new AbstractGameObject();
try {
BeanUtils.copyProperties(aEntity, entity);
} catch (Exception ex) {
// use a logger
}
System.out.println(aEntity.similar);
System.out.println(entity.similar);
}
public static void main(String[] args){
Main main = new Main();
}
public class Entity {
private int similar = 10;
private int differentE = 9;
public int getSimilar() {
return similar;
}
public void setSimilar(int similar) {
this.similar = similar;
}
public int getDifferentE() {
return differentE;
}
public void setDifferentE(int differentE) {
this.differentE = differentE;
}
}
public class AbstractGameObject {
private int similar = 2;
private int differentA = 1;
public int getSimilar() {
return similar;
}
public void setSimilar(int similar) {
this.similar = similar;
}
public int getDifferentA() {
return differentA;
}
public void setDifferentA(int differentA) {
this.differentA = differentA;
}
}
}
此外,请注意,如果您使用 Lombok 生成 public getter 和 setter,BeanUtils.copyProperties()
将不起作用。您必须手动创建它们。
我有一段代码,我使用 BeanUtils.copyProperities(dest, orig)
将一个 class 的相似属性复制到另一个。然而。这是行不通的。我收到错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
我正在使用 BeanUtils 1.9.2,Java8,Windows10,Eclipse。
import org.apache.commons.beanutils.*;
public class Main{
public Main(){
Entity entity = new Entity();
AbstractGameObject aEntity = new AbstractGameObject();
try {
BeanUtils.copyProperties(aEntity, entity);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(aEntity.similar); // Should print out 10, No?;
}
public static void main(String[] args) {
Main main = new Main();
}
private class Entity{
int similar = 10;
int differentE = 9;
public Entity(){
}
}
private class AbstractGameObject{
int similar = 2;
int differentA = 1;
public AbstractGameObject(){
}
}
}
重要提示:类 必须是 public 并且 copyProperties 使用 setter 和 getter。 尝试:
public class Main {
public Main() {
Entity entity = new Entity();
AbstractGameObject aEntity = new AbstractGameObject();
try {
BeanUtils.copyProperties(aEntity, entity);
} catch (Exception ex) {
// use a logger
}
System.out.println(aEntity.similar);
System.out.println(entity.similar);
}
public static void main(String[] args){
Main main = new Main();
}
public class Entity {
private int similar = 10;
private int differentE = 9;
public int getSimilar() {
return similar;
}
public void setSimilar(int similar) {
this.similar = similar;
}
public int getDifferentE() {
return differentE;
}
public void setDifferentE(int differentE) {
this.differentE = differentE;
}
}
public class AbstractGameObject {
private int similar = 2;
private int differentA = 1;
public int getSimilar() {
return similar;
}
public void setSimilar(int similar) {
this.similar = similar;
}
public int getDifferentA() {
return differentA;
}
public void setDifferentA(int differentA) {
this.differentA = differentA;
}
}
}
此外,请注意,如果您使用 Lombok 生成 public getter 和 setter,BeanUtils.copyProperties()
将不起作用。您必须手动创建它们。