gpt4 book ai didi

java - 带有 Couchbase 服务的 Spring WebMVC - 没有可用的名为 'couchbaseRepositoryOperationsMapping' 的 bean

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:44 24 4
gpt4 key购买 nike

我正在使用 thymeleaf 创建一个简单的 Spring WebMVC 应用程序,现在我想添加一个服务来提供与 couchbase 服务器的连接。

我尝试根据以下教程创建 couchbase 服务 Couchbase with Spring-Boot and Spring Data

我有以下项目结构:

src/main/java
com.project
config
MvcWebApplicationInitializer
MvcWebConfig
controller
IndexController
model
Area
Building
BuildingRepository
BuildingService
BuildingServiceImpl

当尝试 Autowiring 服务时,我得到一个异常,即没有名为 couchbaseRepositoryOperationsMapping 的 bean,这是因为我的存储库类扩展了 CouchbasePagingAndSortingRepository,它引发了异常。

我得到以下异常:

[main] INFO org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcher' [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode! [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode. [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 64ms. Found 1 repository interfaces. [main] WARN org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'buildingServiceImpl': Unsatisfied dependency expressed through field 'buildingRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'buildingRepository': 'buildingRepository' depends on missing bean 'couchbaseRepositoryOperationsMapping'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'couchbaseRepositoryOperationsMapping' available [main] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'buildingServiceImpl': Unsatisfied dependency expressed through field 'buildingRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'buildingRepository': 'buildingRepository' depends on missing bean 'couchbaseRepositoryOperationsMapping'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'couchbaseRepositoryOperationsMapping' available

As far as I understand it is not working because there is no bean named couchbaseRepositoryOperationsMapping.

我的 MvcWebConfig:

@Configuration
@EnableWebMvc
@ComponentScan("com.xplorg.controller")
@EnableCouchbaseRepositories(basePackages = {"com.xplorg.model"})
public class MvcWebConfig implements WebMvcConfigurer {

@Autowired
private ApplicationContext applicationContext;

@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}

public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
registry.viewResolver(viewResolver);
}

public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

}

我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.project</groupId>
<artifactId>xplorg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Project</name>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>3.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.18.v20190429</version>
</plugin>
</plugins>
</build>
</project>

****更新****

添加 CouchbaseConfig 类解决了 couchbaseRepositoryOperationsMapping 的问题,但我现在得到另一个错误,现在有一个名为 buildingRepository 的 bean。所有类(class)均基于教程 Couchbase with Spring-Boot and Spring Data .我是否错过了注释,因此 spring 无法 Autowiring 该类?

我的存储库类:

@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "building")
public interface BuildingRepository extends CouchbasePagingAndSortingRepository<Building, String> {

List<Building> findByCompanyId(String companyId);

Page<Building> findByCompanyIdAndNameLikeOrderByName(String companyId, String name, Pageable pageable);

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and companyId = $1 and $2 within #{#n1ql.bucket}")
Building findByCompanyAndAreaId(String companyId, String areaId);

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} AND ANY phone IN phoneNumbers SATISFIES phone = $1 END")
List<Building> findByPhoneNumber(String telephoneNumber);

@Query("SELECT COUNT(*) AS count FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} and companyId = $1")
Long countBuildings(String companyId);
}

我的服务类:

@Service
public class BuildingServiceImpl implements BuildingService {

@Autowired
private BuildingRepository buildingRepository;

@Override
public List<Building> findByCompanyId(String companyId) {
return buildingRepository.findByCompanyId(companyId);
}

public List<Building> findByCompanyIdAndNameLike(String companyId, String name, int page) {
return buildingRepository.findByCompanyIdAndNameLikeOrderByName(companyId, name, new PageRequest(page, 20))
.getContent();
}

@Override
public Building findByCompanyAndAreaId(String companyId, String areaId) {
return buildingRepository.findByCompanyAndAreaId(companyId, areaId);
}

@Override
public List<Building> findByPhoneNumber(String telephoneNumber) {
return buildingRepository.findByPhoneNumber(telephoneNumber);
}

@Override
public Building findById(String buildingId) {
return buildingRepository.findById(buildingId).get();
}

@Override
public Building save(Building building) {
return buildingRepository.save(building);
}

@Override
public Long countBuildings(String companyId) {
return buildingRepository.countBuildings(companyId);
}

}

最佳答案

你错过了配置类couchebase服务器

创建一个扩展 AbstractCouchbaseConfiguration 的专用配置类对于你的 couhcebase 如下(服务器@,用户,密码)

@Configuration
@EnableCouchbaseRepositories(basePackages = {"com.xplorg.model"})
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {

@Override
protected List<String> getBootstrapHosts() {
return Arrays.asList("your server ip (like localhost in local)");
}

@Override
protected String getBucketName() {
return "username";
}

@Override
protected String getBucketPassword() {
return "passsword";
}
}

还要在您的项目配置中将 @ComponentScan("com.xplorg.controller") 更改为 @ComponentScan("com.xplorg") 以便您的存储库接口(interface)和服务应该扫描

关于java - 带有 Couchbase 服务的 Spring WebMVC - 没有可用的名为 'couchbaseRepositoryOperationsMapping' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56617334/

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