|
@@ -1,12 +1,7 @@
|
|
package com.mooctest.config;
|
|
package com.mooctest.config;
|
|
|
|
|
|
-import com.google.common.collect.ImmutableList;
|
|
|
|
import com.mongodb.*;
|
|
import com.mongodb.*;
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
-import org.springframework.boot.autoconfigure.mongo.MongoProperties;
|
|
|
|
-import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.context.annotation.Primary;
|
|
@@ -14,51 +9,32 @@ import org.springframework.data.mongodb.MongoDbFactory;
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
|
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
|
-import org.springframework.core.env.Environment;
|
|
|
|
-
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
-@EnableMongoRepositories(
|
|
|
|
- basePackages = "com.mooctest.dao",
|
|
|
|
- mongoTemplateRef = "mongoTemplate1")
|
|
|
|
|
|
+@EnableMongoRepositories(basePackages = "com.mooctest.dao",mongoTemplateRef = "mongoTemplate1")
|
|
public class MongoOneConfig extends AbstractMongoConfig {
|
|
public class MongoOneConfig extends AbstractMongoConfig {
|
|
- @Value("${mongodb1.host}")
|
|
|
|
|
|
+ @Value("${mongodb.host}")
|
|
private String host;
|
|
private String host;
|
|
- @Value("${mongodb1.database}")
|
|
|
|
|
|
+ @Value("${mongodb.database}")
|
|
private String database;
|
|
private String database;
|
|
- @Value("${mongodb1.port}")
|
|
|
|
|
|
+ @Value("${mongodb.port}")
|
|
private int port;
|
|
private int port;
|
|
- @Value("${mongodb1.username}")
|
|
|
|
|
|
+ @Value("${mongodb.username}")
|
|
private String username;
|
|
private String username;
|
|
- @Value("${mongodb1.password}")
|
|
|
|
|
|
+ @Value("${mongodb.password}")
|
|
private String password;
|
|
private String password;
|
|
- //Setter methods go here..
|
|
|
|
- /*
|
|
|
|
- * 创建MongoDBFactory的方法
|
|
|
|
- * 两个MongoDB连接共用
|
|
|
|
- */
|
|
|
|
|
|
|
|
- public MongoDbFactory mongoDbFactory() throws Exception {
|
|
|
|
|
|
+ public MongoDbFactory mongoDbFactory() {
|
|
String uriStr="mongodb://"+username+":"+password+"@"+host+":"+port+"/"+database;
|
|
String uriStr="mongodb://"+username+":"+password+"@"+host+":"+port+"/"+database;
|
|
- System.out.println(uriStr);
|
|
|
|
MongoClientURI mongoClientURI=new MongoClientURI(uriStr);
|
|
MongoClientURI mongoClientURI=new MongoClientURI(uriStr);
|
|
MongoDbFactory mongoDbFactory=new SimpleMongoDbFactory(mongoClientURI);
|
|
MongoDbFactory mongoDbFactory=new SimpleMongoDbFactory(mongoClientURI);
|
|
return mongoDbFactory;
|
|
return mongoDbFactory;
|
|
-// System.out.println(host + port + username);
|
|
|
|
-// 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()));
|
|
|
|
-// MongoClient mongoClient = new MongoClient(new ServerAddress(), ImmutableList.of(MongoCredential.createCredential(username, database, password.toCharArray())));
|
|
|
|
-// return new SimpleMongoDbFactory(mongoClient, database);
|
|
|
|
-// return new SimpleMongoDbFactory(new MongoClient(host, port), database);
|
|
|
|
|
|
+ // 无密码使用
|
|
|
|
+ // return new SimpleMongoDbFactory(new MongoClient(host, port), database);
|
|
}
|
|
}
|
|
|
|
|
|
@Primary
|
|
@Primary
|
|
- public @Bean(name = "mongoTemplate1") MongoTemplate getMongoTemplate() throws Exception {
|
|
|
|
|
|
+ public @Bean(name = "mongoTemplate1") MongoTemplate getMongoTemplate() {
|
|
return new MongoTemplate(mongoDbFactory());
|
|
return new MongoTemplate(mongoDbFactory());
|
|
}
|
|
}
|
|
}
|
|
}
|