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();
}
}