package com.capitalone.dashboard.config; import java.util.Collections; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import com.capitalone.dashboard.repository.RepositoryPackage; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; @Component @EnableMongoRepositories(basePackageClasses = RepositoryPackage.class) public class MongoConfig extends AbstractMongoConfiguration { @Value("${dbname:dashboard}") private String databaseName; @Value("${dbhost:localhost}") private String host; @Value("${dbport:27017}") private int port; @Value("${dbusername:}") private String userName; @Value("${dbpassword:}") private char[] password; @Override protected String getDatabaseName() { return databaseName; } @Override @Bean public MongoClient mongo() throws Exception { if (StringUtils.isEmpty(userName)) { return new MongoClient(new ServerAddress(host, port)); } MongoCredential mongoCredential = MongoCredential.createMongoCRCredential(userName, databaseName, password); return new MongoClient(new ServerAddress(host, port), Collections.singletonList(mongoCredential)); } @Override protected String getMappingBasePackage() { return "com.capitalone.dashboard.model"; } @Bean public MongoTemplate mongoTemplate() throws Exception { return new MongoTemplate(mongo(), getDatabaseName()); } @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }