java - Beanutils.copyProperties 与 protected 设置方法

标签 java copy javabeans apache-commons-beanutils

有没有办法让 BeanUtils 使用 protected setXXX(X x) 方法?或者您知道执行此操作的其他方法吗?

类似于:

public class A{
    private String text;

    public String getText(){
         return this.text;
    }
    protected void setText(String text){
         this.text = text;
    }
}
public class B{
    private String text;

    public String getText(){
         return this.text;
    }
    protected void setText(String text){
         this.text = text;
    }
}

public static void main(String[] args){
     A a = new A();
     a.setText("A text");
     B b = new B();
     BeanUtils.copyProperties(b, a);
     System.out.println(b.getText()); //A text
}

最佳答案

尝试使用 BULL (Bean Utils Light Library)

public static void main(String[] args) {
   A a = new A();
   a.setText("A text");
   B b = BeanUtils.getTransformer(B.class).apply(a);     
   System.out.println(b.getText()); //A text
}

关于java - Beanutils.copyProperties 与 protected 设置方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990286/

相关文章:

Java:将 ArrayList<Byte> 转换为 byte[] 的最佳方法是什么?

java - 如何使用pdfbox从pdf中提取粗体文本?

java - Photoshop 图片在 Android 应用程序开发中无法运行(使用 Eclipse)

c++ - 如何使用 C++ 程序快速地将文件从一个位置复制到另一个位置?

Java SQL 语句没有给出错误,也没有结果集

python - 如何深度复制 pygame Sprite 组?

java - 未定义名为 'org.sakaiproject.logic.SakaiProxy' 的 bean

java - 在Java bean中创建一个进程,获取输出并返回网页

netbeans - 创建 session bean 时出错

Ruby 文件强制复制