0

Started learning couple days ago Spring Boot by following online tutorial and I stuck because I getting error while following tutorial step by step having no idea what I did wrong there.

While compiling i get following error:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'pl.springlearning.javastartspring.beans.MessagePrinter' available

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class MessageProducer {

    private int number;

    public MessageProducer() {
        number = new Random().nextInt();
    }

    public int getNumber() {
        return number;
    }
}
@Component
public class MessagePrinter {

    @Autowired
    private MessageProducer producer;

    public void printMessage() {
        System.out.println(producer.getNumber());
    }
}
@Configuration
@ComponentScan
public class SpringDiApplication {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringDiApplication.class);

        MessagePrinter bean1 = ctx.getBean(MessagePrinter.class);
        bean1.printMessage();
        MessagePrinter bean2 = ctx.getBean(MessagePrinter.class);
        bean2.printMessage();

        ctx.close();
    }
}
lobsang
  • 11
  • 1
  • Can you add the package definition for each class, because you have added the ComponentScan in the class `SpringDiApplication` which will be considered the root for component scan, hence edit the post with a package declaration also – Prasanth Rajendran Aug 27 '20 at 19:25
  • @PrasanthRajendran After your comment while I was updating topic I pulled SpringDiApplication out of package and all worked.. so problem solved! – lobsang Aug 27 '20 at 19:32
  • Good to know that problem is resolved – Prasanth Rajendran Aug 27 '20 at 19:36
  • 1
    Does this answer your question? [What is a NoSuchBeanDefinitionException and how do I fix it?](https://stackoverflow.com/questions/39173982/what-is-a-nosuchbeandefinitionexception-and-how-do-i-fix-it) – Savior Aug 27 '20 at 19:38

0 Answers0