|
@@ -2,6 +2,8 @@ package com.mooctest.config;
|
|
|
|
|
|
import com.mongodb.MongoClient;
|
|
|
import com.mongodb.MongoClientOptions;
|
|
|
+import com.mongodb.MongoCredential;
|
|
|
+import com.mongodb.ServerAddress;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -16,6 +18,9 @@ import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
|
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
@Configuration
|
|
|
@EnableMongoRepositories(
|
|
|
basePackages = "com.mooctest.dao",
|
|
@@ -27,13 +32,27 @@ public class MongoOneConfig extends AbstractMongoConfig {
|
|
|
private String database;
|
|
|
@Value("${mongodb1.port}")
|
|
|
private int port;
|
|
|
+ @Value("${mongodb1.username}")
|
|
|
+ private String username;
|
|
|
+ @Value("${mongodb1.password}")
|
|
|
+ private String password;
|
|
|
//Setter methods go here..
|
|
|
/*
|
|
|
* 创建MongoDBFactory的方法
|
|
|
* 两个MongoDB连接共用
|
|
|
*/
|
|
|
+
|
|
|
+
|
|
|
public MongoDbFactory mongoDbFactory() throws Exception {
|
|
|
- return new SimpleMongoDbFactory(new MongoClient(host, port), database);
|
|
|
+ List<ServerAddress> seeds = new ArrayList<>();
|
|
|
+ ServerAddress serverAddress = new ServerAddress(host, port);
|
|
|
+ seeds.add(serverAddress);
|
|
|
+ List<MongoCredential> mongoCredentialList = new ArrayList<>();
|
|
|
+ mongoCredentialList.add(MongoCredential.createCredential(username, database, password.toCharArray()));
|
|
|
+ return new SimpleMongoDbFactory(new MongoClient(seeds, mongoCredentialList), database);
|
|
|
+// return new SimpleMongoDbFactory(new MongoClient(host, port), database);
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|