How to copy properties from a bean to another bean in different class?
Use BeanUtils
:
import org.apache.commons.beanutils.BeanUtils;
UserBean newObject = new UserBean();
BeanUtils.copyProperties(newObject, oldObject);
Check out the Dozer Framework - its an object to object mapping framework. The idea is that:
- Usually it will map by convention.
- You can override this convention with a mapping file.
. . therefore mapping files are as compact as possible. Its useful for many cases, such as mapping a use-case specify service payload on to the reusable core model objects.
When delivering the SpringSource training courses we used to point out this framework very often.
Edit:
These days try MapStruct.