0
我試圖用彈簧和嵌入式彈性建立休息API。嘗試啓動我的應用程序時遇到NoSuchBeanDefinitionException。帶彈簧的自動裝配嵌入式彈性
目前,我有這樣的佈線彈性DB:
@Configuration
public class EsConfig {
Node node;
@Bean
public Client es() {
node = nodeBuilder().local(true).node();
return node.client();
}
(Destructor)
}
,並在控制器:
@RestController
public class Controller {
@Autowired
public Client elasticSearchClient;
...
}
但是當我啓動它,我得到這個異常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'controller': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.elasticsearch.client.Client package.Controller.elasticSearchClient;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.elasticsearch.client.Client] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我已經嘗試了幾個不同的註釋,但我很明顯地離開了。
缺少哪個bean?你能否在你的問題中包括例外情況?不確定什麼是空的'@ Qualifier',但如果不指定限定符,則不需要它。 – zapl
它缺少Controller類中的客戶端Bean。我更新了這個問題,以包括例外情況。我之前在限定詞中有一個名字,只是忘記完全刪除它。就像我說的,我一直在嘗試很多不同的東西。 –
啊,試着讓你的配置成爲'@ Configuration',這是一個提供'@ Bean'的類,'@ Component'本身就只是一個'@ Bean'。如果你不使用命名bean的方法,請去除限定符:) – zapl