【问题标题】:How could I create a custom dozer initializer如何创建自定义推土机初始化程序
【发布时间】:2017-12-04 13:23:30
【问题描述】:

是否可以创建一个自定义推土机初始化程序,以便它接受一个带有构造函数的参数,而不仅仅是一个空的构造函数?

例如next转换器由于dozer无法初始化而失败,并抛出java.lang.InstantiationException

public class MyCustomDozerConverter extends DozerConverter<MyObject, String> {

    private static String myParameter;

    // How could dozer accepts this constructor?
    public MyCustomDozerConverter(String myParameter) {
        super(MyObject.class, String.class);
        this.myParameter = myParameter;
    }

    @Override
    public String convertTo(MyObject source, String destination) {      
        // Using value of myParamter which passed in constructor
        // business logic
        return destination;
    }

    @Override
    public MyObject convertFrom(String source, MyObject destination) {
        // business logic
        return null;
    }
}

如果可能的话,我怎样才能将此参数发送给构造函数,使其成为动态而不是静态的?

注意:我在 spring-boot 项目中使用推土机

【问题讨论】:

    标签: java spring-boot dozer


    【解决方案1】:

    在你的配置类中你需要添加这个部分:

    @Bean
    public DozerBeanMapper mapper() throws IOException {
        List<String> mappingFiles = new ArrayList<String>();
        List<CustomConverter> customConverters = new ArrayList<CustomConverter>();
        customConverters.add(new MyCustomDozerConverter(""));
    
        DozerBeanMapper mapper = new DozerBeanMapper();
        mapper.setMappingFiles(mappingFiles);
        mapper.setCustomConverters(customConverters);
    
        return mapper;
    }
    

    这样,dozer 将使用您在 mapper.setCustomConverters(customConverters) 方法中设置的 MyCustomConverter 实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 2023-03-28
      • 2020-11-07
      相关资源
      最近更新 更多