java - spring data couchbase中的自定义方法

标签 java spring-data couchbase spring-data-couchbase

我需要为spring data couchbase存储库编写一个自定义方法。这是我的代码。

CBsampleRepositoryCustom.java

public interface CBsampleRepositoryCustom  {
public void addIndex() ;
}

CBsampleRepositoryImpl.java

public class CBsampleRepositoryImpl implements CBsampleRepositoryCustom {
@Override
public void addIndex() {
    System.out.println("CBsampleRepositoryCustomImpl createIndex");
}
}

CBsampleRepository.java

@Repository
public interface CBsampleRepository extends  CouchbaseRepository<Content,String> ,     CBsampleRepositoryCustom{
}

CouchBaseBeansConfiguration.java

@Configuration
public class CouchBaseBeansConfiguration {
@Bean
public CouchbaseClient couchbaseClient() throws IOException {

    return new CouchbaseClient(Arrays.asList(URI
            .create("http://localhost:8091/pools")), "test", "");
}
@Bean
public CouchbaseTemplate couchbaseTemplate() throws IOException {
    return new CouchbaseTemplate(couchbaseClient());
}
}

Main.java

public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
        CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
        CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
        template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class);
repository.addIndex();
}

但是在运行时显示错误。

Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: Could not load view "addIndex" for design doc "content"; nested exception is com.couchbase.client.protocol.views.InvalidViewException: Could not load view "addIndex" for design doc "content"

最佳答案

我们需要将实现类对象传递给 getRepository 函数。如下更改主类。

Main.java

public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
    CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
    CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
    template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,new CBsampleRepositoryImpl());
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,custom);
repository.addIndex();
}

关于java - spring data couchbase中的自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27166442/

相关文章:

java - 多次重用 PreparedStatement

java - ZK 应用程序测试套件 (ZATS) 返回 java.lang.NoClassDefFoundError : org/zkoss/zats/ZatsException

java - JAX-WS 在 AppEngine 上运行缓慢

rest - 无限制地从 couchbase 全文搜索 rest API 获取所有结果

java - 如何将空格替换为 nbsp?

spring-data - 如何在 spring 数据 mongoDB 中使用 @DocumentField 消除歧义

eclipse - 使用运行对话框 (F11) 在 Eclipse 中的远程集群上启动 Hadoop Map Reduce 作业

java - Spring 数据 Neo4j : Complex relationships not persisting

java - membase 服务器到 couchbase 服务器迁移 : Java client

java - 如何让服务层在从数据库中获取任何内容时返回 Mono::error