Java 如何在springboot的ConversionService中自动连接
试图访问springboot中模型中的ConversionControl,运气不佳Java 如何在springboot的ConversionService中自动连接,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,试图访问springboot中模型中的ConversionControl,运气不佳 @Component public class CityHelperService { @Autowired ConversionService conversionService;// = ConversionServiceFactory.registerConverters(); public City toEntity(CityDTO dto){ City en
@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.
查看后发现,您应该声明一个ConversionService
bean。在XML配置中,它将如下所示:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="example.MyCustomConverter"/>
</set>
</property>
</bean>
不完全同意接受的答案,因为将有一个名为
mvcConversionService
的默认ConversationService
,因此您将得到重复bean异常。相反,将Converter添加到FormatterRegistry
,以下是部分答案的链接:
您还需要(在某些情况下)为转换服务
定义至少一个空的组件
,如下所示:
@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(...);
}
}
谢谢你的评论。但是什么是bean.setConverters(…);它显示了一个错误-org.springframework.core.convert.ConverterNotFoundException:找不到能够从com.lumiin.mytalk.dto.CityDTO类型转换为com.lumiin.mytalk.model.CityYou可能没有指定转换器-您必须创建一个类,该类实现接口
转换器
,另一个反过来,然后在转换服务工厂bean
中注册这两个。请看本教程:在Javaconfig中,您甚至不需要像上面的示例那样从ConversionServiceFactoryBean
返回ConversationService
,只需返回一个ConversionServiceFactoryBean
bean,spring将为您完成其余的工作mvcConversionService
仅当您的项目中有org.springframework.boot:spring boot starter web
依赖项时才会出现
class WebMvcConfigurerAdapter {
...
public void addFormatters(FormatterRegistry registry) {
//registry.addConverter(...);
}
}