๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-10-25 10:15:30.812 ERROR 12345 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.devinhyderabad.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.SimpleConstructorResolver.instantiate(SimpleConstructorResolver.java:172)โก Quick Fix Works 80% of the time
Add @Service or @Component to the class you are trying to inject.
@Service
public class UserServiceImpl implements UserService { ... }๐ ๏ธ Solutions (3 Ways to Fix)
Add a stereotype annotation to the class
๐ Use this when you wrote the class yourself but forgot to tell Spring to manage it.
Annotate the class with @Service, @Component, or @Repository so Spring picks it up during component scanning.
@Service
public class UserServiceImpl implements UserService { ... }Fix the ComponentScan base package
๐ Use this if your main Application.java is in a different root package than your controllers/services.
By default, Spring only scans the package of the main class and below. If your services are in a sibling package, they won't be found. Move the main class to the root package or explicitly scan components.
@SpringBootApplication
@ComponentScan(basePackages = "com.devinhyderabad")
public class Application { ... }Define a @Bean method in a @Configuration class
๐ Use this if the class belongs to a third-party library (you can't edit it to add @Service).
Create a configuration class that manually instantiates the object and registers it as a Spring bean.
@Configuration
public class AppConfig {
@Bean
public ThirdPartyService thirdPartyService() {
return new ThirdPartyService();
}
}๐ Version Notes
Standard behavior. Throws NoSuchBeanDefinitionException.
Identical behavior, but uses Jakarta EE namespaces internally.
๐ง Why this Happens
Tap to expand the deep technical explanation
You used @Autowired to inject a class, but Spring's component scanner didn't find any class annotated with @Service, @Component, or @Repository that matches that type. Spring cannot inject an object it doesn't know exists.
The HITEC City Parking Spot Analogy:
Imagine you ordered a Biryani at a restaurant, but the kitchen doesn't have any chefs who know how to cook it. The waiter can't bring you what doesn't exist.
๐ก๏ธ How to Prevent This Next Time
Keep your main Application.java in the root package (e.g., com.devinhyderabad) so it automatically scans all sub-packages.