Spring Boot项目无ElasticSearch依赖却尝试连接该数据库的问题
问题:未引入ElasticSearch依赖的Spring Boot项目启动时出现ES连接拒绝错误
我的Spring Boot项目仅配置了MySQL依赖与连接属性,完全未引入任何ElasticSearch相关依赖、插件及配置,但启动时控制台反复抛出ElasticSearch连接拒绝的报错。已重装IntelliJ IDEA,问题依旧。
控制台错误日志
2022-09-01 14:24:29 INFO o.s.d.r.c.RepositoryConfigurationExtensionSupport - Spring Data Elasticsearch - Could not safely identify store assignment for repository candidate interface it.ProjectMiC.readformatopacchetto.repository.DepositatoreRepository. If you want this repository to be a Elasticsearch repository, consider annotating your entities with one of these annotations: org.springframework.data.elasticsearch.annotations.Document (preferred), or consider extending one of the following types with your repository: org.springframework.data.elasticsearch.repository.ElasticsearchRepository, org.springframework.data.elasticsearch.repository.ElasticsearchRepository. 2022-09-01 14:24:29 INFO o.s.d.r.c.RepositoryConfigurationExtensionSupport - Spring Data Elasticsearch - Could not safely identify store assignment for repository candidate interface it.ProjectMiC.readformatopacchetto.repository.FormatoValidazionePacchettoRepository. If you want this repository to be a Elasticsearch repository, consider annotating your entities with one of these annotations: org.springframework.data.elasticsearch.annotations.Document (preferred), or consider extending one of the following types with your repository: org.springframework.data.elasticsearch.repository.ElasticsearchRepository, org.springframework.data.elasticsearch.repository.ElasticsearchRepository. 2022-09-01 14:24:29 INFO o.s.d.r.c.RepositoryConfigurationExtensionSupport - Spring Data Elasticsearch - Could not safely identify store assignment for repository candidate interface it.ProjectMiC.readformatopacchetto.repository.TipoFormatoRepository. If you want this repository to be a Elasticsearch repository, consider annotating your entities with one of these annotations: org.springframework.data.elasticsearch.annotations.Document (preferred), or consider extending one of the following types with your repository: org.springframework.data.elasticsearch.repository.ElasticsearchRepository, org.springframework.data.elasticsearch.repository.ElasticsearchRepository. 2022-09-01 14:24:29 INFO o.s.d.r.c.RepositoryConfigurationExtensionSupport - Spring Data Elasticsearch - Could not safely identify store assignment for repository candidate interface it.ProjectMiC.readformatopacchetto.repository.ValidazioneRepository. If you want this repository to be a Elasticsearch repository, consider annotating your entities with one of these annotations: org.springframework.data.elasticsearch.annotations.Document (preferred), or consider extending one of the following types with your repository: org.springframework.data.elasticsearch.repository.ElasticsearchRepository, org.springframework.data.elasticsearch.repository.ElasticsearchRepository. 2022-09-01 14:24:29 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 62 ms. Found 0 Elasticsearch repository interfaces. 2022-09-01 14:24:29 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode! 2022-09-01 15:40:33 ERROR o.elasticsearch.client.sniff.Sniffer - error while sniffing nodes java.net.ConnectException: Connection refused: no further information at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:918) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:299) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:287) at org.elasticsearch.client.sniff.ElasticsearchNodesSniffer.sniff(ElasticsearchNodesSniffer.java:106) at org.elasticsearch.client.sniff.Sniffer.sniff(Sniffer.java:209) at org.elasticsearch.client.sniff.Sniffer$Task.run(Sniffer.java:140) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: java.net.ConnectException: Connection refused: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946) at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvent(DefaultConnectingIOReactor.java:174) at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:148) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:351) at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:221) at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64) ... 1 common frames omitted
项目pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>it.Project-MiC</groupId> <artifactId>readformatopacchetto</artifactId> <version>0.0.1</version> <name>readformatopacchetto</name> <description>Spring Boot Project for readformatopacchetto Application</description> <properties> <java.version>1.8</java.version> <org.mapstruct.version>1.5.2.Final</org.mapstruct.version> <projectlombok.version>1.18.22</projectlombok.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.16.2</version> </dependency> <dependency> <groupId>com.codahale.metrics</groupId> <artifactId>metrics-core</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.6.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>4.7.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>9</source> <!-- depending on your project --> <target>9</target> <!-- depending on your project --> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${projectlombok.version}</version> </path> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>0.2.0</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> </project>
解决方案
原因分析
问题根源在于:
- Spring Data自动扫描Repository时,因未明确指定Repository类型,会尝试匹配所有Spring Data模块,触发了ElasticSearch相关的检测逻辑。
- 可能存在间接依赖(如Actuator或其他组件)引入了ElasticSearch客户端相关jar包,导致Spring Boot自动初始化ES连接。
具体解决步骤
- 排除ElasticSearch自动配置类
在Spring Boot启动类上添加排除配置,禁止ES相关自动配置加载:
@SpringBootApplication(exclude = { ElasticsearchDataAutoConfiguration.class, ElasticsearchRestClientAutoConfiguration.class }) public class ReadformatopacchettoApplication { public static void main(String[] args) { SpringApplication.run(ReadformatopacchettoApplication.class, args); } }
- 明确指定JPA Repository扫描范围
通过@EnableJpaRepositories指定JPA Repository所在包,让Spring Data仅扫描JPA相关的Repository:
@SpringBootApplication @EnableJpaRepositories(basePackages = "it.ProjectMiC.readformatopacchetto.repository") public class ReadformatopacchettoApplication { public static void main(String[] args) { SpringApplication.run(ReadformatopacchettoApplication.class, args); } }
- 检查并排除间接依赖
执行mvn dependency:tree命令查看依赖树,若发现有间接引入的ElasticSearch模块,在对应依赖中添加排除项:
<dependency> <!-- 替换为实际引入ES的依赖 --> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </exclusion> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </exclusion> </exclusions> </dependency>
内容的提问来源于stack exchange,提问作者Java
相关产品推荐
相关产品推荐

