Java / Micronaut Interview questions
What is dependency injection in Micronaut?
Dependency injection (DI) in Micronaut is the pattern where a class declares what it needs through its constructor or fields instead of creating those dependencies itself, and the framework supplies them.
A class becomes injectable by annotating it with a scope annotation such as @Singleton, and consumers request it with @Inject on a constructor parameter or field. At compile time, Micronaut's annotation processor generates a BeanDefinition for each candidate class describing its dependencies, qualifiers, and scope.
@Singleton public class InvoiceService { private final PaymentClient paymentClient; @Inject public InvoiceService(PaymentClient paymentClient) { this.paymentClient = paymentClient; } }
At startup, the ApplicationContext reads these generated bean definitions - not the class files themselves - to wire everything together, so no reflection-based scanning of the classpath is needed.
More Related questions...