org.springframework.beans.factory.UnsatisfiedDependencyException Error creating bean with name 'demoRestController'
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
UnsatisfiedDependencyException is thrown when Spring cannot inject a required dependency into a bean. The error Error creating bean with name 'demoRestController': Unsatisfied dependency expressed through field 'service' typically means the injected service class is missing a @Service or @Component annotation, is not in a package scanned by @ComponentScan, or has its own unsatisfied dependencies (cascading failure). The fix involves checking annotations, package scanning, constructor parameters, and the full stack trace to find the root bean that failed.
The Error Message
This tells you: Spring tried to create demoRestController, found it needs a DemoService, but could not find a bean of that type in the application context.
Cause 1: Missing @Service or @Component Annotation
Spring only manages classes annotated with @Component, @Service, @Repository, @Controller, or @Configuration. Without these, the class is invisible to dependency injection.
Cause 2: Wrong Package — Not Scanned
@SpringBootApplication includes @ComponentScan which scans the package of the main class and all subpackages. Classes in sibling or parent packages are not discovered.
Cause 3: Interface Without Implementation
When injecting by interface type, the concrete implementation class must be annotated. Spring resolves the interface to its annotated implementation.
Cause 4: Cascading Dependency Failure
Read the full stack trace — the root cause is at the bottom. The controller fails because the service fails because the repository is not a bean.
Cause 5: Missing Dependency or Configuration
JPA repositories need spring-boot-starter-data-jpa and a configured datasource. Missing either causes the repository bean creation to fail, cascading up to the controller.
Cause 6: Constructor Injection Issues
Constructor injection produces clearer error messages than field injection. The error explicitly states which constructor parameter could not be resolved.
Debugging Checklist
Common Pitfalls
- Annotating the interface instead of the implementation:
@Serviceon an interface does nothing if there is no concrete implementation also annotated. Spring needs an instantiable class to create a bean. Put@Serviceon the implementation class, not the interface. - Service class in a package outside component scan:
@SpringBootApplicationscans its own package and subpackages. A service incom.other.serviceis invisible if the main class is incom.example.app. Add@ComponentScan(basePackages = {...})or restructure packages. - Multiple beans of the same type without
@Primaryor@Qualifier: If two classes implement the same interface and both are annotated, Spring throwsNoUniqueBeanDefinitionException. Use@Primaryon the preferred bean or@Qualifier("beanName")at the injection point. - Not reading the full stack trace: The
UnsatisfiedDependencyExceptionon the controller is often caused by a deeper failure in a service or repository. The root cause (at the bottom of the stack trace) reveals the actual missing bean or configuration error. - Missing Spring Boot starter dependency: JPA repositories need
spring-boot-starter-data-jpa, web controllers needspring-boot-starter-web, and security needsspring-boot-starter-security. A missing starter means Spring cannot create the infrastructure beans your code depends on.
Summary
UnsatisfiedDependencyExceptionmeans Spring cannot find or create a required bean- Check that the dependency class has
@Service,@Component, or@Repository - Ensure the class is in a package scanned by
@ComponentScan(subpackage of the main class) - Read the full stack trace — the root cause is at the bottom of the exception chain
- Use constructor injection for clearer error messages and easier testing
- Add
@Primaryor@Qualifierwhen multiple beans of the same type exist
Related reading
- Overriding an Autowired Bean in Unit Tests
- Overriding superclass property with different type in Swift
- Parameter 0 of constructor in ..... Spring Boot
- Parameter 0 of constructor in required a bean of type 'java.lang.String' that could not be found
- org.springframework.boot.loader.JarLauncher cannot be found, but org.springframework.boot.loader.launch.JarLauncher can
- org.springframework.context.ApplicationContextException Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry
- Pass An Instantiated System.Type as a Type Parameter for a Generic Class
- Passing arguments to C generic new of templated type

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.