Spring Data - CouchbaseTemplate / Bucket direct access


I’ve been looking to try out Spring Boot to make a quick REST API with Couchbase Spring Data for some tests.

For one particular part, I’d like to access the lower-level Couchbase API’s directly. Fortunately it seems that CouchbaseTemplate provides a getCouchbaseBucket() method for exactly this purpose.

My issue is that I can’t quite see how to wire everything up so that the CouchbaseTemplate is accessible from the right classes. Or, in fact, where would be the appropriate place to access the CouchbaseTemplate.

Currently I have several basic files:

Application, Greeting,  GreetingController,  GreetingRepository

Application implements getBootstrapHosts, getBucketName, getBucketPassword and has the main function which runs the SpringApplication:

public class Application extends AbstractCouchbaseConfiguration

The Greeting class is a basic entity with an ID and a field with accessor methods:

public class Greeting {
    private final String id;
    private final String name;

GreetingRepository is currently a blank interface which extends CouchbaseRepository:

interface GreetingRepository extends CouchbaseRepository<Greeting, String>

GreetingController has REST end points and GreetingRepository reference:

public class GreetingController {

    private GreetingRepository repository;
public Greeting cbgreeting() {

Going back to the original question, where is the appropriate place to access CouchbaseTemplate, and what class structure is best to wrap the calls in?

It seems SimpleCouchbaseRepository has references to CouchbaseTemplate whilst the CouchbaseRepository interface does not have the relevant methods.


A couple of things:

  • first class integration in Spring Boot is coming and we’re currently working with the boot team to make it a great experience. Notably, we are looking at letting Spring Boot instantiate the SDK beans (Cluster, Bucket) and pass them to Spring Data, but also potentially to other components like Spring Cache, Spring Session, even if you don’t have need for Spring Data.

  • I’ll add the getter on the template (or rather the CouchbaseOperations) in the CouchbaseRepository interface. Tracked now in DATACOUCH-216.

  • These are beans, so you should be able to @Autowired the Bucket if you only use one, anywhere in your application. It really depends on what you are planning to do with it.

  • Even if you have multiple beans of the same type, by default the beans are named after the constants in BeanNames, which will become public in 2.1, so there’s that to look forward to as well.

Hope this sets you on the right path!


Ah, so actually it seems that in this case it’s just as simple as adding:

private Bucket cbBucket;

I.e. As there is only one bucket, it’s automatically being registered in the context, and so @Autowired is injecting the instance here as opposed to having to chase down lots of method calls to retrieve the instance?

This seems to be working well… looks like the answer was fairly straightforward this time!

Thanks for your help.