Spring "Parameter 0 of constructor required a bean that could not be found" - Fix in CI
Spring tried to inject a constructor parameter and found no bean of the required type in the context. The class is asking for a collaborator the container was never told how to build.
What this error means
Context startup fails with Parameter 0 of constructor in com.example.Service required a bean of type 'com.example.Repo' that could not be found. Spring usually appends an "Action" suggesting you define such a bean. The app or @SpringBootTest aborts before any request is served.
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.OrderService required a bean of
type 'com.example.OrderRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.OrderRepository' in your
configuration.Common causes
The bean type is not a component or not scanned
The dependency class lacks @Component/@Repository/@Service, or it lives in a package outside the @SpringBootApplication scan root, so it never becomes a bean.
A @Bean factory method is missing
For third-party types you do not own (a client, a template), there is no @Bean method producing one, so the container has no candidate.
A test slice excludes the bean
A @WebMvcTest/@DataJpaTest slice only loads part of the context. A collaborator outside that slice is absent unless mocked or imported.
How to fix it
Make the dependency a scanned bean
Annotate the implementation and ensure it is under the application package so component scanning finds it.
@Repository // now a bean; package must be under the @SpringBootApplication root
public class OrderRepositoryImpl implements OrderRepository { /* ... */ }Provide a @Bean for types you do not own
Declare a factory method in a @Configuration class for any collaborator you cannot annotate.
@Configuration
public class Clients {
@Bean
OrderRepository orderRepository(DataSource ds) {
return new JdbcOrderRepository(ds);
}
}Mock the bean in a test slice
If the failure is only in a sliced test, supply the missing collaborator as a mock rather than loading the full context.
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@MockBean OrderService orderService; // satisfies the slice
}How to prevent it
- Keep all components under the
@SpringBootApplicationpackage so scanning is automatic. - For external types, define
@Beanfactories explicitly rather than relying on scanning. - Run
@SpringBootTestin CI so a missing bean fails fast in the pipeline, not in production.