目前测试正常的各组件详细版本如下:
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文件:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.12.RELEASEversion>
<relativePath/>
parent>
<groupId>com.xxx.xxxxgroupId>
<artifactId>xxx-third-partyartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>xxx-***name>
<description>***服务description>
<properties>
<java.version>11java.version>
<spring-cloud.version>Hoxton.SR5spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>com.***.****groupId>
<artifactId>****-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-oss-spring-boot-starterartifactId>
<version>1.0.0version>
<exclusions>
<exclusion>
<groupId>com.aliyungroupId>
<artifactId>aliyun-java-sdk-ossartifactId>
exclusion>
<exclusion>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.aliyungroupId>
<artifactId>aliyun-java-sdk-coreartifactId>
<version>4.5.7version>
dependency>
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.10.2version>
dependency>
<dependency>
<groupId>javax.xml.bindgroupId>
<artifactId>jaxb-apiartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>javax.activationgroupId>
<artifactId>activationartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.glassfish.jaxbgroupId>
<artifactId>jaxb-runtimeartifactId>
<version>2.3.3version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
<version>2.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.1.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
repository>
repositories>
project>
问题的“隐秘原因”在上面的pom.xml中已经注释说明,就是添加了下面的依赖:
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<scope>testscope>
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注释或者删除,上述系列异常才消失无踪。