2020-02-07

Spring MongoDB auto-configuration

Spring Boot provides auto-configuration for MongoDB. You can make use of the other projects, but you must configure them yourself. Refer to the appropriate reference documentation at
spring.io/projects/spring-data.

MongoDB is an open-source NoSQL document database that uses a JSON-like schema instead of
traditional table-based relational data. Spring Boot offers several conveniences for working with
MongoDB, including the spring-boot-starter-data-mongodb and spring-boot-starter-data-mongodbreactive
“Starters”.

Connecting to a MongoDB Database

To access Mongo databases, you can inject an auto-configured
org.springframework.data.mongodb.MongoDbFactory. By default, the instance tries to connect to a
MongoDB server at mongodb://localhost/test. The following example shows how to connect to a
MongoDB database:

import org.springframework.data.mongodb.MongoDbFactory;
import com.mongodb.DB;
@Component
public class MyBean {
private final MongoDbFactory mongo;
@Autowired
public MyBean(MongoDbFactory mongo) {
this.mongo = mongo;
}
// ...
public void example() {
DB db = mongo.getDb();
// ...
}
}

You can set the configprop:spring.data.mongodb.uri[] property to change the URL and configure
additional settings such as the replica set, as shown in the following example:

spring.data.mongodb.uri=mongodb://user:secret@mongo1.example.com:12345,mongo2.example.
com:23456/test

Alternatively, as long as you use Mongo 2.x, you can specify a host/port. For example, you might
declare the following settings in your application.properties:
spring.data.mongodb.host=mongoserver
spring.data.mongodb.port=27017
If you have defined your own MongoClient, it will be used to auto-configure a suitable
MongoDbFactory. Both com.mongodb.MongoClient and com.mongodb.client.MongoClient are supported.

If you use the Mongo 3.0 Java driver, spring.data.mongodb.host and
spring.data.mongodb.port are not supported. In such cases,
spring.data.mongodb.uri should be used to provide all of the configuration.

If spring.data.mongodb.port is not specified, the default of 27017 is used. You could
delete this line from the example shown earlier.

If you do not use Spring Data Mongo, you can inject com.mongodb.MongoClient beans
instead of using MongoDbFactory. If you want to take complete control of
establishing the MongoDB connection, you can also declare your own
MongoDbFactory or MongoClient bean.

If you are using the reactive driver, Netty is required for SSL. The autoconfiguration
configures this factory automatically if Netty is available and the
factory to use hasn’t been customized already.


MongoTemplate

Spring Data MongoDB provides a MongoTemplate class that is very similar in its design to Spring’s
JdbcTemplate. As with JdbcTemplate, Spring Boot auto-configures a bean for you to inject the
template,
 as follows:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final MongoTemplate mongoTemplate;
@Autowired
public MyBean(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
// ...
}

Spring Data MongoDB Repositories

Spring Data includes repository support for MongoDB. As with the JPA repositories discussed
earlier, the basic principle is that queries are constructed automatically, based on method names.
In fact, both Spring Data JPA and Spring Data MongoDB share the same common infrastructure. You
could take the JPA example from earlier and, assuming that City is now a Mongo data class rather
than a JPA @Entity, it works in the same way, as shown in the following example:

package com.example.myapp.domain;
import org.springframework.data.domain.*;
import org.springframework.data.repository.*;
public interface CityRepository extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
City findByNameAndStateAllIgnoringCase(String name, String state);
}

You can customize document scanning locations by using the @EntityScan
annotation.

For complete details of Spring Data MongoDB, including its rich object mapping
technologies, refer to its reference documentation.

Embedded Mongo

Spring Boot offers auto-configuration for Embedded Mongo. To use it in your Spring Boot
application, add a dependency on de.flapdoodle.embed:de.flapdoodle.embed.mongo.
The port that Mongo listens on can be configured by setting the
configprop:spring.data.mongodb.port[] property. To use a randomly allocated free port, use a value
of 0. The MongoClient created by MongoAutoConfiguration is automatically configured to use the
randomly allocated port.

If you do not configure a custom port, the embedded support uses a random port
(rather than 27017) by default.

If you have SLF4J on the classpath, the output produced by Mongo is automatically routed to a
logger named org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo.

You can declare your own IMongodConfig and IRuntimeConfig beans to take control of the Mongo
instance’s configuration and logging routing. The download configuration can be customized by
declaring a DownloadConfigBuilderCustomizer bean.

No comments:

Post a Comment