首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单个项目中的多个Spring库导致NoClassDefFoundError

单个项目中的多个Spring库导致NoClassDefFoundError
EN

Stack Overflow用户
提问于 2019-09-30 16:59:23
回答 1查看 642关注 0票数 0

如果我使用单个Spring库,例如implementation("org.springframework.cloud:spring-cloud-gcp-starter-sql-postgresql:1.1.1.RELEASE")和属性:spring.cloud.gcp.credentials.encoded-key=作为凭证.一切都很好。

但是,如果我也想通过:implementation("org.springframework.cloud:spring-cloud-gcp-starter-pubsub:1.1.3.RELEASE")向我的项目添加pub/sub,那么我将得到下面的例外情况。

我试过:

implementation("org.springframework.cloud:spring-cloud-gcp-starter-logging:1.1.3.RELEASE")

  • Using

  • 使用不同的Java 11和12

  • 不同的Spring库作为“第二个”库implementation("org.springframework.cloud:spring-cloud-gcp-starter:1.1.3.RELEASE")spring.cloud.gcp.credentials.location而不是spring.cloud.gcp.credentials.encoded-key

  • Use管理服务帐户,以确保它不是IAM角色问题。

implementation("org.springframework.cloud:spring-cloud-gcp-starter:1.1.3.RELEASE")implementation("org.springframework.cloud:spring-cloud-gcp-starter-logging:1.1.3.RELEASE")

一旦我包含了多个这些Spring库,同样的SocketFactory错误就会出现。从医生那里看,应该能正常工作。第二个库应该只使用相同的凭据。

如果我返回到一个库,那么它可以正常工作。

代码语言:javascript
复制
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Factory method 'cloudSqlDataSourceProperties' threw exception; nested exception is java.lang.NoClassDefFoundError: com/google/cloud/sql/core/CoreSocketFactory
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 171 common frames omitted
Caused by: java.lang.NoClassDefFoundError: com/google/cloud/sql/core/CoreSocketFactory
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration.cloudSqlDataSourceProperties(GcpCloudSqlAutoConfiguration.java:209) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration$$EnhancerBySpringCGLIB$$4f5495da.CGLIB$cloudSqlDataSourceProperties$0(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration$$EnhancerBySpringCGLIB$$4f5495da$$FastClassBySpringCGLIB$$58c2377.invoke(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration$$EnhancerBySpringCGLIB$$4f5495da.cloudSqlDataSourceProperties(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 172 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.google.cloud.sql.core.CoreSocketFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[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]
    ... 183 common frames omitted

我还在这里创建了一个Github问题:https://github.com/spring-cloud/spring-cloud-gcp/issues/1948

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-30 19:02:26

由于从Maven Central复制粘贴导入而没有检查版本,所以使用了不同版本的库。

使用的版本包括1.1.1.RELEASE1.1.3.RELEASE。一次只应该使用其中的一个发行号。

重构代码以删除此错误的可能性:

代码语言:javascript
复制
implementation("org.springframework.cloud:spring-cloud-gcp-starter-sql-postgresql:$springCloudGCPVersion")
implementation("org.springframework.cloud:spring-cloud-gcp-starter-pubsub:$springCloudGCPVersion")
implementation("org.springframework.cloud:spring-cloud-gcp-dependencies:$springCloudGCPVersion")
implementation("org.springframework.cloud:spring-cloud-gcp-starter:$springCloudGCPVersion")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58172262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档