Couchbase
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 spring-boot-starter-data-couchbase
and spring-boot-starter-data-couchbase-reactive
“Starters” for collecting the dependencies in a convenient way.
Connecting to Couchbase
You can get a Cluster
by adding the Couchbase SDK and some configuration.
The spring.couchbase.*
properties can be used to customize the connection.
Generally, you provide the connection string, username, and password, as shown in the following example:
-
Properties
-
YAML
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"
It is also possible to customize some of the ClusterEnvironment
settings.
For instance, the following configuration changes the timeout to open a new Bucket
and enables SSL support:
-
Properties
-
YAML
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"
Check the spring.couchbase.env.* properties for more details.
To take more control, one or more ClusterEnvironmentBuilderCustomizer beans can be used.
|
Spring Data Couchbase Repositories
Spring Data includes repository support for Couchbase. For complete details of Spring Data Couchbase, see the reference documentation.
You can inject an auto-configured CouchbaseTemplate
instance as you would with any other Spring Bean, provided a CouchbaseClientFactory
bean is available.
This happens when a Cluster
is available, as described above, and a bucket name has been specified:
-
Properties
-
YAML
spring.data.couchbase.bucket-name=my-bucket
spring:
data:
couchbase:
bucket-name: "my-bucket"
The following examples shows how to inject a CouchbaseTemplate
bean:
-
Java
-
Kotlin
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final CouchbaseTemplate template;
public MyBean(CouchbaseTemplate template) {
this.template = template;
}
// ...
public String someMethod() {
return this.template.getBucketName();
}
}
import org.springframework.data.couchbase.core.CouchbaseTemplate
import org.springframework.stereotype.Component
@Component
class MyBean(private val template: CouchbaseTemplate) {
// ...
fun someMethod(): String {
return template.bucketName
}
}
There are a few beans that you can define in your own configuration to override those provided by the auto-configuration:
-
A
CouchbaseMappingContext
@Bean
with a name ofcouchbaseMappingContext
. -
A
CustomConversions
@Bean
with a name ofcouchbaseCustomConversions
. -
A
CouchbaseTemplate
@Bean
with a name ofcouchbaseTemplate
.
To avoid hard-coding those names in your own config, you can reuse BeanNames
provided by Spring Data Couchbase.
For instance, you can customize the converters to use, as follows:
-
Java
-
Kotlin
import org.assertj.core.util.Arrays;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.BeanNames;
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions;
@Configuration(proxyBeanMethods = false)
public class MyCouchbaseConfiguration {
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
public CouchbaseCustomConversions myCustomConversions() {
return new CouchbaseCustomConversions(Arrays.asList(new MyConverter()));
}
}
import org.assertj.core.util.Arrays
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.couchbase.config.BeanNames
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions
@Configuration(proxyBeanMethods = false)
class MyCouchbaseConfiguration {
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
fun myCustomConversions(): CouchbaseCustomConversions {
return CouchbaseCustomConversions(Arrays.asList(MyConverter()))
}
}