【问题标题】:NoSuchBeanDefinitionException: No bean named 'elasticsearchTemplate' available when I use AutoconfigurationNoSuchBeanDefinitionException:当我使用自动配置时,没有名为“elasticsearchTemplate”的 bean 可用
【发布时间】:2020-10-07 18:01:39
【问题描述】:

我想使用 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);
    }

}

【问题讨论】:

标签: java spring-boot elasticsearch spring-data spring-autoconfiguration


【解决方案1】:

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

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

   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 创建的。请使用请求的信息更新您的问题。

【讨论】:

  • 原来我遇到了一些问题。我重新创建了测试项目,一切对我来说都很好。谢谢。
【解决方案2】:

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

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

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

希望这会有所帮助!

【讨论】:

  • 什么意思?我更新了问题。我刚刚创建了一个存储库并想使用它。默认情况下,连接应在 localhost:9200 上进行。
  • 我唯一能想到的不起作用的是您使用的是 IDE,您添加了 elasticsearch-rest-high-level-client 依赖项并且您拥有未刷新 IDE 中的依赖项。我建议您从 CLI 或您的 IDE 中清理构建;刷新依赖项,清理,构建并运行它。另外,启用执行器,如果应用程序启动,则列出 bean,并确保您看到列出的 elasticsearchTemplate 请告知进展情况!
猜你喜欢
  • 2018-11-07
  • 2020-10-23
  • 2016-01-07
  • 2022-10-09
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
  • 2019-10-22
相关资源
最近更新 更多