Spring Autowire NoSuchBeanDefinitionException

Spring Autowire NoSuchBeanDefinitionException

0 Comments

When Autowiring Spring Beans, a common error is to see a NoSuchBeanDefinitionException.

Here is an example of the NoSuchBeanDefinitionException you may see thrown by the Spring context:

NoSuchBeanDefinitionException

 Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [guru.springframework.controllers.HelloController] is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1060)

By default, Spring will Autowire beans by type. You can see in the error message the type of bean Spring is expecting to find. The problem is that the Spring Context is not finding a Java class of this type configured for use. There are a couple small gotch ya’s when using the Spring Boot application and in how Spring Boot scans for eligible Spring Beans.

Free Introduction to Spring Tutorial

Are you new to the Spring Framework? Checkout my Free Introduction to Spring Online Tutorial.

In the video below, I show you how to correct a component scan problem in Spring Boot causing a NoSuchBeanException.

About jt

    You May Also Like

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.