0

我想使用 ElasticSearch 和 Spring Data。我添加了这个依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

我想使用自动配置。我遵循这个文档:https ://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-connecting-to-elasticsearch-rest

If you have the org.elasticsearch.client:elasticsearch-rest-high-level-client 
dependency on the classpath, Spring Boot will auto-configure a RestHighLevelClient

我创建了存储库:

@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {

    Page<Article> findByAuthorsName(String name, Pageable pageable);
}

我添加了这个,但它现在可以工作了!

启动我的应用程序后出现错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'articleRepository' defined in com.example.elasticsearch.repository.ArticleRepository defined in @EnableElasticsearchRepositories declared on Config: 
Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'elasticsearchTemplate' available
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342)

但是为什么会这样呢?我想使用自动配置,我不想手动描述 bean,正如在对 stackoverflow 的其他响应中所说的那样!

我使用 Spring boot 2.3.1.RELEASE 和 Java 11

编辑

我的配置:

@Configuration
@EnableElasticsearchRepositories(basePackages = " com.example.elasticsearch.repository")
public class Config {
}

我的应用程序:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
4

2 回答 2

2

我试图在本地重现您的设置,一切正常。

正如问题评论中所问的那样,我启用了调试输出以获取我的应用程序的自动配置报告,我得到:

   ElasticsearchDataAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' (OnClassCondition)

   ElasticsearchDataConfiguration.BaseConfiguration#elasticsearchConverter matched:
      - @ConditionalOnMissingBean (types: org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; SearchStrategy: all) did not find any beans (OnBeanCondition)

   ElasticsearchDataConfiguration.BaseConfiguration#mappingContext matched:
      - @ConditionalOnMissingBean (types: org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; SearchStrategy: all) did not find any beans (OnBeanCondition)

   ElasticsearchDataConfiguration.RestClientConfiguration matched:
      - @ConditionalOnClass found required class 'org.elasticsearch.client.RestHighLevelClient' (OnClassCondition)

   ElasticsearchDataConfiguration.RestClientConfiguration#elasticsearchTemplate matched:
      - @ConditionalOnBean (types: org.elasticsearch.client.RestHighLevelClient; SearchStrategy: all) found bean 'elasticsearchRestHighLevelClient'; @ConditionalOnMissingBean (names: elasticsearchTemplate types: org.springframework.data.elasticsearch.core.ElasticsearchOperations; SearchStrategy: all) did not find any beans (OnBeanCondition)

正如预期的那样,它ElasticsearchRestTemplate是由 Spring Boot 创建的。请使用请求的信息更新您的问题。

于 2020-06-19T14:06:06.130 回答
0

自动配置将假设一些事情来配置您的上下文,但是您仍然需要将 bean 连接到您的类中才能使用它。

验证所有依赖项都作为构建的一部分导入,然后确保将弹性搜索模板 bean 连接到您的类中。

如果您仍然遇到问题,您可以发布您的课程以获得更好的见解。

希望这可以帮助!

于 2020-06-17T20:31:43.267 回答