Couchbase is an open source, document-oriented, distributed,
multi-model NoSQL database optimized for interactive applications. In addition, Spring Boot provides
auto-configuration for Couchbase and abstractions provided through Spring Data Couchbase.
For convenient collection of dependencies, there are “starter packages”
spring-boot-starter-data-couchbase
and spring-boot-starter-data-couchbase-reactive
Connect to Couchbase
You can get a Cluster
by adding the Couchbase SDK and some configuration. The
spring.couchbase.*
properties can be used to configure the connection. As a rule, connection
string is specified, username and password, as shown in the following example:
spring.couchbase.connection-string=couchbase://192.168.1.123
spring.couchbase.username=user
spring.couchbase.password=secret
spring:
couchbase:
connection-string: "couchbase://192.168.1.123"
username: "user"
password: "secret"
You can also configure some ClusterEnvironment
parameters. For example, the following configuration
changes the timeout setting used to open a new Bucket
and also enables SSL protocol support:
spring.couchbase.env.timeouts.connect=3s
spring.couchbase.env.ssl.key-store=/location/of/keystore.jks
spring.couchbase.env.ssl.key-store-password=secret
spring:
couchbase:
env:
timeouts:
connect: "3s"
ssl:
key-store: "/location/of/keystore.jks"
key-store-password: "secret"
spring.couchbase.env.*
properties for more details. To gain more control, you can use one or
more ClusterEnvironmentBuilderCustomizer
beans.
Spring Data Couchbase Repositories
Spring Data includes repository support for Couchbase.
An auto-configurable CouchbaseTemplate
instance can be implemented just like any other Spring bean, as
long as the CouchbaseClientFactory
bean is present. This is possible if you have the
Cluster
described above, and the name of the bucket is given:
spring.data.couchbase.bucket-name=my-bucket
spring:
data:
couchbase:
bucket-name: "my-bucket"
The following examples show how to implement the CouchbaseTemplate
bean:
@Component
public class MyBean {
private final CouchbaseTemplate template;
public MyBean(CouchbaseTemplate template) {
this.template = template;
}
}
@Component
class MyBean(private val template: CouchbaseTemplate) {
}
There are several beans that you can define in your own configuration to override those provided by autoconfiguration:
-
A
CouchbaseMappingContext
bean marked with the@Bean
annotation, namedcouchbaseMappingContext
. -
A
CustomConversions
bean marked with a@Bean
annotation namedcouchbaseCustomConversions
. -
A
CouchbaseTemplate
bean marked with the@Bean
annotation, namedcouchbaseTemplate
.
To avoid hard-coding these names in your own configuration, you can use the BeanNames
provided by Spring
Data Couchbase. For example, you can configure the converters you use as follows:
@Configuration(proxyBeanMethods = false)
public class MyCouchbaseConfiguration {
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
public CouchbaseCustomConversions myCustomConversions() {
return new CouchbaseCustomConversions(Arrays.asList(new MyConverter()));
}
}
@Configuration(proxyBeanMethods = false)
class MyCouchbaseConfiguration {
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
fun myCustomConversions(): CouchbaseCustomConversions {
return CouchbaseCustomConversions(Arrays.asList(MyConverter()))
}
}
GO TO FULL VERSION