Reputation: 498
I use spring boot to operate mongodb, and in my application.properties is spring.data.mongodb.uri=mongodb://username:password@hostIp:27017/database1
and I add jar is
spring-boot-starter-data-mongodb
, but now there are two database in my mongodb, and how can I add another database in my spring boot, and how can I distinguish them when I use different database?
Upvotes: 0
Views: 2623
Reputation: 153
Yes, You can configure 2 databases in single spring-boot application. You have to add two database properties in single application.yml file.
#application.yml
spring:
data:
mongodb:
database: database_one
uri: mongodb://root:[email protected]:27017/database_one
# username : root, Password : root
secondary:
mongodb:
database: database_two
uri: mongodb://root:[email protected]:27017/database_two
# username : root, Password : password
Then you have to add two database configuration files for spring and read these two properties in each file. For first database configuration file just add @Primary annotation for primary database properties. You can combine these two files in single as per your requirement.
@Configuration
public class DatabaseConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseConfiguration.class);
@Value("${spring.data.mongodb.uri}")
private String mongoUri;
@Value("${spring.data.mongodb.database}")
private String mongoDbName;
@Primary
@Bean
public MongoTemplate mongoTemplate() {
LOGGER.debug(" Instantiating MongoDbFactory ");
SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient(), mongoDbName);
return new MongoTemplate(mongoDbFactory);
}
@Primary
@Bean
public MongoClient mongoClient() {
return new MongoClient(mongoClientURI());
}
@Primary
@Bean
public MongoClientURI mongoClientURI() {
LOGGER.debug(" creating connection with mongodb with uri [{}] ", mongoUri);
return new MongoClientURI(mongoUri);
}
}
# Second database configuration file.
@Configuration
public class SecondaryDatabaseConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(SecondaryDatabaseConfiguration.class);
@Value("${secondary.mongodb.uri}")
private String mongoUri;
@Value("${secondary.mongodb.database}")
private String mongoDbName ;
@Bean(name = "mongoTemplateSecond")
public MongoTemplate mongoTemplateSecondary() {
LOGGER.debug(" Instantiating MongoDbFactory ");
SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClientSecondary(), mongoDbName);
return new MongoTemplate(mongoDbFactory);
}
@Bean
public MongoClient mongoClientSecondary() {
return new MongoClient(mongoClientURISecondary());
}
@Bean
public MongoClientURI mongoClientURISecondary() {
LOGGER.debug(" creating connection with mongodb with uri [{}] ", mongoUri);
return new MongoClientURI(mongoUri);
}
}
Then use @Qualifier annotation in your implementation class and pass bean name if you want to fetch from secondary database and for primary database you can directly Autowire.
@Qualifier annotation is used to differentiate beans of the same type.
@Component
public class RepositoryImpl{
@Qualifier(value = "mongoTemplateSecond")
@Autowired
MongoTemplate mongoTemplateSecond; // for secondary database
@Autowired
private MongoTemplate mongoTemplate; // for primary database
public List<BasicDBObject> findRecordsInSecondary(Query query){
List<BasicDBObject> basicDBObjects = mongoTemplateSecond.find(query, BasicDBObject.class, YOUR_COLLECTION_NAME);
return basicDBObjects;
}
public List<BasicDBObject> findRecordsInPrimary(Query query){
List<BasicDBObject> basicDBObjects = mongoTemplate.find(query, BasicDBObject.class, YOUR_COLLECTION_NAME);
return basicDBObjects;
}
}
Upvotes: 3