美文网首页
spring boot 集成spring-boot-starte

spring boot 集成spring-boot-starte

作者: 有点儿意思 | 来源:发表于2021-09-08 16:48 被阅读0次

    我的项目结构

    ***-api:接口层
    
    ***-app:业务层
    
    ***-domain:领域层
    
    ***-sharedModel:共享模型
    
    ***-infrastructure:基础层
    

    继承ElasticsearchRepository接口的类在基础层,代码如下:

    @Repository
    
    public interface QuestionEsRepository extends ElasticsearchRepository {
    
    }
    

    ES相关配置ben也在此层,代码如下:

    @Configuration
    @EnableElasticsearchRepositories
    public class ElasticsearchConfiguration extends AbstractElasticsearchConfiguration {
    
      @Value("${spring.elasticsearch.rest.uris}")
      private String uris;
    
      @Override
      @Bean
      public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration =ClientConfiguration.builder().connectedTo(uris.split(","))
              .withConnectTimeout(Duration.ofSeconds(5))
              .withSocketTimeout(Duration.ofSeconds(30))
              .build();
        return RestClients.create(clientConfiguration).rest();
      }
    
      @Bean("esRestTemplate")
      public ElasticsearchRestTemplate esRestTemplate() {
          return new ElasticsearchRestTemplate(elasticsearchClient());
      }
    }
    

    api层是整个系统的入口,也就是会启动springboot,在启动时,总是会报如下错误:

    Field questionRepository in com******domain.service.QuestionDomainService 
    required a bean of type 'com.*******.QuestionEsRepository' that could not be found.
    

    也就是说,spring boot是无法动态生成QuestionEsRepository接口的代理对象的。经过这样那样的搜....。最终发现,应该在配置类ElasticsearchConfiguration,的@EnableElasticsearchRepositories注解中,增加对于继承es包的扫描。

    代码修改后如下:
    @EnableElasticsearchRepositories(basePackages = {"com.****.gatewayimpl"})

    配置类完整代码如下:

    
    @Configuration
    @EnableElasticsearchRepositories(basePackages = {"com.****.gatewayimpl"})
    public class ElasticsearchConfiguration extends AbstractElasticsearchConfiguration {
      @Value("${spring.elasticsearch.rest.uris}")
      private String uris;
    
      @Override
      @Bean
      public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration =ClientConfiguration.builder().connectedTo(uris.split(","))
              .withConnectTimeout(Duration.ofSeconds(5))
              .withSocketTimeout(Duration.ofSeconds(30))
              .build();
        return RestClients.create(clientConfiguration).rest();
      }
    
      @Bean("esRestTemplate")
      public ElasticsearchRestTemplate esRestTemplate() {
          return new ElasticsearchRestTemplate(elasticsearchClient());
      }
    }
     
    

    具体原理还待有时间深入研究

    相关文章

      网友评论

          本文标题:spring boot 集成spring-boot-starte

          本文链接:https://www.haomeiwen.com/subject/hgbvwltx.html