2

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

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

主.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();
}

但是在运行时显示错误。

线程“主”org.springframework.dao.InvalidDataAccessResourceUsageException 中的异常:无法为设计文档“内容”加载视图“addIndex”;嵌套异常是 com.couchbase.client.protocol.views.InvalidViewException:无法为设计文档“内容”加载视图“addIndex”

4

2 回答 2

1

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

主.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();
}
于 2014-11-28T04:28:11.450 回答
1

Spring Data Couchbase 文档涵盖以下内容:Spring Data Reference Link

为了快速进行高级概述,您可以使用所需的客户方法为您的实现创建一个接口。然后为那些与您的 CrudRepository 接口同名但后缀为“Impl”的方法创建实现类;这个后缀很重要。然后,当您创建扩展 Spring CrudRepository 的接口时,您不仅扩展了 CrudRepository,还扩展了您为自定义方法创建的接口。然后,当您正常构建时,Spring 应该使用您指定的自定义方法处理生成 CrudRepository。

这是上面引用的 Spring 文档中的一个简单示例。首先是自定义界面:

interface UserRepositoryCustom {
  public void someCustomMethod(User user);
}

现在这个接口的实现:

class UserRepositoryImpl implements UserRepositoryCustom {

  public void someCustomMethod(User user) {
    // Your custom implementation
  }
}

最后是您的 CrudRepository 的典型接口,还扩展了您的自定义接口:

interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {

  // Declare query methods here
}

有关更多详细信息,请参考上面链接的 Spring Data Couchbase 文档。

于 2015-11-04T01:51:00.227 回答