对象存储依赖有两个,一个是新版本,一个是旧版本:
<!--旧版本-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
</dependency>
<!--新版本-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
</dependency>
然而我无论导入哪个版本,maven都无法自动帮我加载jar包里面的依赖,拿旧版本举例,导入旧版本
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
</dependency>
按下Ctrl同时点击spring-cloud-starter-alicloud-oss,进到它的里面,核心依赖spring-cloud-alicloud-oss报红
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
<name>Spring Cloud Starter Alibaba Cloud OSS</name>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alicloud-oss</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
</dependencies>
</project>
同样的,导入新版本时点进去,也有报红的依赖。
我本来想运行测试类,测试一下阿里的对象存储依赖不用配置直接上传文件至阿里的服务器,虽然依赖报红,但我还是想试一试,运行测试类:
@SpringBootTest
public class MallProductApplicationTests {
@Autowired
OSSClient oss;
@Test
public void testUpload() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("C:\\Users\\001\\Desktop\\lena.JPG");
oss.putObject("mallhup", "lena.jpg", inputStream);
oss.shutdown();
System.out.println("上传完成");
}
}
果然运行失败了,报错空指针,于是我分析确实是我的依赖导入出现了问题。
为什么它不能自动导入呢?
我有以下几点推测;
- springboot版本问题,尝试换到和其他博主一样的版本,未解决。
- 清idea缓存,重启,未解决。
- 换成自己指定的maven仓库,未解决。
我决定自己去下载jar包,阿里云的仓库地址,先试试旧版本,搜索spring-cloud-starter-alicloud-oss,下载成功,放到项目的library里面,即
加入之后,这个问题依然没有解决。
反转来了!!我发现是我在运行测试类的时候没有添加测试类@RunWith(SpringRunner.class)!!于是我加上了这个注释,可是依然报错
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'ossStorageProtocolResolver', defined in class path resource [com/alibaba/cloud/spring/boot/oss/autoconfigure/OssAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alibaba/alicloud/oss/OssAutoConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
2021-03-17 14:01:37.522 ERROR 3180 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@49e53c76] to prepare test instance [com.hup.mall.product.MallProductApplicationTests@1aa99005]
java.lang.IllegalStateException: Failed to load ApplicationContext
这回就好办了,报错信息写的很清楚,大致意思就是我在反复导入jar包修改jar包的过程中,ossStorageProtocolResolver这个bean写入重复了。在yml文件中配置一下spring.main.allow-bean-definition-overriding=true就好了。直接copy报错信息里面的
spring:
main:
allow-bean-definition-overriding=true
然后配置之后依然报错:
在Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.main' to org.springframework.boot.SpringApplication
at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:242)
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:218)
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:202)
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:159)
at org.springframework.boot.SpringApplication.bindToSpringApplication(SpringApplication.java:537)
... 29 more
粗心的我把格式写错了,改成下面就好了
spring:
main:
allow-bean-definition-overriding: true
最后测试类运行成功
总结:依赖内部报错,问题不大,重点是要细心。我这次把错误原因搞错了,整个解决问题的方向就歪了,浪费了好多时间。
PS
- 最后的jar包我使用的是旧版本的2.2.0.RELEASE
- 新版本当然也可以用,但是要加入
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-spring-boot-dependencies</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
因为OSS依赖被转移到了alibaba/aliyun-spring-boot仓库中。