123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package com.yihu.hos.rest.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
- public class MongoConfig extends AbstractMongoConfiguration {
- @Value("${spring.data.mongodb.host}")
- private String host;
- @Value("${spring.data.mongodb.port}")
- private int port;
- @Value("${spring.data.mongodb.username}")
- private String username;
- @Value("${spring.data.mongodb.password}")
- private String password;
- @Value("${spring.data.mongodb.authenticationDatabase}")
- private String authenticationDatabase;
- @Value("${spring.data.mongodb.database}")
- private String database;
- @Override
- public String getDatabaseName() {
- return database;
- }
- @Bean
- public MongoClient mongoClient() throws Exception {
- return new MongoClient(singletonList(new ServerAddress(host, port)),
- singletonList(MongoCredential.createCredential(username, authenticationDatabase, password.toCharArray())));
- }
- @Bean
- public Mongo mongo() throws Exception {
- return new MongoClient(singletonList(new ServerAddress(host, port)),
- singletonList(MongoCredential.createCredential(username, authenticationDatabase, password.toCharArray())));
- }
- public String getHost() {
- return host;
- }
- public int getPort() {
- return port;
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- }
|