NoSuchBeanDefinitionException:没有可用类型的限定bean

时间:2017-08-08 14:56:09

标签: spring-boot

在我的春季启动应用程序中,我有一个服务可以写在txt文件上,如下所示:

@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)

2 个答案:

答案 0 :(得分:0)

echo "$1" | sed ...课程中,添加扫描注释并输入相关的包名称:

SeleniumApplication

答案 1 :(得分:-1)

使用主方法

在类中使用@SpringBootApplication