Micronaut Dependency Injection Interview Questions and Answers
Master Micronaut Dependency Injection with interview questions covering IoC, compile-time DI, bean scopes, constructor injection, qualifiers, factories, lifecycle, and production best practices.
Micronaut Dependency Injection Interview Questions and Answers
Introduction
Dependency Injection (DI) is one of the core features of Micronaut. Instead of creating objects manually using the new keyword, Micronaut automatically creates, manages, and injects dependencies through its Inversion of Control (IoC) Container.
Unlike Spring Boot, Micronaut performs dependency injection at compile time, eliminating runtime reflection and significantly improving startup performance and memory usage.
Dependency Injection Architecture
flowchart LR
Application --> IoCContainer
IoCContainer --> BeanDefinitions
BeanDefinitions --> Service
Service --> Repository
Repository --> Database
Q1. What is Dependency Injection (DI)?
Answer
Dependency Injection is a design pattern where the framework provides required objects instead of the application creating them manually.
Without DI
public class UserService {
private UserRepository repository =
new UserRepository();
}
With DI
@Singleton
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
}
Benefits
- Loose coupling
- Easy testing
- Better maintainability
- Easier dependency management
- Improved scalability
Q2. What is IoC (Inversion of Control)?
Answer
IoC means the framework controls object creation instead of the application.
Traditional Approach
Application
↓
new UserService()
↓
new Repository()
IoC
Application
↓
Micronaut IoC Container
↓
Creates Beans
↓
Injects Dependencies
Diagram
flowchart TD
Application --> MicronautContainer
MicronautContainer --> UserService
MicronautContainer --> UserRepository
UserService --> UserRepository
Q3. How is Micronaut DI different from Spring DI?
| Micronaut | Spring Boot |
|---|---|
| Compile-Time DI | Runtime DI |
| No Reflection | Reflection |
| Faster Startup | Slower Startup |
| Lower Memory | Higher Memory |
| GraalVM Friendly | Spring AOT Required |
Micronaut generates bean metadata during compilation.
Q4. What are Beans in Micronaut?
Beans are objects managed by the Micronaut IoC container.
Common Bean Annotations
| Annotation | Purpose |
|---|---|
@Singleton |
One instance |
@Prototype |
New instance every request |
@Factory |
Creates custom beans |
@Primary |
Default implementation |
@Bean |
Registers external object |
Example
@Singleton
public class NotificationService {
}
Q5. Why is Constructor Injection recommended?
Constructor injection makes dependencies mandatory.
Example
@Singleton
public class OrderService {
private final PaymentService paymentService;
public OrderService(
PaymentService paymentService) {
this.paymentService = paymentService;
}
}
Advantages
- Immutable objects
- Easier unit testing
- No NullPointerException
- Mandatory dependencies
Q6. What are Bean Scopes?
Micronaut supports multiple bean scopes.
| Scope | Description |
|---|---|
| Singleton | One instance |
| Prototype | New object every injection |
| Context | Created during startup |
| Infrastructure | Internal framework beans |
Example
@Prototype
public class Employee {
}
Lifecycle
flowchart LR
Application --> Singleton
Application --> Prototype
Singleton --> Reused
Prototype --> NewObject
Q7. What is @Factory?
Factories create beans manually.
Example
@Factory
public class DatabaseFactory {
@Singleton
DataSource dataSource() {
return new HikariDataSource();
}
}
Useful for
- Database connections
- Third-party libraries
- External SDKs
Q8. What are Qualifiers?
When multiple beans implement the same interface, qualifiers identify which implementation should be injected.
Example
@Singleton
@Named("email")
class EmailService
implements NotificationService {
}
@Singleton
@Named("sms")
class SmsService
implements NotificationService {
}
Injection
public UserService(
@Named("email")
NotificationService service) {
}
Q9. How does Bean Lifecycle work?
Micronaut creates beans during startup.
Lifecycle
sequenceDiagram
Application->>Container: Start
Container->>Bean: Create
Container->>Bean: Inject Dependencies
Bean-->>Application: Ready
Application->>Bean: Use Bean
Application->>Container: Shutdown
Bean lifecycle annotations
@PostConstruct
@PreDestroy
Q10. Dependency Injection Best Practices
Prefer Constructor Injection
Avoid field injection.
Keep Beans Stateless
Singleton beans should not store request-specific data.
Use Interfaces
Program against interfaces rather than implementations.
Minimize Bean Creation
Avoid unnecessary objects.
Banking Example
flowchart TD
RESTController --> TransferService
TransferService --> ValidationService
TransferService --> FraudService
TransferService --> PaymentService
PaymentService --> Repository
Repository --> Database
Every component is managed by the Micronaut IoC container.
Common Interview Questions
- What is Dependency Injection?
- What is IoC?
- How is Micronaut DI different from Spring?
- What are Beans?
- Why prefer Constructor Injection?
- Explain Bean Scopes.
- What is
@Factory? - What is
@Named? - Explain Bean Lifecycle.
- Why is Micronaut faster?
Quick Revision
| Topic | Summary |
|---|---|
| Dependency Injection | Framework injects dependencies |
| IoC | Framework controls object creation |
| Bean | Managed object |
| Singleton | One instance |
| Prototype | Multiple instances |
| Factory | Creates custom beans |
| Constructor Injection | Recommended approach |
| Qualifier | Selects bean implementation |
| Bean Lifecycle | Create → Inject → Use → Destroy |
| Compile-Time DI | No runtime reflection |
Dependency Injection Flow
sequenceDiagram
Application->>Micronaut: Start
Micronaut->>Compiler Metadata: Load Bean Definitions
Compiler Metadata-->>Micronaut: Bean Information
Micronaut->>Service: Create Bean
Service->>Repository: Inject Dependency
Repository-->>Service: Ready
Service-->>Application: Execute Request
Key Takeaways
- Dependency Injection (DI) allows Micronaut to create and inject objects automatically instead of using the
newkeyword. - Micronaut performs dependency injection at compile time, eliminating runtime reflection and improving startup speed.
- The IoC Container manages bean creation, dependency resolution, and lifecycle.
- Constructor injection is the preferred approach because it promotes immutability, testability, and mandatory dependencies.
- Common bean scopes include
@Singletonand@Prototype. - Use
@Factoryto create beans for third-party libraries and external resources. - Qualifiers such as
@Namedhelp resolve multiple implementations of the same interface. - Bean lifecycle can be customized using
@PostConstructand@PreDestroy. - Keep singleton beans stateless and design services around interfaces for better maintainability.
- Understanding Micronaut Dependency Injection is essential for building high-performance, cloud-native enterprise applications.