k_b
k_b

Reputation: 343

Couchbase with Spring data using spring config example

I am a newbie to couchbase. I am using spring in my application through which I am trying to connect to couchbase in my local. I am trying to create a couchbase template(similar to what is done in mongo template) in configurations as below:

Spring configuration

Repository using couchbase template

But when I start application, I get autowiring errors and I am unable to fetch back anything. Can some please help with above issue? Or if someone can share sample code to extract data from couchbase using sprin-data & spring configuration? Please help!

Upvotes: 1

Views: 1553

Answers (1)

Simon Baslé
Simon Baslé

Reputation: 28301

So you are using Spring Data Couchbase 1.4? The framework offers a dedicated namespace for the Spring xml configuration that you should use instead of attempting to call constructors:

xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:couchbase="http://www.springframework.org/schema/data/couchbase"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/data/couchbase http://www.springframework.org/schema/data/couchbase/spring-couchbase.xsd
                       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <couchbase:couchbase host="127.0.0.1,192.168.1.101" bucket="default" password=""/>

    <couchbase:template/> <!-- will use default client above, no additional tuning-->
</beans>

First notice the xmlns:couchbase and the couchbase-specific xsi:schemaLocation in the root.

Secondly, not providing ids will wire up beans with the default IDs (and same for references, eg. the default template will reference the default client if no "client-ref" attribute is specified).

Thirdly, notice the format for the "host" parameter is just a String, hostnames or ips separated by commas (in case you want to bootstrap from a cluster).

At this point the template should be available for autowiring.

repository

For CRUD operations, Spring Data Couchbase already comes with a CRUDRepository abstraction. You only need to:

  • declare an interface UsersRepository extending CRUDRepository<Users, String> (string being the type of the Users field identifying it). Let's say it's in package com.test.repo.
  • enable building of repositories by the framework in the xml config: <repositories base-package="com.test.repo" /> (click for relevant section of the doc)
  • autowire your repository: @Autowired public UsersRepository repo;

The documentation has many more details about concepts like the repositories, pre-requisites for a CRUD repository to work in couchbase (short story: you need to create a view to back it up), how to model and annotate entities, etc...

Please have a look at it, at http://docs.spring.io/spring-data/couchbase/docs/1.4.0.RELEASE/reference/html/

Upvotes: 2

Related Questions