异常解决:多个ConversionService,注入失败

异常解决:多个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:\Java\develop\maven_repository\org\springframework\integration\spring-integration-core\5.2.5.RELEASE\spring-integration-core-5.2.5.RELEASE.jar!\org\springframework\integration\config\spring-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自动注入的时候就出现错误了

### 解决PyCharm无法加载Conda虚拟环境的方法 #### 配置设置 为了使 PyCharm 能够成功识别并使用 Conda 创建的虚拟环境,需确保 Anaconda 的路径已正确添加至系统的环境变量中[^1]。这一步骤至关重要,因为只有当 Python 解释器及其关联工具被加入 PATH 后,IDE 才能顺利找到它们。 对于 Windows 用户而言,在安装 Anaconda 时,默认情况下会询问是否将它添加到系统路径里;如果当时选择了否,则现在应该手动完成此操作。具体做法是在“高级系统设置”的“环境变量”选项内编辑 `Path` 变量,追加 Anaconda 安装目录下的 Scripts 文件夹位置。 另外,建议每次新建项目前都通过命令行先激活目标 conda env: ```bash conda activate myenvname ``` 接着再启动 IDE 进入工作区,这样有助于减少兼容性方面的问题发生概率。 #### 常见错误及修复方法 ##### 错误一:未发现任何解释器 症状表现为打开 PyCharm 新建工程向导页面找不到由 Conda 构建出来的 interpreter 列表项。此时应前往 Preferences/Settings -> Project:...->Python Interpreter 下方点击齿轮图标选择 Add...按钮来指定自定义的位置。按照提示浏览定位到对应版本 python.exe 的绝对地址即可解决问题。 ##### 错误二:权限不足导致 DLL 加载失败 有时即使指定了正确的解释器路径,仍可能遇到由于缺乏适当的操作系统级许可而引发的功能缺失现象。特别是涉及到调用某些特定类型的动态链接库 (Dynamic Link Library, .dll) 时尤为明显。因此拥有管理员身份执行相关动作显得尤为重要——无论是从终端还是图形界面触发创建新 venv 流程均如此处理能够有效规避此类隐患。 ##### 错误三:网络连接异常引起依赖下载超时 部分开发者反馈过因网速慢或者其他因素造成 pip install 操作中途断开进而影响整个项目的初始化进度条卡住的情况。对此可尝试调整镜像源加速获取速度或是离线模式预先准备好所需资源包后再继续后续步骤。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值