๐Ÿ”ด 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 constructor in com.devinhyderabad.controller.UserController 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

Ensure the required dependency is annotated with @Service/@Component and is in a scanned package.

@Service public class UserServiceImpl implements UserService {}

๐Ÿง  Why this Happens

Tap to expand the deep technical explanation

You used constructor injection for a class, but Spring could not find a matching bean in the context to pass as parameter 0 (the first argument) of the constructor. This is a specific variant of the missing bean error, pointing exactly to the constructor parameter that failed.

The HITEC City Parking Spot Analogy:

You ordered a combo meal at a restaurant, but the cashier tells you they are out of the main burger (Parameter 0). They can't give you the combo because the primary component is missing.

๐Ÿ” How to Reproduce Confirm this is your error

Create `UserController` with a constructor `public UserController(UserService service)`. Do not annotate any class with `@Service` to implement `UserService`. Start the app.

๐Ÿ› ๏ธ Solutions (5 Ways to Fix)

Solution 1โœ“ Most common cause

Implement the missing interface and annotate it

๐Ÿ‘‰ Use this when you haven't created the implementation class yet.

Create a class that implements the required interface and annotate it with @Service.

@Service public class UserServiceImpl implements UserService { ... }
Solution 2

Check for profile mismatches (@Profile)

๐Ÿ‘‰ Use this if the bean exists, but is annotated with @Profile("dev") and you are running in prod.

If a bean is profile-specific, it won't be created unless that profile is active. Ensure you set the correct active profile.

@Service @Profile("dev") // Ensure this profile is active if you need this bean public class MockUserService implements UserService { ... }
Solution 3

Use @Lazy to break initialization cycles

๐Ÿ‘‰ Use this if the bean exists, but Spring fails to inject it due to a circular dependency.

If the missing bean is actually caught in a circular dependency loop, @Lazy tells Spring to inject a proxy instead of failing immediately.

@RestController public class UserController { public UserController(@Lazy UserService userService) { ... } }
Solution 4

Ensure the bean is not excluded by @ConditionalOnProperty

๐Ÿ‘‰ Use this if you are using auto-configuration properties to toggle beans.

Check your application.properties. If the property governing the @ConditionalOnProperty is false or missing, Spring will skip creating the bean.

# application.properties app.feature.enabled=true
Solution 5

Provide an @Bean definition in @Configuration

๐Ÿ‘‰ Use this if you need to instantiate a third-party class.

Manually define the bean in a configuration class.

@Configuration public class AppConfig { @Bean public UserService userService() { return new UserServiceImpl(); } }

๐Ÿ“‹ Version Notes

Spring Boot 2.x

Standard constructor injection failure.

Spring Boot 3.x

Stricter constructor injection, fails fast if multiple constructors exist without @Autowired.

๐Ÿ›ก๏ธ How to Prevent This Next Time

Use IDE tooling to generate constructors. The IDE will warn you if an interface has no implementations.

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