How to set property value of a bean?
Commons BeanUtils component provides a class called
PropertyUtils that supplies method for manipulating a bean such as our
Track class below. For this demo we use a
Track class that have a property called
To set the value of a bean we can use the
PropertyUtils.setProperty() method. This method ask for the bean instance whos property value to be set, the property name and the value.
Some exceptions could be thrown by this method, so we need to handle the
NoSuchMethodException. This could happen if we don't have access to the property, or the bean's accessor throws an exception or if the method we tried to manipulate doesn't exist.