๐ด 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 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)
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 { ... }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 { ... }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) { ... }
}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=trueProvide 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
Standard constructor injection failure.
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.