现象:![在这里插入图片描述](https://img-blog.csdnimg.cn/a25074bf6b9549ee9d827f66c97873bb.png)
服务启动过程中直接报:Field ossClient in com.walshling.gulimall.thirdparty.utils.OssOperateProvider required a bean of type ‘com.aliyun.oss.OSSClient’ that could not be found.
环境:
SpringBoot 2.2.1.RELEASE
SpringCloud Hoxton.SR12
SpringCloud Alibaba 2.1.0.RELEASE
oss依赖
解决思路:
问题涉及到OssClient组件加载,所以从SpringBoot容器启动注册组件流程入手(断点调试):
1、容器注入组件首先会从EnableAutoConfiguration中选择
2、初始化容器加载哪些组件取决于AutoConfigurationImportSelector
3、找到Selector选择器中selectImports类getAutoConfigurationEntry方法
4、筛选组件列表
5、从configurations列表中可以看到初始化容器会给我们加载的所有Configuration组件,在其中找到oss相关的:
6、排查以上几个类中有ossClient实例化和初始化方法内容:
7、以上所有@Conditional的条件注入都满足的情况下排查ossClient方法返回的结果是否和我们写的有差异
8、这里的ossClient返回的OSS是接口,所以需要在代码中定义OSS ossClient而不是OssClient ossClient