Quarkus ArC Dependency Injection Interview Questions and Answers
Master Quarkus ArC Dependency Injection with interview questions covering CDI, ArC container, bean scopes, qualifiers, producers, lifecycle, interceptors, and enterprise best practices.
Quarkus ArC Dependency Injection Interview Questions and Answers
Introduction
ArC is Quarkus' implementation of CDI (Contexts and Dependency Injection). It provides a lightweight, build-time optimized dependency injection container that manages application beans efficiently.
Unlike traditional CDI implementations that perform significant runtime processing, ArC analyzes beans during build time, removes unused beans, and generates optimized metadata. This results in faster startup, lower memory usage, and excellent performance for cloud-native applications.
ArC is one of the core components that makes Quarkus suitable for Kubernetes, Docker, and GraalVM Native Image deployments.
ArC Architecture
flowchart LR
Developer --> BeanClasses
BeanClasses --> BuildTimeAnalysis
BuildTimeAnalysis --> ArCContainer
ArCContainer --> InjectDependencies
InjectDependencies --> Application
Q1. What is ArC?
Answer
ArC is the CDI container used by Quarkus.
It manages:
- Bean creation
- Dependency Injection
- Bean lifecycle
- Context management
- Interceptors
- Events
Unlike traditional CDI implementations, ArC performs most processing during build time.
Benefits
- Fast startup
- Low memory usage
- Build-time optimization
- GraalVM compatibility
Q2. What is Dependency Injection?
Dependency Injection allows the framework to create and inject required objects automatically.
Without DI
public class PaymentService {
private PaymentRepository repository =
new PaymentRepository();
}
With ArC
@ApplicationScoped
public class PaymentService {
@Inject
PaymentRepository repository;
}
Dependency Flow
flowchart TD
Application --> ArC
ArC --> PaymentService
ArC --> PaymentRepository
PaymentService --> PaymentRepository
Q3. What Bean Scopes are supported?
| Scope | Description |
|---|---|
@ApplicationScoped |
One instance for the application |
@Singleton |
Singleton bean |
@RequestScoped |
One bean per request |
@Dependent |
Default dependent scope |
@SessionScoped |
One bean per user session |
Example
@ApplicationScoped
public class CustomerService {
}
ApplicationScoped is the most commonly used scope.
Q4. What is @Inject?
@Inject requests the ArC container to inject a managed bean.
Example
@Inject
EmailService emailService;
Constructor Injection
@ApplicationScoped
public class UserService {
private final UserRepository repository;
@Inject
public UserService(UserRepository repository) {
this.repository = repository;
}
}
Constructor injection is the recommended approach.
Q5. What are Qualifiers?
Qualifiers distinguish multiple implementations of the same interface.
Example
@ApplicationScoped
@Named("email")
public class EmailService
implements NotificationService {
}
@ApplicationScoped
@Named("sms")
public class SmsService
implements NotificationService {
}
Injection
@Inject
@Named("email")
NotificationService service;
Q6. What are Producer Methods?
Producer methods create beans that cannot be instantiated directly.
Example
@ApplicationScoped
public class DataSourceProducer {
@Produces
DataSource createDataSource() {
return new HikariDataSource();
}
}
Use cases
- Database connections
- Third-party libraries
- External SDKs
- Custom object creation
Q7. What is Bean Lifecycle?
ArC manages bean creation and destruction.
Lifecycle
sequenceDiagram
Application->>ArC: Startup
ArC->>Bean: Create
ArC->>Bean: Inject Dependencies
Bean-->>Application: Ready
Application->>Bean: Use
Application->>ArC: Shutdown
ArC->>Bean: Destroy
Lifecycle annotations
@PostConstruct
@PreDestroy
Q8. What are Interceptors?
Interceptors execute additional logic before or after business methods.
Examples
- Logging
- Security
- Transactions
- Metrics
- Auditing
Example
@Transactional
public void transfer() {
}
The transaction interceptor begins and commits the transaction automatically.
Q9. What are CDI Events?
CDI Events allow beans to communicate without direct dependencies.
Producer
@Inject
Event<OrderCreatedEvent> event;
Fire Event
event.fire(new OrderCreatedEvent());
Observer
public void process(
@Observes
OrderCreatedEvent event){
}
Event Flow
flowchart LR
OrderService --> CDIEvent
CDIEvent --> NotificationService
CDIEvent --> AuditService
CDIEvent --> Analytics
Q10. ArC Best Practices
Prefer Constructor Injection
Improves immutability and testability.
Keep Beans Stateless
Avoid storing request-specific data in application-scoped beans.
Minimize Bean Creation
Remove unnecessary beans.
Use Producer Methods
For external libraries and infrastructure objects.
Banking Example
flowchart TD
TransferController --> TransferService
TransferService --> FraudService
TransferService --> NotificationService
TransferService --> AuditService
TransferService --> AccountRepository
AccountRepository --> Database
All components are managed by the ArC container.
Common Interview Questions
- What is ArC?
- What is CDI?
- Explain Dependency Injection.
- What is
@Inject? - Explain Bean Scopes.
- What are Qualifiers?
- What are Producer Methods?
- Explain Bean Lifecycle.
- What are CDI Events?
- ArC vs Spring Dependency Injection?
Quick Revision
| Topic | Summary |
|---|---|
| ArC | Quarkus CDI container |
| CDI | Contexts and Dependency Injection |
| @Inject | Dependency injection |
| @ApplicationScoped | Application-wide bean |
| @Singleton | Singleton bean |
| Producer | Creates custom beans |
| Qualifier | Select bean implementation |
| Interceptor | Cross-cutting logic |
| CDI Event | Event-based communication |
| Build-Time Analysis | Faster startup |
Dependency Injection Lifecycle
sequenceDiagram
Application->>ArC: Startup
ArC->>Build Metadata: Load Bean Definitions
Build Metadata-->>ArC: Optimized Beans
ArC->>Service: Inject Dependencies
Service->>Repository: Use Bean
Repository-->>Service: Data
Service-->>Application: Response
Key Takeaways
- ArC is Quarkus' implementation of CDI (Contexts and Dependency Injection).
- ArC performs build-time bean analysis, reducing runtime overhead and improving startup performance.
- Dependency Injection promotes loose coupling, testability, and maintainability.
- Common bean scopes include
@ApplicationScoped,@Singleton,@RequestScoped, and@Dependent. - Constructor injection is the preferred approach for mandatory dependencies.
- Qualifiers resolve ambiguity when multiple implementations of an interface exist.
- Producer methods create beans for third-party libraries and infrastructure components.
- Interceptors handle cross-cutting concerns such as transactions, logging, and security.
- CDI Events enable loosely coupled communication between application components.
- ArC is optimized for cloud-native, containerized, and GraalVM Native Image applications through compile-time processing.