I refactored the setup and now its working fine. Part of the issue was that all the BeanUtils methods are static so that any bean implementation had to accomodate that.
I refactored at follows:
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.FluentPropertyBeanIntrospector;
import org.apache.commons.beanutils.PropertyUtils;
import java.lang.reflect.InvocationTargetException;
public class MyBeanUtils extends BeanUtils {
public MyBeanUtils() {
super();
}
public static void copyProperty(Object obj1, String string, Object obj2) throws InvocationTargetException, IllegalAccessException {
PropertyUtils.addBeanIntrospector(new FluentPropertyBeanIntrospector());
BeanUtils.copyProperty(obj1, string, obj2);
}
}
, then the bean class:
import mypath.MyBeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanUtilsConfiguration {
@Bean(name = "myBeanUtils")
public MyBeanUtils beanUtils() {
return new MyBeanUtils();
}
}
, then on the invoking class simply:
.
.
@Autowired
private MyBeanUtils myBeanUtils;
.
.
dlmyBeanUtils.copyProperty(objectToUpdate, field, value);
.
.
This works fine. Problem solved.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…