Search code examples
springcouchbasespring-data-couchbase

How to access a bucket object in spring-data-couchbase?


I'm using spring-data-couchbase:2.0.0.RC1 in my app but I have some legacy code that relies on Bucket object and work with it directly. Is there a way how to get Bucket object from spring-data-couchbase ? I found this http://docs.spring.io/spring-data/couchbase/docs/2.0.0.RC1/reference/html/#couchbase.template in documentation but can't figure out, how to configure it in my app.

My spring config object is as below ...

@Configuration
public class SpringCouchbaseConfig extends AbstractCouchbaseConfiguration {

    @Value("${scheduled.task.couchbase_sync.host}")
    String host;

    @Value("${scheduled.task.couchbase_sync.bucket}")
    String bucket;

    @Override
    protected List<String> getBootstrapHosts() {
        return Arrays.asList(this.host);
    }

    @Override
    protected String getBucketName() {
        return this.bucket;
    }

    @Override
    protected String getBucketPassword() {
        return "";
    }

    @Override
    protected CouchbaseEnvironment getEnvironment() {
        return DefaultCouchbaseEnvironment.builder()
                .connectTimeout(TimeUnit.SECONDS.toMillis(10))
                .computationPoolSize(6)
                .build();
    }


    @Bean(name = "org.springframework.data.couchbase.core.convert.customConversions")
    public CustomConversions customConversions() {
        return new CustomConversions(Collections.emptyList());
    }

}

Solution

  • If you are only configuring one Bucket, the AbstractCouchbaseConfiguration will declare a @Bean named couchbaseBucket of type Bucket so you could autowire it where you need it:

    @Autowired
    private Bucket myBucketUsedBySpringData;
    

    If you have a reference to the CouchbaseTemplate (eg. also autowired somewhere) you can also easily obtain the backing Bucket by calling getCouchbaseBucket() on it.