首页 文章

使用spring配置示例使用Spring数据的Couchbase

提问于
浏览
1

我是沙发基地的新手 . 我在我的应用程序中使用spring,我正在尝试连接到本地的couchbase . 我正在尝试在配置中创建一个couchbase模板(类似于mongo模板中所做的),如下所示:

Spring configuration

Repository using couchbase template

但是,当我开始应用程序时,我收到自动装配错误,我无法取回任何东西 . 有人可以帮助解决上述问题吗?或者,如果有人可以共享示例代码以使用sprin-data和spring配置从couchbase中提取数据?请帮忙!

1 回答

  • 2

    那么您使用的是Spring Data Couchbase 1.4吗?该框架为Spring xml配置提供了一个专用的命名空间,您应该使用该命名空间而不是尝试调用构造函数:

    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>
    

    首先注意根中的 xmlns:couchbase 和特定于couchbase的 xsi:schemaLocation .

    其次,不提供ID将使用默认ID连接bean(对于引用也是如此,例如,如果未指定“ client-ref ”属性,则默认模板将引用默认客户端) .

    第三,注意“ host ”参数的格式只是一个 String ,主机名或用逗号分隔的ips(如果你想从集群引导) .

    此时,模板应该可用于自动装配 .

    repository

    对于CRUD操作,Spring Data Couchbase已经带有 CRUDRepository 抽象 . 你只需要:

    • 声明一个接口 UsersRepository extends CRUDRepository<Users, String> (string是标识它的 Users 字段的类型) . 让's say it'在包 com.test.repo 中 .

    • 启用xml配置框架构建存储库:<repositories base-package="com.test.repo" />(单击文档的相关部分)

    • 自动装配您的存储库: @Autowired public UsersRepository repo;

    该文档有更多关于存储库等概念的详细信息,CRUD存储库在couchbase中工作的先决条件(简短的故事:您需要创建一个视图来备份它),如何建模和注释实体等等......

    请看一下,http://docs.spring.io/spring-data/couchbase/docs/1.4.0.RELEASE/reference/html/

相关问题