代码之家  ›  专栏  ›  技术社区  ›  carrier

如何使用BeanUtils copyProperties将布尔值复制到布尔值?

  •  1
  • carrier  · 技术社区  · 17 年前

    BeanUtils copyProperties开箱即用,似乎无法处理从布尔对象属性到布尔基元属性的复制。

    我想我可以创建并注册一个转换器来处理这个问题,但这似乎不起作用。

    那么,如何使用BeanUtils将属性从类源复制到类目标,其中:

    public class Destination {
    
        private boolean property;
    
        public boolean isProperty() {
            return property;
        }
    
        public void setProperty(boolean property) {
            this.property = property;
        }
    }
    
    
    public class Source{
    
        private Boolean property;
    
        public Boolean getProperty() {
            return property;
        }
    
        public void setProperty(Boolean property) {
            this.property = property;
        }
    }
    
    3 回复  |  直到 14 年前
        1
  •  2
  •   user5508898 user5508898    10 年前
    try creating both 
    /*by default beanutils copyproperties looks for below method if you use either apache or spring flavour of beanutils.
    always prefer using apache 1.9.2 ( fixed many bugs) but bit slow compared with spring beanutils.*/
     public Boolean getProperty() {
            return property;
        }
    //which is used by some frameworks 
     public Boolean isProperty() {
            return property;
        }
    
        2
  •  0
  •   Boris Pavlović    17 年前

    事实上,反之亦然:

    public static void main(String[] args) throws Exception {
        Source d = new Source();
        d.setProperty(Boolean.TRUE);
        BeanMap beanMap = new BeanMap(d);
    
        Destination s = new Destination();
        BeanUtils.populate(s, beanMap);
        System.out.println("s.getProperty()=" + s.isProperty());
    }
    
        3
  •  0
  •   CSchulz cL83    14 年前
    public class Destination {
        private boolean property;
    
        // code getProperty() instead
        public boolean isProperty() {
            return property;
        }
    
        public void setProperty(boolean property) {
            this.property = property;
        }
    }