“使用名称创建bean时出错” Couchbase + Spring

tom*_*ssi 5 java spring nosql couchbase

我试图弄清楚如何使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
Run Code Online (Sandbox Code Playgroud)

连接很好,但是由于某种原因无法创建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>
Run Code Online (Sandbox Code Playgroud)

这是存储库:

package com.jcg.examples.repo;

...

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

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

该文件:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我测试的方式:

package com.jcg.examples;

...

public class ApplicationTest {

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

该示例取自该网站。由于我实际上没有使用过,localhost但我设法解决了连接问题,但无法弄清楚这一点。

编辑通过正确配置Docker容器解决了问题,从而解决了连接问题。

Sim*_*slé 3

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

Couchbase Server 是否已启动并正在运行localhost?如果您在实际配置中使用其他 IP,客户端计算机是否可以 ping 通该 IP?延迟是多少?

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

<couchbase:env id="couchbaseEnv" connectTimeout="10000" />
Run Code Online (Sandbox Code Playgroud)