CDI & Dependency Injection Basics
- Scopes (
@ApplicationScoped,@RequestScoped),@Inject, producers, and qualifiers.
1. What is CDI and how does Quarkus use it?
Section titled “1. What is CDI and how does Quarkus use it?”Answer:
CDI (Contexts and Dependency Injection) is the standard dependency injection framework in Jakarta EE. Quarkus uses ArC, its own build-time CDI implementation that processes beans at compile time — making it fast and native-image compatible.
Key difference from Spring: CDI uses scopes tied to lifecycle contexts (HTTP request, application, session), not just singletons/prototypes.
2. What are the main CDI scopes in Quarkus?
Section titled “2. What are the main CDI scopes in Quarkus?”Answer:
| Scope | Annotation | Lifecycle | When to use |
|---|---|---|---|
| Dependent | @Dependent (default) | New instance per injection point | Stateful, short-lived beans |
| Application | @ApplicationScoped | One instance per app lifetime | Stateless services, repositories |
| Request | @RequestScoped | One per HTTP request | Request-scoped state |
| Singleton | @Singleton | Same as ApplicationScoped (no proxy) | Simple singletons |
Most common: @ApplicationScoped for services, @RequestScoped for request-specific state.
3. How do you inject a bean with @Inject?
Section titled “3. How do you inject a bean with @Inject?”Answer:
Field injection (simplest):
@ApplicationScopedpublic class OrderService {
@Inject UserRepository userRepo;
@Inject EmailService emailService;
public void placeOrder(Order order) { User user = userRepo.findById(order.userId); emailService.sendConfirmation(user.email); }}Constructor injection (recommended for testability):
@ApplicationScopedpublic class OrderService { private final UserRepository userRepo; private final EmailService emailService;
@Inject public OrderService(UserRepository userRepo, EmailService emailService) { this.userRepo = userRepo; this.emailService = emailService; }}4. What is a CDI producer (@Produces)?
Section titled “4. What is a CDI producer (@Produces)?”Answer:
A @Produces method creates a bean from code you control (e.g., third-party classes or conditional logic):
@ApplicationScopedpublic class ConfigProducer {
@Produces @ApplicationScoped public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return mapper; }}
// Inject it anywhere:@InjectObjectMapper mapper;5. What are qualifiers and when do you need them?
Section titled “5. What are qualifiers and when do you need them?”Answer:
Qualifiers distinguish between multiple beans of the same type:
// Define qualifier@Qualifier@Retention(RUNTIME)@Target({FIELD, METHOD, TYPE})public @interface Premium {}
// Two implementations@ApplicationScopedpublic class BasicEmailService implements EmailService { ... }
@ApplicationScoped@Premiumpublic class PremiumEmailService implements EmailService { ... }
// Inject specific one@Inject@PremiumEmailService emailService;Without a qualifier, CDI would fail with an “ambiguous dependencies” error if two beans satisfy the same type.
6. What is @Observes and how do you listen to CDI events?
Section titled “6. What is @Observes and how do you listen to CDI events?”Answer:
CDI events allow decoupled communication between beans:
// Fire an event@InjectEvent<UserCreatedEvent> userCreatedEvent;
public void createUser(User user) { // ... save user userCreatedEvent.fire(new UserCreatedEvent(user));}
// Listen to itpublic void onUserCreated(@Observes UserCreatedEvent event) { emailService.sendWelcome(event.getUser().email);}Quarkus also supports startup/shutdown events:
void onStart(@Observes StartupEvent event) { log.info("Application started");}
void onStop(@Observes ShutdownEvent event) { log.info("Application stopping");}Summary Table
Section titled “Summary Table”| Concept | Key Point |
|---|---|
@ApplicationScoped | One shared instance per app |
@RequestScoped | New instance per HTTP request |
@Inject | Injects a managed CDI bean |
@Produces | Creates beans from custom factory methods |
| Qualifier | Disambiguates multiple beans of same type |
@Observes | Listens to CDI events (decoupled communication) |
| ArC | Quarkus’s build-time CDI — no runtime reflection |