“创建名为”Couchbase + Spring

时间:2016-06-15 22:40:07

标签: java spring couchbase nosql

我正在试图弄清楚如何让Spring与Couchbase一起工作,但出于某种原因我得到了以下异常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookRepo': Cannot resolve reference to bean 'couchbaseTemplate' while setting bean property 'couchbaseOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couchbaseTemplate': Cannot resolve reference to bean 'couchbaseBucket' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couchbaseBucket': Invocation of init method failed; nested exception is java.lang.RuntimeException: java.util.concurrent.TimeoutException

连接正常,但由于某种原因无法创建bean。

这是我的spring-couchbase-integration.xml文件:

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

    <couchbase:cluster>
        <couchbase:node>127.0.0.1</couchbase:node>
    </couchbase:cluster>

    <!-- This is needed to probe the server for N1QL support -->
    <!-- Can be either cluster credentials or a bucket credentials -->
    <couchbase:clusterInfo login="login"
        password="password" />

    <beans:bean id="couchbaseEnv"
        class="com.couchbase.client.java.env.DefaultCouchbaseEnvironment"
        factory-method="create" />
    <beans:bean id="myCustomTranslationService"
        class="org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService" />

    <couchbase:indexManager />

    <couchbase:repositories base-package="com.jcg.examples.repo" />

    <couchbase:template translation-service-ref="myCustomTranslationService" />

    <couchbase:bucket bucketName="JavaCodeGeeks"
        bucketPassword="password.1" />
</beans:beans>

这是存储库:

package com.jcg.examples.repo;

...

@Repository
public interface BookRepo extends CouchbaseRepository<Book, Long> {

    @Query(value = "select * from JavaCodeGeeks")
    public List<Book> getBooksByContainedWord(String containedString);
}

文件:

package com.jcg.examples.entity;

...

@Document(expiry = 0)
public class Book {

    @Id
    private long bookId;

    public long getBookId() {
        return bookId;
    }

    public void setBookId(long bookId) {
            this.bookId = bookId;
    }

}

这就是我在测试它的方式:

package com.jcg.examples;

...

public class ApplicationTest {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("spring-couchbase-integration.xml").getPath());
    }
}

该示例来自this website。我设法解决了连接问题,因为我实际上并没有使用localhost,但我无法解决这个问题。

编辑:问题已修复by configuring the Docker container properly,从而解决了连接问题。

1 个答案:

答案 0 :(得分:3)

该消息表明SDK无法足够快地连接到群集。默认超时为5秒。

Couchbase Server是否已在localhost启动并运行?如果您在实际配置中使用其他IP,客户端机器是否可以ping通它?有什么延迟?

您可以尝试设置更高的超时(以毫秒为单位):

<couchbase:env id="couchbaseEnv" connectTimeout="10000" />