Spring Boot: What to do when Autowired stops working

That’s weird! Suddenly, you try to inject a dependency in your Spring Boot application, but it’s like @Autowired stopped working.

You will see this in several ways. IntelliJ will provide a warning from the Spring plugin, which says “Could not autowire. No beans of ‘YourBeanName’ type found.”

At runtime, Spring itself will fail with a message like:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.scottshipp.www.YourBeanName' available: expected at least 1 bean which qualifies as autowire candidate.

If this behavior looks familiar, please read on.

The Solution
Most likely what is happening is pretty simple. Somewhere you’ve defined a class with the @SpringBootApplication annotation and a main method. Which package is this class in? If it is not in the base package above all the components, services, repositories, etc. that you want to autowire, then you will get the dreaded “No qualifying bean of type…” error. Guaranteed!

The simple solution is simply to move that class to the base package, and all should be right with the world again. Hope its helpful.

Leave a Reply

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