java - 在 Spring Boot 中以编程方式注册 Spring Converter

标签 java spring spring-mvc 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,我取得了一些进展,但现在我得到了一个 "No default constructor发现 StringToAssessmentConverter 类的“ 错误。我不确定为什么 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

关于java - 在 Spring Boot 中以编程方式注册 Spring Converter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35025550/

相关文章:

java - 在 lucene 索引中存储和检索 Json 对象

java - 环境变量被 Spring Value 注释覆盖

Spring aspectj jar未正确配置

java - 如何在 Spring MVC 中为不存在的资源设置 url 映射?

angular - 部署在本地 tomcat 服务器上的 Angular 应用程序的 url 不会在将 url 粘贴到浏览器的新选项卡上时加载组件

Java:for循环:不同的循环样式

java - 查找除数

java - Spring - 从 jar 文件使用/加载上下文文件

linux - 无法找到错误的位置

java - ubuntu上JDK下无法使用javac命令编译java文件(APARAPI)