一、现象描述
在spring boot项目中引入 bboss es starter相关依赖之后,跑测试类,发生报错:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.frameworkset.elasticsearch.boot.BBossESStarter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1824)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1383)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:773)
... 76 common frames omitted
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.dromara.test.ElasticDsRecordInfoTest': Unsatisfied dependency expressed through field 'bBossESStarter': No qualifying bean of type 'org.frameworkset.elasticsearch.boot.BBossESStarter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这个错误org.springframework.beans.factory.NoSuchBeanDefinitionException表示Spring框架在尝试自动装配(autowire)一个类型为org.frameworkset.elasticsearch.boot.BBossESStarter的bean时失败了,因为它没有在Spring的应用上下文中找到一个合格的bean候选者。说白了,就是BBossESStarter没有被注入进来
试着启动一下springboot的main方法,看看控制台是否有es相关的初始化信息,发现完全没有加载es信息
二、排查配置
1、检查maven配置,看了未发现什么问题
dependency>
groupId>com.bbossgroups.pluginsgroupId>
artifactId>bboss-elasticsearch-spring-boot-starterartifactId>
version>7.1.3version>
exclusions>
exclusion>
artifactId>slf4j-log4j12artifactId>
groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
2、yml文件配置es连接参数信息,也都配置了,没有遗漏的
spring:
elasticsearch:
bboss:
elasticUser: elastic
elasticPassword: password123
elasticsearch:
rest:
hostNames: 192.168.8.25:9200,192.168.8.26:9200,192.168.8.27:9200 ##集群地址配置
dateFormat: yyyy.MM.dd
timeZone: Asia/Shanghai
showTemplate: true
discoverHost: false
dslfile:
refreshInterval: -1
http:
timeoutConnection: 5000
timeoutSocket: 5000
connectionRequestTimeout: 5000
retryTime: 1
maxLineLength: -1
maxHeaderCount: 200
maxTotal: 400
defaultMaxPerRoute: 200
soReuseAddress: false
soKeepAlive: false
timeToLive: 3600000
keepAlive: 3600000
keystore:
keyPassword:
hostnameVerifier:
三、顺藤摸瓜
刚刚报错是通过测试类发现的,我们就通过测试类来排查,测试类代码如下:
package org.dromara.test;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class ElasticDsRecordInfoTest {
@Autowired
private BBossESStarter bBossESStarter;
@Test
public void testsql() throws InterruptedException {
ClientInterface restClient = bBossESStarter.getRestClient();
System.out.println(restClient);
String result = restClient.executeHttp("/_stats",ClientInterface.HTTP_GET);
System.out.println(result);
}
}
打断点进入到BBossESStarter 类中,看底层源码
启动测试类,发现根本都没有进入start方法里面,所以yml文件配置的es参数信息,根本没有起作用。
四、解决办法
查询了大量的资料,最终在官网找到了答案,是因为springboot和bboss的版本问题导致,具体版本对应如下:
- spring boot 1.x,2.x依赖包
dependency>
groupId>com.bbossgroups.pluginsgroupId>
artifactId>bboss-elasticsearch-spring-boot-starterartifactId>
version>7.1.7version>
dependency>
- spring boot 3.x依赖包
dependency>
groupId>com.bbossgroups.pluginsgroupId>
artifactId>bboss-elasticsearch-spring-boot3-starterartifactId>
version>7.1.7version>
dependency>
而本文报错的springboot项目版本是 3.1.7
maven依赖的bboss版本是7.1.3
所以,按照官网,应该把maven依赖修改为
dependency>
groupId>com.bbossgroups.pluginsgroupId>
artifactId>bboss-elasticsearch-spring-boot3-starterartifactId>
version>7.1.7version>
dependency>
五、运行测试
修改完maven依赖之后,我们启动一下springboot的main方法,看一下控制台是否有es相关的初始化信息
现在发现有es连接成功信息了,说明生效了。问题解决
六、总结
spring注入BBossESStarter失败,是由于springboot和bboss的版本不一致造成,具体版本对照如下
- spring boot 1.x,2.x依赖包
dependency>
groupId>com.bbossgroups.pluginsgroupId>
artifactId>bboss-elasticsearch-spring-boot-starterartifactId>
version>7.1.7version>
dependency>
- spring boot 3.x依赖包
dependency>
groupId>com.bbossgroups.pluginsgroupId>
artifactId>bboss-elasticsearch-spring-boot3-starterartifactId>
version>7.1.7version>
dependency>