MongoJava驱动程序2.14.0。更改不推荐使用的代码
我正在将MongoJava驱动程序jar升级到2.14.0。我的旧代码工作正常,但下面的代码显示了不推荐使用的类和构造函数,因此我需要不推荐使用的类和构造函数的兼容代码,用于mongo-java-driver.jar 2.14.0MongoJava驱动程序2.14.0。更改不推荐使用的代码,java,spring,mongo-java-driver,Java,Spring,Mongo Java Driver,我正在将MongoJava驱动程序jar升级到2.14.0。我的旧代码工作正常,但下面的代码显示了不推荐使用的类和构造函数,因此我需要不推荐使用的类和构造函数的兼容代码,用于mongo-java-driver.jar 2.14.0 public MongoTemplate getMongoTemplate() { SimpleMongoDbFactory simpleMongoDbFactory = null; try { MongoOptions opts
public MongoTemplate getMongoTemplate() {
SimpleMongoDbFactory simpleMongoDbFactory = null;
try {
MongoOptions opts = new MongoOptions();//depricate
opts.threadsAllowedToBlockForConnectionMultiplier = getThreadsAllowedToBlockForConnectionMultiplier();//depricate
opts.connectionsPerHost = getConnectionsPerHost();//depricate
ServerAddress addr = new ServerAddress(getHost(), getPort());
Mongo mongo = new Mongo(addr, opts);//depricate
simpleMongoDbFactory = new SimpleMongoDbFactory(mongo,
getDatabaseName());//depricate
if (mongoTemplate == null) {
mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
}
} catch (UnknownHostException e) {
LOGGER.error(e.getMessage());
} catch (MongoException e) {
LOGGER.error(e.getMessage());
}
return mongoTemplate;
}
这不是Spring数据MongoDB代码。这是来自MongoDB Java驱动程序的代码,该驱动程序早就被弃用了。鼓励用户使用
MongoClient
overMongo
,mongoclientations
overmongoptions
等。这不是Spring数据MongoDB代码。这是来自MongoDB Java驱动程序的代码,该驱动程序早就被弃用了。鼓励用户使用MongoClient
overMongo
,mongoclientations
overmongoptions
等。
public MongoTemplate getMongoTemplate() {
SimpleMongoDbFactory simpleMongoDbFactory = null;
try {
Builder builder =MongoClientOptions.builder();
builder.threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockForConnectionMultiplier());
builder.connectionsPerHost(getConnectionsPerHost());
MongoClientOptions options = builder.build();
ServerAddress addr = new ServerAddress(getHost(), getPort());
MongoClient mongo = new MongoClient(addr, options);
simpleMongoDbFactory = new SimpleMongoDbFactory(mongo,getDatabaseName());
if (mongoTemplate == null) {
mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
}
} catch (UnknownHostException e) {
LOGGER.error(e.getMessage());
} catch (MongoException e) {
LOGGER.error(e.getMessage());
}
return mongoTemplate;
}
相同的等效代码为
public MongoTemplate getMongoTemplate() {
SimpleMongoDbFactory simpleMongoDbFactory = null;
try {
Builder builder =MongoClientOptions.builder();
builder.threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockForConnectionMultiplier());
builder.connectionsPerHost(getConnectionsPerHost());
MongoClientOptions options = builder.build();
ServerAddress addr = new ServerAddress(getHost(), getPort());
MongoClient mongo = new MongoClient(addr, options);
simpleMongoDbFactory = new SimpleMongoDbFactory(mongo,getDatabaseName());
if (mongoTemplate == null) {
mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
}
} catch (UnknownHostException e) {
LOGGER.error(e.getMessage());
} catch (MongoException e) {
LOGGER.error(e.getMessage());
}
return mongoTemplate;
}
你的问题到底是什么?我在哪里提到了不推荐的那些行,希望用spring-data-mongodb-1.9.2.jar的代码替换。你的问题到底是什么?我在哪里提到了不推荐的那些行,希望用spring-data-mongodb-1.9.2.jar的代码替换。谢谢。。那是我的错误。您是对的,这段代码是MongoDB Java驱动程序的代码,它在新版本的mongo Java驱动程序中已被弃用。谢谢。。那是我的错误。您是对的,这段代码是MongoDB Java驱动程序的代码,它在新版本的mongo Java驱动程序中已被弃用。