1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| @Configuration @EnableMongoRepositories(mongoTemplateRef = "firstMongo") public class FirstMongoTemplate {
private MongoProperties mongoProperties;
@Bean(name = "firstMongo") public MongoTemplate getMongoTemplate() { return new MongoTemplate(getMongoDbFactory(this.mongoProperties)); }
@Bean("firstMongoDbFactory") public MongoDbFactory getMongoDbFactory(@Qualifier("firstMongoProperties") MongoProperties mongoProperties) { ServerAddress serverAdress = new ServerAddress(mongoProperties.getHost(),mongoProperties.getPort()); if (!ObjectUtils.isEmpty(mongoProperties.getUsername()) && !ObjectUtils.isEmpty(mongoProperties.getPassword())) { MongoCredential credential = MongoCredential.createCredential( mongoProperties.getUsername(), mongoProperties.getDatabase(), mongoProperties.getPassword()); return new SimpleMongoDbFactory(new MongoClient(serverAdress, credential, MongoClientOptions.builder().build()), mongoProperties.getDatabase()); } else { return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase()); } }
}
@Configuration @EnableMongoRepositories(mongoTemplateRef = "secondMongo") public class SecondMongoTemplate {
private MongoProperties mongoProperties;
@Bean(name = "secondMongo") public MongoTemplate getMongoTemplate() { return new MongoTemplate(getMongoDbFactory(this.mongoProperties)); }
@Bean("secondMongoDbFactory") public MongoDbFactory getMongoDbFactory(@Qualifier("secondMongoProperties") MongoProperties mongoProperties) { ServerAddress serverAdress = new ServerAddress(mongoProperties.getHost(),mongoProperties.getPort()); if (!ObjectUtils.isEmpty(mongoProperties.getUsername()) && !ObjectUtils.isEmpty(mongoProperties.getPassword())) { MongoCredential credential = MongoCredential.createCredential( mongoProperties.getUsername(), mongoProperties.getDatabase(), mongoProperties.getPassword()); return new SimpleMongoDbFactory(new MongoClient(serverAdress, credential, MongoClientOptions.builder().build()), mongoProperties.getDatabase()); } else { return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase()); } } }
|