小编jpo*_*ete的帖子

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

我想以编程方式在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>
Run Code Online (Sandbox Code Playgroud)

我试图找出如何在Spring Boot的SpringBootServletInitializer中做

更新:我通过传递StringToAssessmentConverter作为参数取得了一些进展getConversionService,但现在我收到了"No default constructor found"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;
    }
}  
Run Code Online (Sandbox Code Playgroud)

这是转换器的代码......

 @Component
 public …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

10
推荐指数
2
解决办法
3万
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1

spring-mvc ×1