Issue
I currently have a method that does something like the following:
public void updateField(String user, String field, Date value) {
PropertyUtils.addBeanIntrospector( new FluidPropertyBeanIntrospector());
MyClass myclass = getMyClass(user);
BeanUtils.copyProperty(myClass, field, value);
}
This works fine but I want the copyProperty() method to be a bean so that the bean introspector is not called each time the method is invoked.
I attempted this:
@Configuration
public class BeanUtilsConfig() {
@Bean
public void updateProperty(Myclass myclass, String field, Date Value) {
PropertyUtils.addBeanIntrospector(new FluentPropertyBeanIntrospector());
BeanUtils.copyProperty(singleMetadataDocumentDto, field, value);
}
}
, and at startup get the following error:
Description:
Parameter 1 of method updateProperty in dl.datalake.metadata.config.BeanUtilsConfiguration required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
I guess I doing something basically wrong with defining bean. I would be grateful for any help. Thanks.
Solution
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.
Answered By - Timothy Clotworthy