SpringBoot项目导入aliyun oss starter依赖后启动报错问题

 更新时间:2024年01月13日 09:35:03   作者:庸人冲  
这篇文章主要介绍了SpringBoot项目导入aliyun oss starter依赖后启动报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

描述

参考 官方文档 进行环境搭建:

版本信息:

  • spring boot: 2.6.6
  • spring cloud:2021.0.1
  • spring cloud alibaba:2021.0.1.0
  • aliyun-spring-boot-dependencies:1.0.0

公共项目 gulimall-common 中引入 aliyun oss starter :

    <dependencies>
    	<!--省略其它依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>aliyun-oss-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>aliyun-spring-boot-dependencies</artifactId>
                <version>1.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

其它项目 gulimall-product 中引入 gulimall-common:

<dependencies>
   <dependency>
      <groupId>com.yrc.gulimall</groupId>
      <artifactId>gulimall-common</artifactId>
      <version>0.0.1-SNAPSHOT</version>
   </dependency>
</dependencies>

启动 gulimall-product 项目,错误内容如下:

2022-04-10 11:13:31.081  WARN 22884 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aliCloudEdasSdk' defined in class path resource [com/alibaba/cloud/spring/boot/context/autoconfigure/EdasContextAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.cloud.context.edas.AliCloudEdasSdk]: Factory method 'aliCloudEdasSdk' threw exception; nested exception is java.lang.NoSuchMethodError: com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;
2022-04-10 11:13:31.085  INFO 22884 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-04-10 11:13:31.095  INFO 22884 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-04-10 11:13:31.116 ERROR 22884 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    com.alibaba.cloud.context.AliCloudSdk.<init>(AliCloudSdk.java:76)

The following method did not exist:

    com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;

The calling method's class, com.alibaba.cloud.context.AliCloudSdk, was loaded from the following location:

    jar:file:/D:/maven/respository/com/alibaba/cloud/alicloud-context/1.0.5/alicloud-context-1.0.5.jar!/com/alibaba/cloud/context/AliCloudSdk.class

The called method's class, com.aliyuncs.profile.DefaultProfile, is available from the following locations:

    jar:file:/D:/maven/respository/com/aliyun/aliyun-java-sdk-core/3.4.0/aliyun-java-sdk-core-3.4.0.jar!/com/aliyuncs/profile/DefaultProfile.class

The called method's class hierarchy was loaded from the following locations:

    com.aliyuncs.profile.DefaultProfile: file:/D:/maven/respository/com/aliyun/aliyun-java-sdk-core/3.4.0/aliyun-java-sdk-core-3.4.0.jar


Action:

Correct the classpath of your application so that it contains compatible versions of the classes com.alibaba.cloud.context.AliCloudSdk and com.aliyuncs.profile.DefaultProfile

2022-04-10 11:13:31.117  WARN 22884 --- [      Thread-16] c.a.n.common.http.HttpClientBeanHolder   : [HttpClientBeanHolder] Start destroying common HttpClient
2022-04-10 11:13:31.118  WARN 22884 --- [      Thread-16] c.a.n.common.http.HttpClientBeanHolder   : [HttpClientBeanHolder] Destruction of the end

Process finished with exit code 1

原因

gulimall-common 项目中:

aliyun-oss-spring-boot-starter 默认引入的 aliyun-java-sdk-core 是 3.4.0 版本

-

但是 aliyun-spring-boot-dependencies 中对 aliyun-java-sdk-core 版本管理为:4.5.0

-

gulimall-product 引用了 gulimall-common 的依赖,但是没有引入 aliyun-spring-boot-dependencies 依赖管理,所以两个项目的 aliyun-java-sdk-core 版本不一致,就会会导致项目 gulimall-product 启动失败:

-

解决方法

在公共项目 gulimall-common 中排除 aliyun-oss-spring-boot-starter 默认的 aliyun-java-sdk-core ,单独引入 4.5.0 版本的 aliyun-java-sdk-core

<dependencies>
  <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>aliyun-oss-spring-boot-starter</artifactId>
      <exclusions>
      	<!--排除默认版本的依赖-->
          <exclusion>
              <groupId>com.aliyun</groupId>
              <artifactId>aliyun-java-sdk-core</artifactId>
          </exclusion>
      </exclusions>
  </dependency>
  <dependency>
  	<!--引入4.5.0 版本依赖-->
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.5.0</version>
  </dependency>
</dependencies>

再次启动项目成功:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • JAVA设计模式之建造者模式原理与用法详解

    JAVA设计模式之建造者模式原理与用法详解

    这篇文章主要介绍了JAVA设计模式之建造者模式,简单说明了建造者模式的原理、组成,并结合实例形式分析了java建造者模式的定义与用法,需要的朋友可以参考下
    2017-08-08
  • java LocalDateTime加时间,计算两个时间的差方式

    java LocalDateTime加时间,计算两个时间的差方式

    文章介绍了如何在Java中使用LocalDateTime类添加时间并计算两个时间的差值,通过比较来总结个人经验,并鼓励读者参考和支持脚本之家
    2025-03-03
  • MyBatis使用雪花ID的实现

    MyBatis使用雪花ID的实现

    本文主要介绍了MyBatis使用雪花ID的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • vue3实现一个todo-list

    vue3实现一个todo-list

    这篇文章主要为大家详细介绍了基于vuejs实现一个todolist项目,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能给你带来帮助
    2021-08-08
  • java多文件压缩下载的解决方法

    java多文件压缩下载的解决方法

    这篇文章主要为大家详细介绍了java多文件压缩下载的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 详解Java设计模式编程中的依赖倒置原则

    详解Java设计模式编程中的依赖倒置原则

    这篇文章主要介绍了详解Java设计模式中的依赖倒置原则,针对面对对象编程中的抽象的运用,需要的朋友可以参考下
    2016-02-02
  • Java线程的三种创建方式

    Java线程的三种创建方式

    这篇文章主要给大家分享的是ava线程的三种创建方式,Thread、Runnable和Thread、Runnable和Thread,想了解具体方式的小伙伴可以参考下面文章内容,希望对你有所帮助
    2021-11-11
  • Spring中@Conditional注解用法详解

    Spring中@Conditional注解用法详解

    这篇文章主要介绍了Spring中@Conditional注解用法详解,@Conditional是Spring4版本新提供的一种注解,它的作用是按照设定的条件进行判断,把满足判断条件的bean注册到Spring容器,需要的朋友可以参考下
    2023-11-11
  • Spring集成Mybatis过程详细讲解

    Spring集成Mybatis过程详细讲解

    mybatis-plus是一个Mybatis的增强工具,在Mybatis的基础上只做增强不做改变,为简化开发、提高效率而生,下面这篇文章主要给大家介绍了关于SpringBoot整合Mybatis-plus案例及用法实例的相关资料,需要的朋友可以参考下
    2023-03-03
  • 详解Java如何实现百万数据excel导出功能

    详解Java如何实现百万数据excel导出功能

    这篇文章主要为大家详细介绍了Java如何实现百万数据excel导出功能,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2023-02-02

最新评论