首页 文章

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

提问于
浏览
2

我需要为 spring data couchbase repository 写一个 custom method . 这是我的代码 .

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 回答

  • 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

    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文档 .

相关问题