Skip to content
Latchkey

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.

java
***************************
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.

Terminal
# 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 -60

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.

Java
@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.

Java
@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.

java
@WebMvcTest(OrderController.class)
class OrderControllerTest {
  @MockBean OrderService orderService; // satisfies the slice
}

How to prevent it

  • Keep all components under the @SpringBootApplication package so scanning is automatic.
  • For external types, define @Bean factories explicitly rather than relying on scanning.
  • Run @SpringBootTest in CI so a missing bean fails fast in the pipeline, not in production.

Frequently asked questions

What causes Spring "Parameter 0 of constructor required a bean that could not be found"?
There are 3 common causes: the bean type is not a component or not scanned, a @bean factory method is missing, and a test slice excludes the bean. The dependency class lacks @Component/@Repository/@Service, or it lives in a package outside the @SpringBootApplication scan root, so it never becomes a bean.
How do I fix Spring "Parameter 0 of constructor required a bean that could not be found"?
There are 3 fixes depending on which cause you have: make the dependency a scanned bean, provide a @bean for types you do not own, and mock the bean in a test slice. Work through them in order, since the first is the most common.
What does Spring "Parameter 0 of constructor required a bean that could not be found" actually mean?
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.
How do I stop Spring "Parameter 0 of constructor required a bean that could not be found" happening again?
Keep all components under the @SpringBootApplication package so scanning is automatic. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card