概述
异常解决:多个ConversionService,注入失败
1.错误信息:
Description:
Parameter 0 of method getGenericConversionService in com.lianxin.werm.service.config.web.WebConfiguration required a single bean, but 2 were found:
- mvcConversionService: defined by method 'mvcConversionService' in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
- integrationConversionService: defined in null
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Disconnected from the target VM, address: '127.0.0.1:52144', transport: 'socket'
Process finished with exit code 1
2.异常原因:
自动注入 ConversionService[转换服务] 的时候,发现2个该类型的Bean。一个名称为 mvcConversionService,一个名称为integrationConversionService,通过查看错误提示信息,发现 mvcConversionService 是springboot的 WebMvcAutoConfiguration[mvc自动配置]里面的,但是另外一个 integrationConversionService 的错误描述只是defined in null
- 第一步能想到的解决方法就是使用
@Qualifier("beanName")
去指定使用一个bean。比如
@Bean
public GenericConversionService getGenericConversionService(
@Autowired @Qualifier("mvcConversionService") GenericConversionService conversionService) {
conversionService.addConverter(new ArrayStringConvert());
System.out.println("类型转换已加入!");
return conversionService;
}
这个确实能够解决这个问题。但是另外一个integrationConversionService
是什么东西呢
3.报异常的位置:
参数里的:
@Autowired GenericConversionService conversionService
自动注入失败
@Configuration
public class WebConfiguration {
@Bean
public GenericConversionService getGenericConversionService( @Autowired GenericConversionService conversionService) {
conversionService.addConverter(new ArrayStringConvert());
System.out.println("类型转换已加入!");
return conversionService;
}
}
import java.util.List;
import org.springframework.core.convert.converter.Converter;
import com.xxx.xx.bean.json.ArrayString;
public class ArrayStringConvert implements Converter<List<String>, ArrayString> {
@Override
public ArrayString convert(List<String> source) {
return new ArrayString(source);
}
}
点击左边的绿色叶子,发现有2个mvcConversionService
1个位于spring框架里
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
// ...
@Bean
public FormattingConversionService mvcConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
this.addFormatters(conversionService);
return conversionService;
}
// ...
}
1个位于springboot的webMvc自动配置里
// 其他注解
@Configuration(proxyBeanMethods = false)
public class WebMvcAutoConfiguration {
// ...
@Bean
@Override
public FormattingConversionService mvcConversionService() {
WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat());
addFormatters(conversionService);
return conversionService;
}
// ...
}
这是一个配置类,所以@Bean能够生效,通过打断点发现使用的就是这个springboot提供的mvcConversionService
,但是integrationConversionService
这个Bean位于哪里
通过全局搜索(ctrl+n),并找不到这个Bean。
最后通过全文本搜索(ctrl+shift+F),在一个xsd文件里面找到了integrationConversionService
位于:
D:Javadevelopmaven_repositoryorgspringframeworkintegrationspring-integration-core5.2.5.RELEASEspring-integration-core-5.2.5.RELEASE.jar!orgspringframeworkintegrationconfigspring-integration-5.2.xsd
找到如下描述:
<xsd:documentation>
Allows you to register Converters (implementation of the Converter interface) that will
be automatically registered with the ConversionService. ConversionService itself does not have to be
explicitly defined since the default ConversionService will be registered under the name 'integrationConversionService'
unless bean with this name is already registered.
</xsd:documentation>
// 译:允许你注册转换器(转换器接口的实现),它将会自动注册到转换服务中。
ConversionService本身不一定非得是明确定义,因为默认的ConversionService将以'integrationConversionService'的名义注册。
除非这个名字的bean已经被注册了。
大致意思就是:spring框架有一个默认的转换服务(ConversionService
)是用integrationConversionService
名称注册的,然后springboot也提供了一个ConversionService接口的实现,该类是WebConversionService。所以在使用@autowired
自动注入的时候就出现错误了
最后
以上就是虚幻小土豆为你收集整理的异常解决:多个ConversionService,注入失败的全部内容,希望文章能够帮你解决异常解决:多个ConversionService,注入失败所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复