How to Copy properties of Object Using PropertyUtils vs BeanUtils

Posted By : Pavan Kumar | 28-Apr-2018

In any application many times we  need to convert an Object's property into another object. so for doing this we have different methodologies in java. So in this blog i will show you that how can we can copy the properties of an object into another object using BeanUtils and PropertyUtils. 
They both are used for copying the properties but having some difference.

BeanUtils:- BeanUtils is a class of Apache commons library in java. it is used to copy, get and set the properties of one source object into another targeted object.

suppose that, if we want to convert a DTO into a domain class. then we can use the BeanUtils class for copying all the properties of DTO class into the domain.

PropertyUtils:- PropertyUtils is also used for the copy properties of objects into other objects. but it has a quite different functionality as compare to BeanUtils.

Note:-  BeanUtils don't create a copy of null values for Wrapper classes in java. it will assign the default value of their primitive  to the wrapper instead of null. while PropertyUtils will set the null in the same case. 

let's have a look at the practical implementation.

suppose we want to create copy of a PlayerDTO into Player class.

PlayerDTO.java

public class PlayerDTO {
    private String name;
    private Integer age;
    private String game;

    public PlayerDTO() {
        //Default Constructor
    }

    public PlayerDTO(String name, Integer age, String game) {
        this.name = name;
        this.age = age;
        this.game = game;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGame() {
        return game;
    }

    public void setGame(String game) {
        this.game = game;
    }
}

Now we will create a Player class in which we will create a copy of PlayerDTO

Player.java

 

public class Player {
    private String name;
    private Integer age;
    private String game;

    public Player() {
        //Default Constructor
    }

    public Player(String name, Integer age, String course) {
        this.name = name;
        this.age = age;
        this.game = course;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGame() {
        return game;
    }

    public void setGame(String game) {
        this.game = game;
    }


    @Override
    public String toString() {
        return "Player{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", game='" + game + '\'' +
                '}';
    }
}


 Now we will create a main class and will see the difference of both functionality.

PlayerCopyTest.java

 

public class PlayerCopyTest {

    public static void main(String[] args)throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {

        PlayerDTO playerDTO=new PlayerDTO("Pawan",null,"Cricket");

        Player player=new Player();
        BeanUtils.copyProperties(player,playerDTO);
        //here it will set the age of player to 0
        System.out.println(player);
        PropertyUtils.copyProperties(player,playerDTO);
        //here it will set the age of player to null
        System.out.println(player);
        


    }
}

About Author

Author Image
Pavan Kumar

Pavan is a bright Java developer. He is a learner by heart and has a passion and profile to adapt various technologies.

Request for Proposal

Name is required

Comment is required

Sending message..