๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG*************************** 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)

Solution 1โœ“ Most common cause

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 { ... }
Solution 2

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

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 { ... }
Solution 4

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 { ... }
Solution 5

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

Spring Boot 2.x

Throws NoSuchBeanDefinitionException or a generic startup failure.

Spring Boot 3.x

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.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase