如何在 Springboot 中的转换服务中自动布线

2022-09-04 21:40:06

尝试在 springboot 中访问模型中的转换控件,没有运气。

@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

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

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文档可以发现,您应该声明一个bean。在 XML 配置中,它看起来像这样:ConversionService

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

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

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

答案 2

不完全同意接受的答案,因为会有一个默认值命名,所以你会得到重复的bean异常。取而代之的是 ,下面是零件答案的链接:ConverstionServicemvcConversionServiceaddConverterFormatterRegistry

Java Config equivalent for conversionService / FormattingConversionServiceFactoryBean

此外,您还需要(在某些情况下)将 至少定义为 空的 for ,如下所示:ComponentConversionService

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

这是为了强制弹簧容器发起对以下各项的调用:

class WebMvcConfigurerAdapter {
    ...

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

推荐