"Error creating bean with name" 沙发底座 + Spring

"Error creating bean with name" Couchbase + Spring

我正在尝试弄清楚如何让 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 从而解决了连接问题。

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

localhost 上的 运行 是否已启动 Couchbase 服务器?如果您在实际配置中使用另一个 IP,客户端机器可以 ping 通它吗?有多少延迟?

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

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