Tech Corner

BeanUtils.copyProperties and why it’s not what it looks like

Shripati Bhat
.
Aug 19
.
3-4 mins

Table of contents

Automate your business at $5/day with Engati

REQUEST A DEMO
BeanUtils.copy properties

It's often good if you don't have to talk for 10 mins to make a single point. Similarly, it’s good if you don't need to write 50 lines of code to do a simple task. To bring this layer of abstraction and remove the boilerplate code, many libraries exist such as apache-commons and spring-beans

Let's take a closer look at one of the most powerful and very commonly used utility classes that both of these libraries expose in their own version i.e. BeanUtils.

 

BeanUtils.copyProperties

We've all  worked in multi-tiered applications with the responsibility of different tiers being very clearly defined. So to convert a DTO (data transfer object) to an Entity and back, we usually need to write code that looks like something like this:

Converting data transfer objects to an Entity

Here we're trying to convert an object Bot into its data transfer object BotDTO. The object has 6 fields of primitive data type and a nested object of Config. In the next example, I'm using a builder which is better but still looks pretty verbose.

  

Accomplishing the same task using a builder

But with BeanUtils, we can modify it to look something like this:

How the same code looks on BeanUtils

Looks much neater right? But it’s more than what it looks like in this simple example.

 

Things to be way of in BeanUtils 

1

Uncertainty of fields getting copied

Uncertainty of values being risky can cause major bugs


The above quote from the documentation of apache-commons shows that if your DTO or Entity do not have field signature matching or if your target object has a few nested fields, BeanUtils will not populate those fields for you. So there is no easy way to be sure which fields haven't been copied except by writing elaborate test cases around each field to ensure that the desired fields have been copied.


2

Construction of the objects

Internally these utility libraries use reflection to do all the magic of copying field values from source to target. So if the objects in question have a specific way of constructing using a few mandatory fields in the class or if they are mutable only to a certain level then this utility may not be the best fit (although this utility does provide exclusionFields which then you can handle manually).

 

3

Deep copy issues

The concept of deep copying an object means that all nested objects within an object at all levels of nesting get copied when an attempt is made to copy the parent-level object.

So in this example when we try to copy fields of Bot to BotDTO, fields in Config object also should get copied to ConfigDTO. But BeanUtils.copyProperties does not perform a deep copy of fields and each level has to be created and separately copied.

The more efficient way to code

We're not trying to say that there are all problems with these libraries and we shouldn't use them. This is a very powerful utility tool for simple POJOs and many cases to reduce boilerplate code. But we need to be careful as the library isn't a universal solution and at times, you may need something more custom for your purpose. And along with using this library, we need to absolutely make sure that we write good unit test cases to ensure that all expected fields are getting copied correctly.

Thanks for reading! We hope you found this article helpful.

3x your revenue with Chatbots and Live Chat
Schedule a demo


Shripati Bhat

Shripati is a Senior Director at Engati. He's a technical enthusiast, passionate about designing and building scalable software.

Shripati has a deeply ingrained customer-first ideology and is highly skilled in designing and developing Java/J2EE applications and BigData applications.

Close Icon
Request a Demo!
Get started on Engati with the help of a personalised demo.
Thanks for the information.
We will be shortly getting in touch with you.
Oops! something went wrong!
For any query reach out to us on contact@engati.com
Close Icon
Congratulations! Your demo is recorded.

Select an option on how Engati can help you.

I am looking for a conversational AI engagement solution for the web and other channels.

I would like for a conversational AI engagement solution for WhatsApp as the primary channel

I am an e-commerce store with Shopify. I am looking for a conversational AI engagement solution for my business

I am looking to partner with Engati to build conversational AI solutions for other businesses

continue
Finish
Close Icon
You're a step away from building your Al chatbot

How many customers do you expect to engage in a month?

Less Than 2000

2000-5000

More than 5000

Finish
Close Icon
Thanks for the information.

We will be shortly getting in touch with you.

Close Icon

Contact Us

Please fill in your details and we will contact you shortly.

Thanks for the information.
We will be shortly getting in touch with you.
Oops! Looks like there is a problem.
Never mind, drop us a mail at contact@engati.com