有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java何时可以使用PropertyUtils。copyProperties自动失败?

我使用PropertyUtils.copyProperties()通过反射复制对象的属性,它过去工作得很好。然而,最近它开始无所作为

它不会抛出异常,但不会复制任何字段。尽管源对象中存在非空字段,但目标对象的所有字段都保持为空

我不知道如何复制这个。对我来说,它一直在发生,但它在一个项目中,我不能在这里发布。该项目使用Play框架,该框架执行一些字节码操作,因此这可能是罪魁祸首

关于什么可能导致这种情况,或者如何调试,有什么建议或想法吗?我可以尝试的其他野外复印机也很受欢迎(我以前试过一次BeanUtils,但因为一些我现在不记得的警告而改用PropertyUtils)


共 (3) 个答案

  1. # 1 楼答案

    我从this answer获取代码并运行它,它失败了,因为我有一个只写字段(仅setter,无getter)。很有可能这就是造成房地产混乱的原因

    我发现调用Introspector.getBeanInfo(MyModel.class).getPropertyDescriptors()只返回部分属性列表。转载于this github repository

    我给Introspector.flushCaches();添加了一个调用,希望它能解决这个问题。。。只是没有

    作为一种解决方法,我实现了一种复制字段的方法,而不是对beanutils进行回复:

    public static <T> void copyFields(T target, T source) throws Exception{
        Class<?> clazz = source.getClass();
    
        for (Field field : clazz.getFields()) {
            Object value = field.get(source);
            field.set(target, value);
        }
    }
    
  2. # 2 楼答案

    我想我明白了。这件事今天发生在我身上。我只是用它做了一些小测试,但它不起作用。代码如下:

     static class TesteP {
    
        private String a;
        private String b;
        private String c;
    
        public String getA() {
            return this.a;
        }
    
    
        public void setA(String a) {
            this.a = a;
        }
    
    
    
        public String getB() {
            return this.b;
        }
    
    
        public void setB(String b) {
            this.b = b;
        }
    
    
        public String getC() {
            return this.c;
        }
    
    
        public void setC(String c) {
            this.c = c;
        }
    
        @Override
        public String toString() {
            return new ToStringBuilder(this.getClass()).add("a", this.a).add("b", this.b).toString();
        }
    
    }
    
     static class RestP {
    
        private String a;
    
        public String getA() {
            return this.a;
        }
    
        public void setA(String a) {
            this.a = a;
        }
    
        @Override
        public String toString() {
            return this.a;
        }
    }
    
    public static void main(String[] args)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        TesteP p = new TesteP();
        p.setA("AAAA");
        p.setB("BBB");
        TesteP pp = new TesteP();
        RestP p2 = new RestP();
        p2.setA("aaaa");
        PropertyUtils.copyProperties(p,p2);
    
    }
    

    它解决了这个问题,公开了这些课程。也许你的一门课是不公开的。在我的案例中,以下是解决方案:

     public static class TesteP {
    
        private String a;
        private String b;
        private String c;
    
    
        public String getA() {
            return this.a;
        }
    
    
        public void setA(String a) {
            this.a = a;
        }
    
    
        public String getB() {
            return this.b;
        }
    
    
        public void setB(String b) {
            this.b = b;
        }
    
    
        public String getC() {
            return this.c;
        }
    
    
        public void setC(String c) {
            this.c = c;
        }
    
        @Override
        public String toString() {
            return new ToStringBuilder(this.getClass()).add("a", this.a).add("b", this.b).toString();
        }
    
    }
    
     public static class RestP {
    
        private String a;
    
        public String getA() {
            return this.a;
        }
    
        public void setA(String a) {
            this.a = a;
        }
    
        @Override
        public String toString() {
            return this.a;
        }
    }
    
    public static void main(String[] args)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        TesteP p = new TesteP();
        p.setA("AAAA");
        p.setB("BBB");
        TesteP pp = new TesteP();
        RestP p2 = new RestP();
        p2.setA("aaaa");
        PropertyUtils.copyProperties(p,p2);
    
    }
    
  3. # 3 楼答案

    推土机是一个更复杂的豆复印机,你可能想试试

    要调试PropertyUtils问题,请创建一个单元测试,然后逐步完成