导入阿里云OSS对象存储依赖失败引发的一系列问题(已解决)

对象存储依赖有两个,一个是新版本,一个是旧版本:

        <!--旧版本-->
        <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("上传完成");
    }
}

果然运行失败了,报错空指针,于是我分析确实是我的依赖导入出现了问题。

为什么它不能自动导入呢?
我有以下几点推测;

  1. springboot版本问题,尝试换到和其他博主一样的版本,未解决。
  2. 清idea缓存,重启,未解决。
  3. 换成自己指定的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

  1. 最后的jar包我使用的是旧版本的2.2.0.RELEASE
  2. 新版本当然也可以用,但是要加入
            <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仓库中。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
SpringBoot可以通过整合阿里云OSS对象存储服务来实现文件上传和管理功能。具体实现可以参考以下步骤: 1. 在service层定义FileService接口,该接口包含上传文件到阿里云OSS的方法。例如,可以使用MultipartFile作为参数,返回上传成功后的文件URL。 2. 在controller层编写FileApiController类,该类使用@RestController注解标识为控制器,并使用@RequestMapping注解指定请求路径。在该类中,通过@Autowired注入FileService,并在文件上传的接口方法中调用FileService的上传文件方法并返回上传成功后的文件URL。 3. 在配置文件中配置阿里云OSS的相关信息,包括accessKey、secretKey、bucketName等。可以使用SpringBoot提供的@ConfigurationProperties注解来读取配置文件中的信息。 4. 在pom.xml文件中添加阿里云OSS SDK的依赖。 5. 编写上传文件的前端界面,可以使用HTML或者前端框架如Vue.js、React等。 通过以上步骤的实现,SpringBoot就可以整合阿里云OSS对象存储服务,实现文件上传和管理功能。这样可以将文件存储在阿里云OSS中,提高文件的安全性和可靠性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合阿里云OSS对象存储服务的实现](https://download.csdn.net/download/weixin_38649091/12721580)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [全网最详细SpringBoot、SpringCloud整合阿里云OSS对象存储服务](https://blog.csdn.net/weixin_55076626/article/details/127924003)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值