java - 如何在 springboot 的 ConversionService 中 Autowiring

标签 java spring spring-mvc spring-boot

尝试在 spring boot 中访问模型中的 Conversion Control,没有成功。

@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)}

最佳答案

显然没有可用的 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();
}

关于java - 如何在 springboot 的 ConversionService 中 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30039619/

相关文章:

java - 如何为类型类的数组编写 'get/set method'?

java - 接口(interface)中的 Pojo

java - 如何在 Spring Boot 应用程序中实现长轮询 REST 端点?

java - 由回收器 View 按钮和notifyItemRemoved插入

java - WebElement.clear() 触发 javascript 更改事件 - 替代方案?

eclipse - rest 服务在 Eclipse 中部署时有效,但在 Tomcat 中无效

java - 使用 Spring 服务的构建器类

spring - 使用 springmockk 时 Kotlintest 不执行测试

java - Spring CORS 设置

Java自定义验证注解