![]() |
Spring Data provides additional projects that help you access a variety of NoSQL technologies, including: MongoDB, Neo4J, Elasticsearch, Solr, Redis, Gemfire, Cassandra, Couchbase and LDAP. Spring Boot provides auto-configuration for Redis, MongoDB, Neo4j, Elasticsearch, Solr Cassandra, Couchbase, and LDAP. You can make use of the other projects, but you must configure them yourself. Refer to the appropriate reference documentation at projects.spring.io/spring-data. Redis is a cache, message broker, and richly-featured key-value store. Spring Boot offers basic auto-configuration for the Lettuce and Jedis client libraries and the abstractions on top of them provided by Spring Data Redis. There is a
You can inject an auto-configured @Component public class MyBean { private StringRedisTemplate template; @Autowired public MyBean(StringRedisTemplate template) { this.template = template; } // ... }
If you add your own 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
To access Mongo databases, you can inject an auto-configured
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 spring.data.mongodb.uri=mongodb://user:[email protected]:12345,mongo2.example.com:23456/test Alternatively, as long as you use Mongo 2.x, you can specify a spring.data.mongodb.host=mongoserver spring.data.mongodb.port=27017
Spring Data MongoDB provides a
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; } // ... } See the
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 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 findByNameAndCountryAllIgnoringCase(String name, String country); }
Spring Boot offers auto-configuration for
Embedded Mongo. To use it in
your Spring Boot application, add a dependency on
The port that Mongo listens on can be configured by setting the
If you have SLF4J on the classpath, the output produced by Mongo is automatically routed
to a logger named You can declare your own Neo4j is an open-source NoSQL graph database that uses a rich data
model of nodes related by first class relationships, which is better suited for connected
big data than traditional rdbms approaches. Spring Boot offers several conveniences for
working with Neo4j, including the You can inject an auto-configured @Component public class MyBean { private final Neo4jTemplate neo4jTemplate; @Autowired public MyBean(Neo4jTemplate neo4jTemplate) { this.neo4jTemplate = neo4jTemplate; } // ... } You can take full control of the configuration by adding a
You can configure the user and credentials to use by setting the spring.data.neo4j.uri=http://my-server:7474 spring.data.neo4j.username=neo4j spring.data.neo4j.password=secret If you add spring.data.neo4j.uri=file://var/tmp/graph.db
By default, if you are running a web application, the session is bound to the thread for
the entire processing of the request (that is, it uses the "Open Session in View"
pattern). If you do not want this behavior, add the following line to your
spring.data.neo4j.open-in-view=false Spring Data includes repository support for Neo4j. In fact, both Spring Data JPA and Spring Data Neo4j share the same common infrastructure.
You could take the JPA example from earlier and, assuming that
To enable repository support (and optionally support for @EnableNeo4jRepositories(basePackages = "com.example.myapp.repository")
@EnableTransactionManagement The following example shows an interface definition for a Neo4j repository: package com.example.myapp.domain; import org.springframework.data.domain.*; import org.springframework.data.repository.*; public interface CityRepository extends GraphRepository<City> { Page<City> findAll(Pageable pageable); City findByNameAndCountry(String name, String country); }
Spring Data Gemfire provides
convenient Spring-friendly tools for accessing the
Pivotal Gemfire data management
platform. There is a Apache Solr is a search engine. Spring Boot offers basic
auto-configuration for the Solr 5 client library and the abstractions on top of it
provided by Spring Data Solr. There
is a You can inject an auto-configured @Component public class MyBean { private SolrClient solr; @Autowired public MyBean(SolrClient solr) { this.solr = solr; } // ... } If you add your own Spring Data includes repository support for Apache Solr. As with the JPA repositories discussed earlier, the basic principle is that queries are automatically constructed for \ you based on method names. In fact, both Spring Data JPA and Spring Data Solr share the same common infrastructure.
You could take the JPA example from earlier and, assuming that
Elasticsearch is an open source, distributed, real-time
search and analytics engine. Spring Boot offers basic auto-configuration for
Elasticsearch and the abstractions on top of it provided by
Spring Data Elasticsearch.
There is a If you have spring.elasticsearch.jest.uris=http://search.example.com:9200 spring.elasticsearch.jest.read-timeout=10000 spring.elasticsearch.jest.username=user spring.elasticsearch.jest.password=secret You can also register an arbitrary number of beans that implement
static class HttpSettingsCustomizer implements HttpClientConfigBuilderCustomizer { @Override public void customize(HttpClientConfig.Builder builder) { builder.maxTotalConnection(100).defaultMaxTotalConnectionPerRoute(5); } } To take full control over the registration, define a To connect to Elasticsearch, you must provide the address of one or more cluster nodes.
The address can be specified by setting the spring.data.elasticsearch.cluster-nodes=localhost:9300 @Component public class MyBean { private final ElasticsearchTemplate template; public MyBean(ElasticsearchTemplate template) { this.template = template; } // ... } If you add your own Spring Data includes repository support for Elasticsearch. As with the JPA repositories discussed earlier, the basic principle is that queries are constructed for you automatically based on method names. In fact, both Spring Data JPA and Spring Data Elasticsearch share the same common
infrastructure. You could take the JPA example from earlier and, assuming that
Cassandra is an open source, distributed database
management system designed to handle large amounts of data across many commodity servers.
Spring Boot offers auto-configuration for Cassandra and the abstractions on top of it
provided by Spring Data
Cassandra. There is a You can inject an auto-configured spring.data.cassandra.keyspace-name=mykeyspace spring.data.cassandra.contact-points=cassandrahost1,cassandrahost2 The following code listing shows how to inject a Cassandra bean: @Component public class MyBean { private CassandraTemplate template; @Autowired public MyBean(CassandraTemplate template) { this.template = template; } // ... } If you add your own Spring Data includes basic repository support for Cassandra. Currently, this is more
limited than the JPA repositories discussed earlier and needs to annotate finder methods
with
Couchbase is an open-source, distributed, multi-model NoSQL
document-oriented database that is optimized for interactive applications. Spring Boot
offers auto-configuration for Couchbase and the abstractions on top of it provided by
Spring Data Couchbase. There are
You can get a spring.couchbase.bootstrap-hosts=my-host-1,192.168.1.123 spring.couchbase.bucket.name=my-bucket spring.couchbase.bucket.password=secret
It is also possible to customize some of the spring.couchbase.env.timeouts.connect=3000 spring.couchbase.env.ssl.key-store=/location/of/keystore.jks spring.couchbase.env.ssl.key-store-password=secret Check the Spring Data includes repository support for Couchbase. For complete details of Spring Data Couchbase, refer to the reference documentation. You can inject an auto-configured The following examples shows how to inject a Couchbase bean: @Component public class MyBean { private final CouchbaseTemplate template; @Autowired public MyBean(CouchbaseTemplate template) { this.template = template; } // ... } There are a few beans that you can define in your own configuration to override those provided by the auto-configuration:
To avoid hard-coding those names in your own config, you can reuse @Configuration public class SomeConfiguration { @Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS) public CustomConversions myCustomConversions() { return new CustomConversions(...); } // ... }
LDAP (Lightweight Directory Access Protocol) is an open, vendor-neutral, industry standard application protocol for accessing and maintaining distributed directory information services over an IP network. Spring Boot offers auto-configuration for any compliant LDAP server as well as support for the embedded in-memory LDAP server from UnboundID. LDAP abstractions are provided by
Spring Data LDAP.
There is a To connect to an LDAP server, make sure you declare a dependency on the
spring.ldap.urls=ldap://myserver:1235 spring.ldap.username=admin spring.ldap.password=secret If you need to customize connection settings, you can use the Spring Data includes repository support for LDAP. For complete details of Spring Data LDAP, refer to the reference documentation. You can also inject an auto-configured @Component public class MyBean { private final LdapTemplate template; @Autowired public MyBean(LdapTemplate template) { this.template = template; } // ... } For testing purposes, Spring Boot supports auto-configuration of an in-memory LDAP server
from UnboundID. To configure the server,
add a dependency to spring.ldap.embedded.base-dn=dc=spring,dc=io
By default, the server starts on a random port and triggers the regular LDAP support.
There is no need to specify a If there is a By default, a standard schema is used to validate InfluxDB is an open-source time series database optimized for fast, high-availability storage and retrieval of time series data in fields such as operations monitoring, application metrics, Internet-of-Things sensor data, and real-time analytics. Spring Boot auto-configures an spring.influx.url=http://172.0.0.1:8086 If the connection to InfluxDB requires a user and password, you can set the
InfluxDB relies on OkHttp. If you need to tune the http client
|