๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method userController in com.devinhyderabad.config.AppConfig required a bean of type 'com.devinhyderabad.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.devinhyderabad.service.UserService' in your configuration.โก Quick Fix Works 80% of the time
Add @Service, @Component, or @Configuration to the missing class.
@Service
public class UserServiceImpl implements UserService {}๐ง Why this Happens
Tap to expand the deep technical explanation
Spring's dependency injection searched the application context for a bean of a specific type, but found zero matches. Spring Boot summarizes the exact missing bean and suggests defining it. This usually means you forgot to annotate a service class, or the class is in a package outside Spring's component scan.
The HITEC City Parking Spot Analogy:
You hired a contractor to build a house (the controller), and the contractor needs an electrician (the service). But you forgot to hire an electrician. Spring acts as the foreman, stopping construction and saying, 'I need an electrician to proceed.'
๐ How to Reproduce Confirm this is your error
Create a Spring Boot app. Create an interface `UserService`. In your `UserController`, add `@Autowired private UserService userService;`. Do not create any class that implements `UserService`. Run the app.
๐ ๏ธ Solutions (5 Ways to Fix)
Add a Stereotype Annotation
๐ Use this when you wrote the class but forgot to tell Spring to manage it.
Annotate the implementation class with @Service, @Component, or @Repository so Spring picks it up during component scanning.
@Service
public class UserServiceImpl implements UserService { ... }Create a @Bean method in a @Configuration class
๐ Use this if the class belongs to a third-party library and you cannot edit it to add @Service.
Manually instantiate the object and register it as a Spring bean inside a configuration class.
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserServiceImpl();
}
}Fix ComponentScan base packages
๐ Use this if your main Application class is in a different root package than your 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 or explicitly scan components.
@SpringBootApplication
@ComponentScan(basePackages = {"com.devinhyderabad.controllers", "com.devinhyderabad.services"})
public class Application { ... }Ensure the interface has an implementation
๐ Use this if you autowired an interface but forgot to write the class that implements it.
Spring cannot inject an interface directly; it needs a concrete class to instantiate. Create a class that implements your interface.
public class UserServiceImpl implements UserService { ... }Use @ConditionalOnMissingBean correctly
๐ Use this if you are writing auto-configurations and the bean isn't being created.
If you misconfigured @ConditionalOnProperty or @ConditionalOnMissingBean, Spring might skip creating the bean silently.
@Configuration
public class MyAutoConfig {
@Bean
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true")
public UserService userService() { ... }
}๐ Version Notes
Throws NoSuchBeanDefinitionException or a generic startup failure.
Provides a much cleaner 'Consider defining a bean...' FailureAnalyzer message.
๐ก๏ธ How to Prevent This Next Time
Always keep your main Application.java in the root package (e.g., com.devinhyderabad) so it automatically scans all sub-packages.