--------------------以下为原文:------------------------------
目前,使用阿里云OSS时,能勉强使用的版本组合为:
Spring Cloud:Hoxton.SR8;
Spring Boot:2.3.7.RELEASE;
spring-cloud-alibaba-dependencies:2.2.0.RELEASE;
spring-cloud-starter-alicloud-oss:2.2.0.RELEASE(该本版号可不指定);
MyBatis Plus:3.4.1;
JDK:11.0.9;
Nacos:1.4.0。
这就是本文要说的临时解决方案。之所以说它临时,是因为该方案无法保证启动应用程序和OSS上传文件同时成功!
具体方法:启动应用程序之前,先将spring-cloud-starter-alicloud-oss依赖注释掉。等应用程序启动成功之后,再取消依赖的注释,使用OSS文件上传功能。
-------------------------------------------强制分割线----------------------------------------
以下是测试过程中的部分异常记录:
按照官方的github项目示例《阿里云Spring Boot OSS Simple》,将aliyun-oss-spring-boot-starter添加到pom.xml依赖中,示例如下:
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-oss-spring-boot-starterartifactId>
dependency>
并在application.properties中添加access-key、secret-key和endpoint:
alibaba.cloud.access-key=LT***t
alibaba.cloud.secret-key=xi***UZ
alibaba.cloud.oss.endpoint=oss-cn-beijing.aliyuncs.com
测试启动应用,启动失败,部分异常如下:
2021-01-03 21:51:58.864 ERROR 16672 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Error processing condition on com.alibaba.alicloud.context.oss.OssContextAutoConfiguration.ossClient
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at 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.0.RELEASE.jar:2.2.0.RELEASE]
at com.alibaba.alicloud.oss.OssApplicationListener.onApplicationEvent(OssApplicationListener.java:34) ~[spring-cloud-alicloud-oss-2.2.0.RELEASE.jar:2.2.0.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
一番搜索之后,看到下面两篇博文和本文遇到的问题一致:
《解决导入aliyun-oss-spring-boot-starter导入依赖报错unknown》
《解决aliyun-oss-spring-boot-starter导入报错的问题》
在dependencyManagement中添加aliyun-spring-boot-dependencies依赖,如下所示:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-spring-boot-dependenciesartifactId>
<version>1.0.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
尝试启动,仍然报错。之后,看到了一篇反对上述方案的文章:
《解决Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown 文件上传报错aliCloudEdasSdk解决》
再结合《导入aliyun-oss-spring-boot-starter依赖报错Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown的解决方法》,便采用了比较旧的方案。即,在pom.xml中添加下面的依赖:
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alicloud-ossartifactId>
dependency>
然后,在application.yml中添加:
spring:
datasource:
username: ***
password: ***
url: jdbc:mysql://192.****:3306/***
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
alicloud:
access-key: L***t
secret-key: x***Z
oss:
endpoint: oss-cn-beijing.aliyuncs.com
这时,启动应用,还是失败,报错同上。但此时测试OSS上传文件,已经可以上传成功。不过,还有一些其它异常,详细如下:
测试文件上传成功!
java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/TestInstancePreDestroyCallback
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestInstancePreDestroyCallbacks(TestMethodTestDescriptor.java:259)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:145)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.TestInstancePreDestroyCallback
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 43 more
后来,又反复尝试了多次解决启动应用和OSS上传文件不能同时成功的问题,并且将spring-cloud-starter-alicloud-oss依赖指定版本号2.2.0.RELEASE,但暂未找到完美解决方案。无奈之下,只能先使用开头提到的临时方案了!
期望后面能够发现更优化的方案。