You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot无法连接ElasticSearch,SimpleElasticsearchRepository实例化失败

问题描述

我的SpringBoot应用无法与ElasticSearch建立连接,启动时持续抛出以下错误:

UnsatisfiedDependencyException: Error creating bean with name 'filterController': Unsatisfied dependency expressed through field 'service': Error creating bean with name 'filterService': Unsatisfied dependency expressed through field 'repository': Error creating bean with name 'filterRepository' defined in repository.FilterRepository defined in @EnableElasticsearchRepositories declared on ElasticsearchRepositoriesRegistrar.EnableElasticsearchRepositoriesConfiguration: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception

代码相关信息

  • 未做任何额外配置,ElasticSearch正常运行
  • 未创建自定义配置文件,依赖SpringBoot自动完成配置
  • 完整源码可查看对应代码仓库的elasticsearch分支

Application.java文件

package br.com.uburu.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}

FilterController.java文件

package br.com.uburu.spring.controller;

// Imports

@RestController
@RequestMapping("/api/v1/filter")
public class FilterController {

    @Autowired
    private FilterService service;

    @GetMapping
    public ResponseEntity<List<Filter>> getAll() {
        List<Filter> filters = service.getAll();
        return new ResponseEntity<List<Filter>>(filters, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Filter> getById(@PathVariable("id") long id) {
        Filter filter = service.findById(id);
        return new ResponseEntity<Filter>(filter, HttpStatus.OK);
    }

    // Some other methods
    
}

FilterService.java文件

package br.com.uburu.spring.service;

// Imports

@Service
public class FilterService {

    @Autowired
    private FilterRepository repository;

    public List<Filter> getAll() {
        List<Filter> filters = new ArrayList<>();
        repository.findAll().forEach(filters::add);

        return filters;
    }

    public Filter findById(long id) {
        return repository.findById(id).orElse(null);
    }

    public Filter save(Filter filter) {
        return repository.save(filter);
    }

    public void delete(long id) {
        repository.deleteById(id);
    }
    
}

FilterRepository.java文件

package br.com.uburu.spring.repository;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import br.com.uburu.spring.document.Filter;

public interface FilterRepository extends ElasticsearchRepository<Filter, Long> {}

项目依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
        
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>3.0.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>5.0.5</version>
    </dependency>
</dependencies>

已尝试的解决方案

  • 移除@Autowired注解初始化service和repository变量
  • 通过构造函数注入,将@Autowired注解放在构造函数上
  • 以管理员身份运行ElasticSearch
  • 清理VSCode的Java Language Server工作区
  • 运行mvn clean install后再启动应用

ElasticSearch日志信息

启动SpringBoot应用时,ElasticSearch控制台出现以下日志:

[2023-04-23T14:57:56,085][WARN ][o.e.h.n.Netty4HttpServerTransport] [DESKTOP-LO4MIAG] received plaintext http traffic on an https channel, closing connection Netty4HttpChannel{localAddress=/127.0.0.1:9200, remoteAddress=/127.0.0.1:53007}


内容的提问来源于stack exchange,提问作者Dheovani Xavier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 22:05:26