in my project I have a Couchbase cluster. I also have different buckets for different datas. I want to configure them in CouchbaseConfiguration
file as it is specified inDocumentation but I got NullPointerException
.
I am using spring-data-couchbase
in pom.xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>4.2.5</version>
</dependency>
And this is my CouchbaseConfiguration file.
package com.example.cbase.configuration;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@Override
public String getConnectionString() {
return "10.10.56.12,10.10.56.13,10.10.56.14";
}
@Override
public String getUserName() {
return "user1";
}
@Override
public String getPassword() {
return "password1!";
}
@Override
public String getBucketName() {
return null;
}
@Bean
public Bucket testCaseBucket(Cluster cluster) {
return cluster.bucket("testCaseBucket");
}
@Bean
public Bucket testRunBucket(Cluster cluster) {
return cluster.bucket("testRunBucket");
}
@Bean
public Bucket userLogBucket(Cluster cluster) {
return cluster.bucket("userLogBucket");
}
@Bean
public Bucket scheduleBucket(Cluster cluster) {
return cluster.bucket("scheduleBucket");
}
}
In your configuration, you need to override configureReactiveRepositoryOperationsMapping() and configureRepositoryOperationsMapping()
You can find this in
git clone git@github.com:spring-projects/spring-data-couchbase.git
cd spring-data-couchbase
./src/test/java/org/springframework/data/couchbase/domain/Config.java
@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName()
} catch (Exception e) {
throw e;
}
}
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
MappingCouchbaseConverter cvtr = (MappingCouchbaseConverter)baseMapping.getDefault().getConverter();
CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName()
} catch (Exception e) {
throw e;
}
}
// do not use reactiveCouchbaseTemplate for the name of this method, otherwise the value of that bean
// will be used instead of the result of this call (the client factory arg is different)
public ReactiveCouchbaseTemplate myReactiveCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
MappingCouchbaseConverter mappingCouchbaseConverter) {
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter,
new JacksonTranslationService(), getDefaultConsistency());
}
// do not use couchbaseTemplate for the name of this method, otherwise the value of that been
// will be used instead of the result from this call (the client factory arg is different)
public CouchbaseTemplate myCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
MappingCouchbaseConverter mappingCouchbaseConverter) {
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, new JacksonTranslationService(),
getDefaultConsistency());
}
// do not use couchbaseClientFactory for the name of this method, otherwise the value of that bean will
// will be used instead of this call being made ( bucketname is an arg here, instead of using bucketName() )
public CouchbaseClientFactory myCouchbaseClientFactory(String bucketName) {
return new SimpleCouchbaseClientFactory(getConnectionString(), authenticator(), bucketName);
}