2015-11-26 67 views
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)} 

我已經嘗試了幾個不同的註釋,但我很明顯地離開了。

+1

缺少哪個bean?你能否在你的問題中包括例外情況?不確定什麼是空的'@ Qualifier',但如果不指定限定符,則不需要它。 – zapl

+0

它缺少Controller類中的客戶端Bean。我更新了這個問題,以包括例外情況。我之前在限定詞中有一個名字,只是忘記完全刪除它。就像我說的,我一直在嘗試很多不同的東西。 –

+1

啊,試着讓你的配置成爲'@ Configuration',這是一個提供'@ Bean'的類,'@ Component'本身就只是一個'@ Bean'。如果你不使用命名bean的方法,請去除限定符:) – zapl

回答

1

No qualifying bean of type [some.Thing]表示彈簧不知道適用於此接口的類。

原因,可以是

  • 具有@Bean方法的類不是@Configuration
  • @Configuration類不是由類路徑分量掃描器拾取。

默認情況下,Spring引導只會掃描@SpringBootApplication的子包層次結構。如果您想要在其外包含代碼,您可以通過@ComponentScan註釋更改掃描行爲。

@SpringBootApplication 
@ComponentScan(basePackageClasses = {MyApp.class, SomeOtherClassInARootPackage.class}) 
public class MyApp { 
... 

會添加一些其他類的包(和子包),同時保持應用程序的包也被掃描。