带有Couchbase服务的Spring WebMVC-没有名为“ couchbaseRepositoryOperationsMapping”的bean

时间:2019-06-16 08:40:09

标签: java spring spring-mvc spring-data couchbase

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

我尝试根据以下教程Couchbase with Spring-Boot and Spring Data

创建couchfa服务

我具有以下项目结构:

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

当尝试自动装配服务时,我得到一个例外,因为没有一个名为couchbaseRepositoryOperationsMapping的bean,这是因为我的存储库类扩展了CouchbasePagingAndSortingRepository而引发异常。

我收到以下异常:

  

[main] INFO org.springframework.web.servlet.DispatcherServlet-   初始化Servlet'dispatcher'[main] INFO   org.springframework.data.repository.config.RepositoryConfigurationDelegate   -找到多个Spring Data模块,进入严格的存储库配置模式! [主要] INFO   org.springframework.data.repository.config.RepositoryConfigurationDelegate   -在默认模式下引导Spring数据存储库。 [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate   -在64毫秒内完成Spring Data信息库扫描。找到1个存储库接口。 [主要]警告   org.springframework.web.context.support.AnnotationConfigWebApplicationContext   -在上下文初始化期间遇到异常-取消刷新尝试:   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为“ buildingServiceImpl”的bean时出错:不满意   通过字段“ buildingRepository”表示的依赖关系;嵌套的   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为“ buildingRepository”的bean时出错:   “ buildingRepository”取决于缺少的bean   'couchbaseRepositoryOperationsMapping';嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   名为“ couchbaseRepositoryOperationsMapping”的bean可用[主要]   错误org.springframework.web.servlet.DispatcherServlet-上下文   初始化失败   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为“ buildingServiceImpl”的bean时出错:不满意   通过字段“ buildingRepository”表示的依赖关系;嵌套的   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为“ buildingRepository”的bean时出错:   “ buildingRepository”取决于缺少的bean   'couchbaseRepositoryOperationsMapping';嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   名为“ couchbaseRepositoryOperationsMapping”的bean可用

     

据我了解,由于没有bean,它不起作用   名为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。所有类均基于教程Couchbase with Spring-Boot and Spring Data。我会错过注释吗,因此spring无法自动为班级布线?

我的存储库类:

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

}

1 个答案:

答案 0 :(得分:2)

您错过了配置类沙发式服务器

创建一个专用的配置类,为您的couhcebase扩展AbstractCouchbaseConfiguration,如下所示(server @,user,pwd)

@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"),以便扫描存储库界面和服务