Fix Quarkus CDI Injection Not Working
In this tutorial, you'll learn about Fix Quarkus CDI Injection Not Working. We cover key concepts, practical examples, and best practices.
The Problem
Quarkus CDI injection fails with UnsatisfiedResolutionException. Beans are not found.
Quick Fix
Add @ApplicationScoped
Wrong:
@Inject ProductService service;
// ProductService not scoped
Output:
UnsatisfiedResolutionException
Right:
@ApplicationScoped
public class ProductService { }
Output:
Bean registered
Use @Produces for factories
Wrong:
// No @Produces
public class Config {
public DataSource ds() { }
}
Output:
Bean not created
Right:
@ApplicationScoped
public class Config {
@Produces
public DataSource dataSource() { }
}
Output:
Bean created
Use @Dependent for narrow scopes
Wrong:
@Singleton
public class HttpClient { } // Held longer than needed
Output:
Memory leak
Right:
@Dependent
public class HttpClient { } // Created per injection point
Output:
Proper scope
Prevention
- Annotate beans with CDI scopes (@ApplicationScoped, @RequestScoped, @Dependent)
- Use @Produces for factory methods
- Choose proper scope based on lifecycle
Common Mistakes with cdi injection
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world QUARKUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro