如何在springboot中的ConversionService中自动装配

Posted

技术标签:

【中文标题】如何在springboot中的ConversionService中自动装配【英文标题】:How to Autowired in ConversionService in springboot 【发布时间】:2015-07-14 09:34:25 【问题描述】:

试图在springboot中访问模型中的ConversionControl,没有成功。

@Component
public class CityHelperService  

    @Autowired
    ConversionService conversionService;// = ConversionServiceFactory.registerConverters();

    public City toEntity(CityDTO dto)
        City entity = conversionService.convert(dto, City.class);
        return entity;
    

    public CityDTO toDTO(City entity)
        CityDTO dto = conversionService.convert(entity, CityDTO.class);
        return dto;
    

它显示以下错误:

Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lumiin.mytalk.model.CityModel com.lumiin.mytalk.controllers.CityController.cityModel;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityModel' defined in file : Unsatisfied dependency expressed through constructor argument with index 1 of type [com.lumiin.mytalk.dao.CityHelperService]: : Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true);
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)

【问题讨论】:

【参考方案1】:

显然没有可用的ConversionService bean,从最后一个嵌套异常判断:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

查看Spring documentation 显示,您应该声明一个ConversionService bean。在 XML 配置中,它看起来像这样:

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="example.MyCustomConverter"/>
        </set>
    </property>
</bean>

由于您使用的是 Spring Boot,我假设您正在以编程方式创建上下文,因此您应该创建一个带有 @Bean 注释的方法,该方法返回一个 ConverstionService,如下所示 (explained here):

@Bean(name="conversionService")
public ConversionService getConversionService() 
    ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
    bean.setConverters(...); //add converters
    bean.afterPropertiesSet();
    return bean.getObject();

【讨论】:

感谢您的 cmets..但是什么是 bean.setConverters(...);它显示一个错误 - org.springframework.core.convert.ConverterNotFoundException: 没有找到能够从 com.lumiin.mytalk.dto.CityDTO 类型转换为 com.lumiin.mytalk.model.City 类型的转换器 你可能没有指定一个转换器——你必须创建一个类,它实现接口Converter&lt;City, CityDTO&gt;,另一个相反,然后在ConversionServiceFactoryBean中注册。请看本教程:javabeat.net/introduction-to-spring-converters-and-formatters 在Javaconfig中,你甚至不需要像上面的例子一样从ConversionServiceFactoryBean返回一个ConverstionService,只需要返回一个ConversionServiceFactoryBean bean,spring会为你做剩下的 【参考方案2】:

不完全同意接受的答案,因为会有一个名为 mvcConversionService 的默认 ConverstionService,所以你会得到 duplicate bean exception。而不是addConverterFormatterRegistry,这里是部分答案的链接:

Java Config equivalent for conversionService / FormattingConversionServiceFactoryBean

您还需要(在某些情况下)为ConversionService 定义至少一个空的Component,如下所示:

@Component @Primary
public class MyConversionService extends DefaultConversionService implements ConversionService 
    // an empty ConversionService to initiate call to register converters

这是强制spring容器发起调用:

class WebMvcConfigurerAdapter 
    ...

    public void addFormatters(FormatterRegistry registry) 
         //registry.addConverter(...);
    

【讨论】:

只有在项目中有 org.springframework.boot:spring-boot-starter-web 依赖项时,mvcConversionService 才会出现

以上是关于如何在springboot中的ConversionService中自动装配的主要内容,如果未能解决你的问题,请参考以下文章

Entity Framework Core中的值转换器(Value Conversion)

Entity Framework Core 中的值转换器(Value Conversion)

我不知道我的一个变量有啥问题。[线程“main”java.util.UnknownFormatConversionException 中的异常:Conversion = 'm']

怎么去掉Xcode工程中的某种类型的警告 Implicit conversion loses integer precision: 'NSInteger' (aka 'long

怎么去掉Xcodeproject中的某种类型的警告 Implicit conversion loses integer precision: &#39;NSInteger&#39; (

Leetcode:ZigZag Conversion