Spring 使用 MongoTemplate:java.lang.String com.mongodb.connection.ClusterSettings.getDescription()
Spring with MongoTemplate: java.lang.String com.mongodb.connection.ClusterSettings.getDescription()
我正在尝试创建一个使用 MongoTemplate 处理 MongoDB 数据库的简单应用程序。然而,这个方法:
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate template = new MongoTemplate(mongoClient(), this.mongodbName);
return template;
}
失败并出现此异常:
Error creating bean with name 'mongoTemplate' defined in class path resource [com/myapp/tryout/repository/config/MongoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: 'java.lang.String com.mongodb.connection.ClusterSettings.getDescription()'
pom.xml 具有以下依赖项:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.5</version>
</dependency>
Spring版本是5.2.6。
此 com.mongodb.connection.ClusterSettings 的文档表明所讨论的方法确实已弃用。此外,通过查看 ClusterSettings class 我找不到此方法。
我的问题是:是否与 Spring 和 mongo-java-驱动程序的当前版本不匹配有关?如果是,能否请您指出要使用的正确软件包组合?
如果您需要更多信息,请询问。我很乐意提供。
添加:啊,讽刺...
我找到了这个
private static Cluster createCluster(final MongoClientSettings settings,
@Nullable final MongoDriverInformation mongoDriverInformation) {
notNull("settings", settings);
List<MongoCredential> credentialList = settings.getCredential() != null ? singletonList(settings.getCredential())
: Collections.<MongoCredential>emptyList();
return new DefaultClusterFactory().createCluster(settings.getClusterSettings(), settings.getServerSettings(),
settings.getConnectionPoolSettings(), getStreamFactory(settings, false), getStreamFactory(settings, true), credentialList,
getCommandListener(settings.getCommandListeners()), settings.getApplicationName(), mongoDriverInformation,
settings.getCompressorList());
}
在 com.mongodb.client.internal.MongoClientImpl.
还有这个
public Cluster createCluster(final ClusterSettings clusterSettings, final ServerSettings serverSettings,
final ConnectionPoolSettings connectionPoolSettings, final StreamFactory streamFactory,
final StreamFactory heartbeatStreamFactory, final List<MongoCredential> credentialList,
final CommandListener commandListener, final String applicationName,
final MongoDriverInformation mongoDriverInformation,
final List<MongoCompressor> compressorList) {
ClusterId clusterId = new ClusterId(clusterSettings.getDescription());
在 com.mongodb.connection.DefaultClusterFactory 中,已弃用。
看来我对版本不匹配的看法是正确的。
将上述软件包的版本更改为这些版本:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.11.2</version>
</dependency>
将解决该问题。我无法将此 post 标记为问题的答案。似乎 spring-data-mongodb 的 3.0.* 版与最新的 mongo-java-驱动程序不兼容,反之亦然。我可能是错的。
Spring 3.x 支持 mongodb java 版本 4.x。 mongo-java-driver 和 mongodb-driver “uber-jars”不再发布,如链接页面中所述。在 3.x 和 4.x mongo 驱动程序 java 版本之间,uber jar 依赖项已拆分为核心和 sync/reactive 流依赖项。我能够使用 3.x 版本重现该问题。一旦我更新以纠正 4.x 依赖项问题就消失了。
所以正确的依赖关系是在下面的顺序
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.0.4</version>
</dependency>
https://mongodb.github.io/mongo-java-driver/4.0/upgrading/#upgrading-from-the-3-12-java-driver
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#upgrading.2-3
我正在尝试创建一个使用 MongoTemplate 处理 MongoDB 数据库的简单应用程序。然而,这个方法:
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate template = new MongoTemplate(mongoClient(), this.mongodbName);
return template;
}
失败并出现此异常:
Error creating bean with name 'mongoTemplate' defined in class path resource [com/myapp/tryout/repository/config/MongoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: 'java.lang.String com.mongodb.connection.ClusterSettings.getDescription()'
pom.xml 具有以下依赖项:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.5</version>
</dependency>
Spring版本是5.2.6。 此 com.mongodb.connection.ClusterSettings 的文档表明所讨论的方法确实已弃用。此外,通过查看 ClusterSettings class 我找不到此方法。
我的问题是:是否与 Spring 和 mongo-java-驱动程序的当前版本不匹配有关?如果是,能否请您指出要使用的正确软件包组合?
如果您需要更多信息,请询问。我很乐意提供。
添加:啊,讽刺... 我找到了这个
private static Cluster createCluster(final MongoClientSettings settings,
@Nullable final MongoDriverInformation mongoDriverInformation) {
notNull("settings", settings);
List<MongoCredential> credentialList = settings.getCredential() != null ? singletonList(settings.getCredential())
: Collections.<MongoCredential>emptyList();
return new DefaultClusterFactory().createCluster(settings.getClusterSettings(), settings.getServerSettings(),
settings.getConnectionPoolSettings(), getStreamFactory(settings, false), getStreamFactory(settings, true), credentialList,
getCommandListener(settings.getCommandListeners()), settings.getApplicationName(), mongoDriverInformation,
settings.getCompressorList());
}
在 com.mongodb.client.internal.MongoClientImpl.
还有这个
public Cluster createCluster(final ClusterSettings clusterSettings, final ServerSettings serverSettings,
final ConnectionPoolSettings connectionPoolSettings, final StreamFactory streamFactory,
final StreamFactory heartbeatStreamFactory, final List<MongoCredential> credentialList,
final CommandListener commandListener, final String applicationName,
final MongoDriverInformation mongoDriverInformation,
final List<MongoCompressor> compressorList) {
ClusterId clusterId = new ClusterId(clusterSettings.getDescription());
在 com.mongodb.connection.DefaultClusterFactory 中,已弃用。
看来我对版本不匹配的看法是正确的。 将上述软件包的版本更改为这些版本:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.11.2</version>
</dependency>
将解决该问题。我无法将此 post 标记为问题的答案。似乎 spring-data-mongodb 的 3.0.* 版与最新的 mongo-java-驱动程序不兼容,反之亦然。我可能是错的。
Spring 3.x 支持 mongodb java 版本 4.x。 mongo-java-driver 和 mongodb-driver “uber-jars”不再发布,如链接页面中所述。在 3.x 和 4.x mongo 驱动程序 java 版本之间,uber jar 依赖项已拆分为核心和 sync/reactive 流依赖项。我能够使用 3.x 版本重现该问题。一旦我更新以纠正 4.x 依赖项问题就消失了。
所以正确的依赖关系是在下面的顺序
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.0.4</version>
</dependency>
https://mongodb.github.io/mongo-java-driver/4.0/upgrading/#upgrading-from-the-3-12-java-driver https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#upgrading.2-3