|
@ -1,20 +1,23 @@
|
|
|
package com.yihu.hos.common.configuration;
|
|
|
|
|
|
import com.mongodb.Mongo;
|
|
|
import com.mongodb.MongoClient;
|
|
|
import com.mongodb.MongoCredential;
|
|
|
import com.mongodb.ServerAddress;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
|
|
|
|
|
import static java.util.Collections.singletonList;
|
|
|
|
|
|
/**
|
|
|
* @created Airhead 2016/7/27.
|
|
|
*/
|
|
|
@Configuration("MongoConfiguration")
|
|
|
public class MongoConfiguration {
|
|
|
public static final String BEAN_ID = "MongoConfiguration";
|
|
|
@Configuration
|
|
|
public class MongoConfiguration extends AbstractMongoConfiguration {
|
|
|
// @Value("${spring.data.mongodb.uri}")
|
|
|
// private String uri;
|
|
|
|
|
|
@Value("${spring.data.mongodb.host}")
|
|
|
private String host;
|
|
@ -29,9 +32,20 @@ public class MongoConfiguration {
|
|
|
@Value("${spring.data.mongodb.database}")
|
|
|
private String database;
|
|
|
|
|
|
private static MongoClient mongo;
|
|
|
@Override
|
|
|
public String getDatabaseName() {
|
|
|
return database;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Bean
|
|
|
public Mongo mongo() throws Exception {
|
|
|
return new MongoClient(singletonList(new ServerAddress(host, port)),
|
|
|
singletonList(MongoCredential.createCredential(username, authenticationDatabase, password.toCharArray())));
|
|
|
}
|
|
|
|
|
|
private static MongoClient mongo;
|
|
|
|
|
|
public MongoClient mongoClient() throws Exception {
|
|
|
if(mongo == null) {
|
|
|
return new MongoClient(singletonList(new ServerAddress(host, port)),
|