gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:59:35 25 4
gpt4 key购买 nike

我需要为 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());
}
}

主.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 函数。如下更改主类。

主.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com