Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

Issue creating BeanUtils Method as Java Bean

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.

question from:https://stackoverflow.com/questions/66052707/issue-creating-beanutils-method-as-java-bean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...