簡體   English   中英

在 Spring Boot 中以編程方式注冊 Spring Converter

[英]Register Spring Converter Programmatically in Spring Boot

我想以編程方式在 Spring Boot 項目中注冊一個 Spring Converter。 在過去的 Spring 項目中,我像這樣用 XML 完成了...

<!-- Custom converters to allow automatic binding from Http requests parameters to objects -->
<!-- All converters are annotated w/@Component -->
<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <ref bean="stringToAssessmentConverter" />
        </list>
    </property>
</bean>

我試圖弄清楚如何在 Spring Boot 的 SpringBootServletInitializer 中做

更新:我通過將 StringToAssessmentConverter 作為參數傳遞給getConversionService取得了一些進展,但現在我收到 StringToAssessmentConverter 類的"No default constructor found"錯誤。 我不確定為什么 Spring 沒有看到 @Autowired 構造函數。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    ...

    @Bean(name="conversionService")
    public ConversionServiceFactoryBean getConversionService(StringToAssessmentConverter stringToAssessmentConverter) {
        ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();

        Set<Converter> converters = new HashSet<>();

        converters.add(stringToAssessmentConverter);

        bean.setConverters(converters);
        return bean;
    }
}  

這是轉換器的代碼...

 @Component
 public class StringToAssessmentConverter implements Converter<String, Assessment> {

     private AssessmentService assessmentService;

     @Autowired
     public StringToAssessmentConverter(AssessmentService assessmentService) {
         this.assessmentService = assessmentService;
     }

     public Assessment convert(String source) {
         Long id = Long.valueOf(source);
         try {
             return assessmentService.find(id);
         } catch (SecurityException ex) {
             return null;
         }
     }
 }

完全錯誤

Failed to execute goal org.springframework.boot:spring-boot-maven-
plugin:1.3.2.RELEASE:run (default-cli) on project yrdstick: An exception 
occurred while running. null: InvocationTargetException: Error creating 
bean with name
'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPo
stProcessor': Invocation of init method failed; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'conversionService' defined in 
me.jpolete.yrdstick.Application: Unsatisfied dependency expressed through 
constructor argument with index 0 of type 
[me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: : Error 
creating bean with name 'stringToAssessmentConverter' defined in file 
[/yrdstick/target/classes/me/jpolete/yrdstick/websupport
/StringToAssessmentConverter.class]: Instantiation of bean failed; nested 
exception is org.springframework.beans.BeanInstantiationException: Failed 
to instantiate 
[me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default 
constructor found; nested exception is java.lang.NoSuchMethodException: 
me.jpolete.yrdstick.websupport.StringToAssessmentConverter.<init>(); 
nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'stringToAssessmentConverter' defined in file [/yrdstick
/dev/yrdstick/target/classes/me/jpolete/yrdstick/websupport
/StringToAssessmentConverter.class]: Instantiation of bean failed; nested 
exception is org.springframework.beans.BeanInstantiationException: Failed 
to instantiate 
[me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default 
constructor found; nested exception is java.lang.NoSuchMethodException: 
me.jpolete.yrdstick.websupport.StringToAssessmentConverter.<init>()

答案是,您只需要將轉換器注釋為@Component

這是我的轉換器示例

import org.springframework.core.convert.converter.Converter;
@Component
public class DateUtilToDateSQLConverter implements Converter<java.util.Date, Date> {

    @Override
    public Date convert(java.util.Date source) {
        return new Date(source.getTime());
    }
}

然后當 Spring 需要進行轉換時,就會調用轉換器。

我的 Spring Boot 版本: 1.4.1

這是我的解決方案:

TypeConverter 注解:

@Target({ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface TypeConverter {
}

轉換器注冊商:

@Configuration
public class ConverterConfiguration {

    @Autowired(required = false)
    @TypeConverter
    private Set<Converter<?, ?>> autoRegisteredConverters;

    @Autowired(required = false)
    @TypeConverter
    private Set<ConverterFactory<?, ?>> autoRegisteredConverterFactories;

    @Autowired
    private ConverterRegistry converterRegistry;

    @PostConstruct
    public void conversionService() {
        if (autoRegisteredConverters != null) {
            for (Converter<?, ?> converter : autoRegisteredConverters) {
                converterRegistry.addConverter(converter);
            }
        }
        if (autoRegisteredConverterFactories != null) {
            for (ConverterFactory<?, ?> converterFactory : autoRegisteredConverterFactories) {
                converterRegistry.addConverterFactory(converterFactory);
            }
        }
    }

}

然后注釋您的轉換器:

@SuppressWarnings("rawtypes")
@TypeConverter
public class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {

    @SuppressWarnings("unchecked")
    public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
        return new StringToEnum(targetType);
    }

    private final class StringToEnum<T extends Enum> implements Converter<String, T> {

        private Class<T> enumType;

        public StringToEnum(Class<T> enumType) {
            this.enumType = enumType;
        }

        @SuppressWarnings("unchecked")
        public T convert(String source) {
            return (T) Enum.valueOf(this.enumType, source.trim().toUpperCase());
        }
    }
}

**如果你不是在 Spring Boot 上,自動注冊用 @Component 注釋的轉換器(和類似的構造型注釋)被執行,並且你不在 Web Mvc 環境中

@Bean
ConversionService conversionService(){
    ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    Set<Converter<?, ?>> convSet = new HashSet<Converter<?, ?>>();
    convSet.add(new MyConverter()); // or reference bean convSet.add(myConverter());
    factory.setConverters(convSet);
    factory.afterPropertiesSet();
    return factory.getObject();
}

嘗試這個:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Bean
    public AssessmentService assessmentService(){
        return new AssessmentService();
    }

    @Bean
    public StringToAssessmentConverter stringToAssessmentConverter(){
        return new StringToAssessmentConverter(assessmentService());
    }

    @Bean(name="conversionService")
    public ConversionService getConversionService() {
        ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();    
        Set<Converter> converters = new HashSet<Converter>();

        //add the converter
        converters.add(stringToAssessmentConverter()); 

        bean.setConverters(converters);
        return bean.getObject();
    }

    // separate these class into its own java file if necessary
    // Assesment service
    class AssessmentService {}

    //converter
    class StringToAssessmentConverter implements Converter<String, Assessment> {

         private AssessmentService assessmentService;

         @Autowired
         public StringToAssessmentConverter(AssessmentService assessmentService) {
             this.assessmentService = assessmentService;
         }

         public Assessment convert(String source) {
             Long id = Long.valueOf(source);
             try {
                 return assessmentService.find(id);
             } catch (SecurityException ex) {
                 return null;
             }
         }

     }
}

或者如果您的 StringToAssessmentConverter 已經是一個 spring bean:

@Autowired
@Bean(name="conversionService")
public ConversionService getConversionService(StringToAssessmentConverter stringToAssessmentConverter) {
    ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();    
    Set<Converter> converters = new HashSet<Converter>();

    //add the converter
    converters.add(stringToAssessmentConverter); 

    bean.setConverters(converters);
    return bean.getObject();
}

對於 Spring Boot,它看起來像:

public class MvcConfiguration implements WebMvcConfigurer {

  @Override
  public void addFormatters(FormatterRegistry registry) {
    // do not replace with lambda as spring cannot determine source type <S> and target type <T>
    registry.addConverter(new Converter<String, Integer>() {
        @Override
        public Integer convert(String text) {
            if (text == null) {
                return null;
            }
            String trimmed = StringUtils.trimWhitespace(text);
            return trimmed.equals("null") ? null : Integer.valueOf(trimmed);
        }
    });
  }

在 xml 配置中注冊自定義轉換器也有問題。 應該將轉換器 id 添加到 annotation-driver

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="ru.javawebinar.topjava.util.StringToLocalDateConverter"/>
            </set>
        </property>
</bean>

<mvc:annotation-driven conversion-service="conversionService"/>

參考鏈接: Spring MVC。 類型轉換, Spring 核心。 類型轉換

如果需要轉換器將屬性轉換為特定對象,將其注冊為組件可能為時已晚。 我正在使用 spring-boot-2.3.11 並收到錯誤No converter found capable of converting from type [java.lang.String] to type [org.raisercostin.jedio.WritableDirLocation]

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'revobet.feed.lsports.rest.dump-dir' to org.raisercostin.jedio.WritableDirLocation:

    Property: myapp.dump-dir
    Value: file://localhost/C:\Users\raiser/.myapp/cache
    Origin: class path resource [myapp.conf]:-1:1
    Reason: No converter found capable of converting from type [java.lang.String] to type [org.raisercostin.jedio.WritableDirLocation]

Action:

Update your application's configuration

解決方案

public class MyApp implements ApplicationRunner {

  public static void main(String[] args) {
    LocationsConverterConfig.init();
    SpringApplication.run(MyApp.class, args);
  }
}

和轉換器

public class LocationsConverterConfig {
  public static void init() {
    ApplicationConversionService conversionService = (ApplicationConversionService) ApplicationConversionService
      .getSharedInstance();
    log.info("adding JacksonStringToObjectConverter to handle Locations serialization as soon as possible");
    conversionService.addConverter(new JacksonStringToObjectConverter());
  }

  //@Component
  public static class JacksonStringToObjectConverter implements ConditionalGenericConverter {

    public JacksonStringToObjectConverter() {
      log.info("adding {} to handle Locations serialization as soon as possible", JacksonStringToObjectConverter.class);
    }

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
      return Collections.singleton(new ConvertiblePair(Object.class, Object.class));
    }

    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
      return true;
    }

    @Override
    @Nullable
    public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
      ...
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM