public void setHost(String host) {
this.host = host;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public MongoDbFactory mongoDbFactory() throws Exception {
// 无认证的初始化方法
return new SimpleMongoDbFactory(new MongoClient(host, port), database);
// 有认证的初始化方法
// ServerAddress serverAddress = new ServerAddress(host, port);
// List<MongoCredential> mongoCredentialList = new ArrayList<>();
// mongoCredentialList.add(MongoCredential.createCredential(username, database,
// password.toCharArray()));
// return new SimpleMongoDbFactory(new MongoClient(serverAddress,
// mongoCredentialList), database);
}
/*
* Factory method to create the MongoTemplate
*/
abstract public MongoTemplate getMongoTemplate() throws Exception;
}
或者
public abstract class AbstractMongoConfig {
private String uri;
/*
* Method that creates MongoDbFactory Common to both of the MongoDb
* connections
*/
public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClientURI(uri);
}
/*
* Factory method to create the MongoTemplate
*/
abstract public MongoTemplate getMongoTemplate() throws Exception;