gpt4 book ai didi

java - NoSuchBeanDefinitionException:当我使用自动配置时,没有名为 'elasticsearchTemplate'的bean可用

转载 作者:行者123 更新时间:2023-12-03 01:16:14 27 4
gpt4 key购买 nike

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

}

最佳答案

我尝试在本地复制您的设置,并且一切正常。

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

   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创建的,符合预期。请使用要求的信息更新您的问题。

关于java - NoSuchBeanDefinitionException:当我使用自动配置时,没有名为 'elasticsearchTemplate'的bean可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62437116/

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