2020-02-07

Spring Couchbase auto-configuration

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-datacouchbase and spring-boot-starter-data-couchbase-reactive “Starters” for collecting the dependencies in a convenient way.

Connecting to Couchbase

You can get a Bucket and Cluster by adding the Couchbase SDK and some configuration. The
spring.couchbase.* properties can be used to customize the connection. Generally, you provide the
bootstrap hosts, bucket name, and password, as shown in the following example:
spring.couchbase.bootstrap-hosts=my-host-1,192.168.1.123
spring.couchbase.bucket.name=my-bucket
spring.couchbase.bucket.password=secret

You need to provide at least the bootstrap host(s), in which case the bucket name is
default and the password is an empty String. Alternatively, you can define your
own org.springframework.data.couchbase.config.CouchbaseConfigurer @Bean to take
control over the whole configuration.

It is also possible to customize some of the CouchbaseEnvironment settings. For instance, the following
configuration changes the timeout to use to open a new Bucket and enables SSL support:
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.couchbase.env.* properties for more details.

Spring Data Couchbase Repositories

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 CouchbaseTemplate instance as you would with any other Spring
Bean, provided a default CouchbaseConfigurer is available (which happens when you enable
Couchbase support, as explained earlier).
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:
• A CouchbaseTemplate @Bean with a name of couchbaseTemplate.
• An IndexManager @Bean with a name of couchbaseIndexManager.
• A CustomConversions @Bean with a name of couchbaseCustomConversions.

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:

@Configuration(proxyBeanMethods = false)
public class SomeConfiguration {
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
public CustomConversions myCustomConversions() {
return new CustomConversions(...);
}
// ...
}

If you want to fully bypass the auto-configuration for Spring Data Couchbase,
provide your own implementation of
org.springframework.data.couchbase.config.AbstractCouchbaseDataConfiguration.

No comments:

Post a Comment