Quick revision
The short answer
Micronaut uses Java annotation processors to generate dependency injection and AOP metadata at compile time, so at run time it creates beans from generated classes instead of using reflection and runtime proxies. Startup time and memory therefore stay nearly flat as the number of beans grows, native images need little reflection configuration, and many wiring errors appear at compile time. The consequence for developers is that annotation processing must run for every module in the build, and dynamic runtime tricks are limited; Micronaut Data applies the same idea to repository queries.
What to remember
In this answer, we saw that Micronaut moves dependency injection from startup to compile time. Annotation processors generate bean definitions and AOP subclasses, so the application builds beans with direct constructor calls, without classpath scanning, reflection or runtime proxies. Startup and memory stay low as beans grow, native images need little configuration, and many wiring errors show early. In exchange, every module must run the annotation processors and runtime dynamism is limited, and Micronaut Data generates repository queries at compile time as well.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Micronaut uses Java annotation processors to generate dependency injection and AOP metadata at compile time, so at run time it creates beans from generated classes instead of using reflection and runtime proxies. Startup time and memory therefore stay nearly flat as the number of beans grows, native images need little reflection configuration, and many wiring errors appear at compile time. The consequence for developers is that annotation processing must run for every module in the build, and dynamic runtime tricks are limited; Micronaut Data applies the same idea to repository queries.
Why this question is asked
Every DI framework has to answer the same questions: which beans exist, what do they need, and how do we build them. Spring answers them mostly at startup, Micronaut answers them in the compiler. That one decision explains most of Micronaut's strengths and most of the surprises developers meet with it. Interviewers ask this to see whether we understand dependency injection at the level of how it is implemented. In this answer, we will recap how Spring does it, see what Micronaut generates instead, look at the effects on startup and errors, and then at the consequences for builds and developers, including Micronaut Data.
1A Quick Recap: How Spring Finds and Wires Beans
Dependency injection means a class declares what it needs, usually as constructor parameters, and the framework supplies those objects. Spring finds candidate classes by scanning the classpath for annotations at startup, reads their constructors and injection points with reflection, and builds the objects. For features like @Transactional, it creates a proxy at runtime, a generated wrapper class, often made with a bytecode library.
This is flexible: anything on the classpath can join the application. But it costs time and memory at every startup, and the cost grows with the number of classes. Reflection data must be kept in memory, and in a native image every reflective access must be declared in advance.
2What Micronaut Generates at Compile Time
Micronaut plugs into javac as an annotation processor, a compiler plugin that sees the annotated classes while they compile. For each bean it generates small classes, a bean definition and its reference, that know how to construct the bean, which dependencies to pass to the constructor and which annotations it carries. For AOP, such as @Transactional or @Cacheable, it generates a subclass at compile time that adds the behaviour, instead of creating a proxy at run time.
- Spring: startupscan classpath, reflect on classes, create runtime proxies
- Spring: run timebeans and proxies in memory
- Micronaut: compile timeannotation processor writes bean definitions and AOP subclasses
- Micronaut: startupload generated definitions, call constructors directly
4 layers, from the top down. Step through them one at a time.
Micronaut moves the analysis into javac, so startup only executes its results.
Notice that the generated code calls new PriceService(...) directly. At run time, Micronaut reads a list of bean definition references, generated into the jar, and uses them. There is no classpath scan and no reflective constructor lookup. One detail follows from this: injecting into private fields would need reflection, so Micronaut recommends constructor injection or package-private fields.
3Effects on Startup, Memory and Errors
Because the work was done in the compiler, startup cost no longer grows much with the number of beans. A service with 50 beans and one with 500 start in a similar, short time, and memory stays low because no reflection metadata is cached. For GraalVM native images this is a natural fit: there is little reflection to declare, so builds need little extra configuration.
Many wiring mistakes surface early. The processor validates what it can see, so some errors, such as an injection point or annotation used incorrectly, fail compilation. Others, such as a missing bean whose implementation lives in another module, still appear at startup, but with a precise message. In Spring, the equivalent problem often appears only when the context starts.
4The Consequences for Developers
The most common surprise is the build. If a module's compilation does not run Micronaut's annotation processors, its classes get no bean definitions, and at run time the beans simply do not exist; the error says no bean of that type was found, though the class is clearly annotated. Every module with beans needs the processor configured, for example annotationProcessor("io.micronaut:micronaut-inject-java") in Gradle or the annotationProcessorPaths in Maven's compiler plugin. Micronaut's Gradle and Maven plugins set this up, which is the safest choice. Kotlin uses KSP or kapt instead, and Groovy has its own AST transformations.
The second consequence is less runtime dynamism. Tricks that rely on modifying beans while the application starts, scanning arbitrary third-party classes for annotations, or creating proxies of any class at run time do not fit the model. Third-party classes that we want as beans are usually exposed through a @Factory method instead. Builds also take a little longer, since the processor runs during compilation.
5Micronaut Data: Queries Generated at Compile Time Too
Micronaut Data applies the same approach to repositories. In Spring Data, a method name such as findByCustomerAndStatus is parsed when the application starts. In Micronaut Data, the annotation processor parses it during compilation and generates the query, JPQL or SQL, into the repository's metadata.
Notice that the typo in the second method fails the build instead of failing startup or the first call. Queries are also not translated at run time, so repository-heavy services start faster. The generated SQL shown in the comment is simplified; the exact text depends on the dialect and mapping.
6Follow-up questions
If Micronaut avoids reflection, how does it read annotations at run time?
It records annotation values in the generated metadata at compile time. When code asks a bean definition for its annotations, Micronaut answers from that metadata, without calling Java's reflection API. This is also how it supports meta-annotations and annotation aliases cheaply.
Can Micronaut use Jakarta Inject annotations?
Yes. Micronaut 4 uses jakarta.inject annotations such as @Inject, @Singleton and @Named, so code that uses only those is fairly portable. Micronaut-specific annotations such as @Factory, @Requires and @Value add its extra features.
What happens in a library module that I forget to process?
The module compiles fine, but its @Singleton classes have no generated definitions, so the application cannot find them. Injecting one causes a no-such-bean error at startup. Checking the module's build configuration for the annotation processor is the first step whenever an annotated bean seems invisible.
7Conclusion
In this answer, we saw that Micronaut moves dependency injection from startup to compile time. Annotation processors generate bean definitions and AOP subclasses, so the application builds beans with direct constructor calls, without classpath scanning, reflection or runtime proxies. Startup and memory stay low as beans grow, native images need little configuration, and many wiring errors show early. In exchange, every module must run the annotation processors and runtime dynamism is limited, and Micronaut Data generates repository queries at compile time as well.
Open with
Spring builds its bean graph at startup: it scans the classpath, uses reflection to read constructors and injection points, and creates proxies at runtime for things like transactions.
Then, if they want more
Micronaut does that work in the compiler. It ships annotation processors that run inside javac, and for each bean they generate a bean definition class that calls the constructor directly with its dependencies, plus metadata about annotations. AOP like @Transactional becomes a generated subclass at compile time, not a runtime proxy.
At startup Micronaut just loads the generated definitions, so there's no scanning and no reflection. The consequences: startup and memory stay low and don't grow much with the number of beans, native images need very little reflection config, and many wiring mistakes show up at compile time or with precise startup errors. For developers, the catch is the build.
Every module with beans must run the processors, micronaut-inject-java in Gradle or Maven, otherwise beans silently don't exist. Field injection into private fields needs reflection, so constructor injection is preferred, and dynamic runtime tricks don't fit. Micronaut Data does the same for repositories: query methods are parsed at compile time, so a typo in a method name fails the build.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does Micronaut implement dependency injection differently from Spring, and what are...": https://scrivio-interview-library.netlify.app/java/other-java-frameworks/6696caf66e/