use*_*536 1 java spring mongodb spring-data-mongodb spring-mongodb
我正在尝试创建一个简单的应用程序,使用 MongoTemplate 处理 MongoDB 数据库。然而,这个方法:
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate template = new MongoTemplate(mongoClient(), this.mongodbName);
return template;
}
Run Code Online (Sandbox Code Playgroud)
因以下异常而失败:
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()'
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Spring版本是5.2.6。com.mongodb.connection.ClusterSettings 的文档表明该方法确实已被弃用。此外,通过查看 ClusterSettings 类,我找不到此方法。
我的问题是:是否与当前版本的 Spring 和 mongo-java-driver 不匹配有关?如果是,您能否指出要使用的正确包组合?
如果您需要更多信息,请询问。我很乐意提供它。
补充:啊,讽刺……我发现了这个
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());
}
Run Code Online (Sandbox Code Playgroud)
在 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());
Run Code Online (Sandbox Code Playgroud)
在 com.mongodb.connection.DefaultClusterFactory 中,已弃用。
Spring 3.x 支持 mongodb java 版本 4.x。mongo-java-driver 和 mongodb-driver \xe2\x80\x9cuber-jars\xe2\x80\x9d 不再发布,如链接页面中所述。在 3.x 和 4.x mongo 驱动程序 java 版本之间,uber jar 依赖项已分为核心依赖项和同步/反应流依赖项。我能够使用 3.x 版本重现该问题。一旦我更新以纠正 4.x 依赖项问题就消失了。
\n所以正确的依赖关系按以下顺序排列
\n<dependency>\n <groupId>org.springframework.data</groupId>\n <artifactId>spring-data-mongodb</artifactId>\n <version>3.0.1.RELEASE</version>\n</dependency>\n\n<dependency>\n <groupId>org.mongodb</groupId>\n <artifactId>mongodb-driver-core</artifactId>\n <version>4.0.4</version>\n</dependency>\n\n<dependency>\n <groupId>org.mongodb</groupId>\n <artifactId>mongodb-driver-sync</artifactId>\n <version>4.0.4</version>\n</dependency>\nRun Code Online (Sandbox Code Playgroud)\nhttps://mongodb.github.io/mongo-java-driver/4.0/upgrading/#upgrading-from-the-3-12-java-driver \n https://docs.spring.io/spring-data/ mongodb/docs/current/reference/html/#upgrading.2-3
\n| 归档时间: |
|
| 查看次数: |
4913 次 |
| 最近记录: |