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.Diagnose it: resolve the effective POM first
Maven merges parent POMs, profiles, and settings before it builds anything. The configuration causing your failure is frequently inherited or activated by a profile that is on locally and off in CI.
# the fully resolved configuration Maven will actually use
mvn help:effective-pom | head -60
# which profiles are active here vs on your machine?
mvn help:active-profiles
# full error, offline-safe, no colour codes to confuse the log
mvn -B -e -X <goal> 2>&1 | tail -60Common 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.