依赖
compile "org.springframework.boot:spring-boot-starter-data-mongodb"
maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> mongo uri使用 mongodb://<username>:<password>@<host>:<port>/<db_name> application.properties配置 #--- Primary MongoDB ---# spring.data.mongodb.uri=mongodb://admin:root@127.0.0.1:27017/primary #--- Secondary MongoDB ---# mongodb.uri=mongodb://admin:root@127.0.0.1:27017/secondary bean配置 package com.futve.common.config import org.springframework.boot.autoconfigure.mongo.MongoProperties import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Primary import org.springframework.data.mongodb.MongoDatabaseFactory import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory @Configuration open class MultipleMongoConfig { @Primary @Bean(name = ["primaryMongoProperties"]) @ConfigurationProperties(prefix = "spring.data.mongodb") open fun getPrimary() : MongoProperties { return MongoProperties() } @Bean(name = ["secondaryMongoProperties"]) @ConfigurationProperties(prefix = "mongodb") open fun getSecondary() : MongoProperties { return MongoProperties() } @Primary @Bean(name = ["primaryMongoTemplate"]) open fun primaryMongoTemplate() : MongoTemplate { return MongoTemplate(primaryMongoDatabaseFactory(getPrimary())) } @Bean(name = ["secondaryMongoTemplate"]) open fun secondaryMongoTemplate() : MongoTemplate { return MongoTemplate(secondaryMongoDatabaseFactory(getSecondary())) } @Primary @Bean open fun primaryMongoDatabaseFactory(mongo : MongoProperties) : MongoDatabaseFactory { return SimpleMongoClientDatabaseFactory( mongo.uri ) } @Bean open fun secondaryMongoDatabaseFactory(mongo : MongoProperties) : MongoDatabaseFactory { return SimpleMongoClientDatabaseFactory( mongo.uri ) } } package com.futve.common.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; @Configuration public class MultipleMongoConfig2 { @Primary @Bean("primaryMongoProperties") @ConfigurationProperties("spring.data.mongodb") MongoProperties getPrimary() { return new MongoProperties(); } @Bean("secondaryMongoProperties") @ConfigurationProperties("mongodb") MongoProperties getSecondary() { return new MongoProperties(); } @Primary @Bean("primaryMongoTemplate") public MongoTemplate primaryMongoTemplate() { return new MongoTemplate(primaryMongoDatabaseFactory(getPrimary())); } @Bean("secondaryMongoTemplate") public MongoTemplate secondaryMongoTemplate() { return new MongoTemplate(secondaryMongoDatabaseFactory(getSecondary())); } @Primary @Bean public MongoDatabaseFactory primaryMongoDatabaseFactory(MongoProperties mongo1) { return new SimpleMongoClientDatabaseFactory( mongo1.getUri() ); } @Bean public MongoDatabaseFactory secondaryMongoDatabaseFactory(MongoProperties mongo2) { return new SimpleMongoClientDatabaseFactory( mongo2.getUri() ); } }
启动类配置
@SpringBootApplication(exclude = [ MongoAutoConfiguration::class, MongoDataAutoConfiguration::class ]) dao使用 @Repository class AuthorDao { @Autowired @Qualifier("secondaryMongoTemplate") protected lateinit var secondaryMongoTemplate : MongoTemplate fun findBySurname(surname): List<Author> { val query = Query() query.addCriteria( Criteria.where("surname").`is`(surname) ) return secondaryMongoTemplate.find(query, Author::class.java) } }