【发布时间】: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);
}
}
【问题讨论】:
-
能否在您的问题中提供此应用程序的自动配置报告?见docs.spring.io/spring-boot/docs/current/reference/html/…
-
我的配置很简单。我只是启用
@EnableElasticsearchRepositories。我更新了我的问题。
标签: java spring-boot elasticsearch spring-data spring-autoconfiguration