2016-07-28 42 views
0

我正在將mongo java驅動程序jar升級到2.14.0。我的舊代碼工作正常,但下面的代碼顯示不贊成使用的類和構造函數,所以我需要兼容的代碼沒有棄用的類和構造函數爲mongo-java-driver.jar 2.14.0。mongo java驅動程序2.14.0。更改不推薦使用的代碼

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; 
} 
+0

你的問題是什麼呢? –

+0

在那裏我提到過這些行要用spring-data-mongodb-1.9.2.jar的代碼替換。 –

回答

1

這不是Spring Data MongoDB代碼。這是不久前已被棄用的MongoDB Java驅動程序的代碼。用戶誘因使用MongoClient超過MongoMongoClientOptionsMongoOptions

+0

謝謝..這是我的錯誤。你是對的這段代碼是MongoDB Java驅動程序,它已在新版本的mongo java驅動程序中被棄用。 –

1

等同的代碼,同樣是

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; 
} 
相關問題