阿里云对象存储OSS奇葩异常的隐秘原因

首先,可以正常使用的各组件版本

目前测试正常的各组件详细版本如下:
Spring Cloud:Hoxton.SR5
Spring Boot:2.2.12.RELEASE
spring-cloud-alibaba-dependencies:2.2.1.RELEASE
aliyun-oss-spring-boot-starter:1.1.0
MyBatis Plus:3.4.1
JDK:11.0.9
Nacos:1.4.0

其次,相关配置文件详细内容

(参考文档:《导入OSS依赖不兼容问题》《spring-cloud-starter-alicloud-oss 依赖导入失败问题》

1、application.yml文件:

#  阿里云OSS配置暂时先放在此处(后续会放在Nacos配置中心):
alibaba:
  cloud:
    access-key: L****t
    secret-key: x****Z
    oss:
      endpoint: oss-cn-beijing.aliyuncs.com
      bucket: xxx

2、pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.12.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.xxx.xxxx</groupId>
    <artifactId>xxx-third-party</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xxx-***</name>
    <description>***服务</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR5</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.***.****</groupId>
            <artifactId>****-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--下面是必须添加的阿里云OSS依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>aliyun-oss-spring-boot-starter</artifactId>
            <!--版本号不能少,否则报错如下:
            No qualifying bean of type 'com.aliyun.oss.OSS' available: expected at least 1 bean which alifies as autowire candidate. Dependency annotations:
             {@org.springframework.beans.factory.annotation.Autowired(required=true)}-->
            <version>1.0.0</version>
            <exclusions>
                <!--排除aliyun-java-sdk-oss,因为版本过低    -->
                <exclusion>
                    <groupId>com.aliyun</groupId>
                    <artifactId>aliyun-java-sdk-oss</artifactId>
                </exclusion>
                <!--排除aliyun-sdk-oss,版本过低,是3.1版本的-->
                <exclusion>
                    <groupId>com.aliyun.oss</groupId>
                    <artifactId>aliyun-sdk-oss</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--        导入新的依赖-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.7</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- no more than 2.3.3-->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
		<!--(**重点!!!**)不能随便添加下面的aliyun-sdk-oss依赖,否则将报错:java.lang.ClassNotFoundException: com.aliyun.oss.OSS-->
        <!--<dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <scope>test</scope>
        </dependency>-->
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
</project>
第三、奇葩异常及其隐秘原因

问题的“隐秘原因”在上面的pom.xml中已经注释说明,就是添加了下面的依赖:

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <scope>test</scope>
</dependency>

该依赖应该是使用Maven构建项目时自动添加。
添加该依赖后,启动当前服务的应用程序,会出现一系列莫名其妙的异常。经过多次尝试后,将部分异常收集如下(可能有部分重复或者缺失的):

java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/TestInstancePreDestroyCallback
Caused by: java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.TestInstancePreDestroyCallback

java.lang.IllegalStateException: Error processing condition on com.alibaba.alicloud.context.oss.OssContextAutoConfiguration.ossClient

org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]

Caused by: java.lang.IllegalStateException: @ConditionalOnMissingBean did not specify a bean using type, name or annotation and the attempt to deduce the bean's type failed at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.validate

java.lang.NoClassDefFoundError: com/aliyun/oss/OSS at com.alibaba.alicloud.oss.OssApplicationListener.onApplicationEvent(OssApplicationListener.java:41) ~[spring-cloud-alicloud-oss-2.2.1.RELEASE.jar:2.2.1.RELEASE] 
Caused by: java.lang.ClassNotFoundException: com.aliyun.oss.OSS
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
... 16 common frames omitted

Caused by: java.lang.IllegalStateException: @ConditionalOnMissingBean did not specify a bean using type, name or annotation and the attempt to deduce the bean's type failed at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.validate(OnBeanCondition.java:487) ~[spring-boot-autoconfigure-2.2.12.RELEASE.jar:2.2.12.RELEASE]

Caused by: org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException: Failed to deduce bean type for com.alibaba.cloud.spring.boot.oss.autoconfigure.OssContextAutoConfiguration.ossClient at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.deducedBeanTypeForBeanMethod(OnBeanCondition.java:517) ~[spring-boot-autoconfigure-2.2.12.RELEASE.jar:2.2.12.RELEASE]

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ossClient' defined in class path resource [com/alibaba/cloud/spring/boot/oss/autoconfigure/OssContextAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.aliyun.oss.OSS]: Factory method 'ossClient' threw exception; nested exception is java.lang.IllegalArgumentException: Oss endpoint can't be empty.

Caused by: java.lang.IllegalStateException: @ConditionalOnMissingBean did not specify a bean using type, name or annotation and the attempt to deduce the bean's type failed
	at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.validate(OnBeanCondition.java:487) ~[spring-boot-autoconfigure-2.2.12.RELEASE.jar:2.2.12.RELEASE]
	at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.<init>(OnBeanCondition.java:436) ~[spring-boot-autoconfigure-2.2.12.RELEASE.jar:2.2.12.RELEASE]
java.lang.NoClassDefFoundError: com/aliyun/oss/OSS at com.alibaba.cloud.spring.boot.oss.context.OssApplicationListener.onApplicationEvent(OssApplicationListener.java:40) ~[aliyun-oss-spring-boot-starter-1.0.0.jar:1.0.0] at 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.***.****.thirdparty.****ThirdPartyApplicationTests': Unsatisfied dependency expressed through field 'ossClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.aliyun.oss.OSS' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

直到将scope为test的依赖aliyun-sdk-oss注释或者删除,上述系列异常才消失无踪。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值