2017-08-08 73 views
1

在我的春天啓動的應用程序,我有一個服務於txt文件,如下寫:NoSuchBeanDefinitionException:無型的排位豆可用

@Service 
public class LinksWriterService { 

    public LinksWriterService(){ 
     //.. 
    } 

    public void writeToFile(String text) { 
     //.. 
    } 
} 

,然後在我的主要方法我已經下文將其稱爲:

public static void main(String[] args) { 

     ConfigurableApplicationContext context = SpringApplication.run(SeleniumApplication.class, args); 
     LinksWriterService writer = context.getBean(LinksWriterService.class); 

     writer.writeToFile("Salmaaaaaaaaaaaaaaaan"); 
    } 

但在最後一行它抱怨:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.service.LinksWriterService' available 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) 
    at com.example.demo.SeleniumApplication.main(SeleniumApplication.java:21) 
+1

嘗試清理和重新編譯您的項目。 – Berger

+0

這個問題與寫入文件無關 – eis

+0

我希望你的'LinksWriterService' bean沒有被組件掃描。檢查它所在的包是否位於主應用程序類聲明的組件掃描範圍內。 –

回答

0

在你SeleniumApplication類的類,添加一個掃描註釋,並把相關的包名稱:

@ComponentScan({ "package.where.LinksWriterService.is.located" }) 
-1

使用@SpringBootApplication與主要方法

相關問題