弹簧数据沙发基地中的自定义方法

时间:2014-11-27 08:49:12

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

但是在运行时显示错误。

  

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

2 个答案:

答案 0 :(得分:1)

我们需要将实现类对象传递给getRepository function.Change main class,如下所示。

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

答案 1 :(得分:1)

Spring Data Couchbase文档涵盖了这一点:Spring Data Reference Link

要快速进行高级概述,请使用所需的客户方法为实施创建界面。然后为那些与CrudRepository接口同名的方法创建实现类,但后缀为&#34; Impl&#34 ;;这个后缀很重要。然后,当您创建扩展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文档。