Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is Micronaut?

Meet Micronaut, the JVM framework that does dependency injection at compile time: fast startup, low memory, small native images, and where it fits.

Micronaut is a JVM framework for building microservices, serverless functions, command-line tools and message-driven apps in Java, Kotlin or Groovy. It looks familiar if you have used Spring Boot: controllers, beans, @Inject, configuration files, data repositories. The big difference is when the framework does its work. Micronaut analyzes your code while it compiles and writes the wiring as ordinary classes, so almost nothing has to be discovered by reflection when the program starts.

This lesson explains that idea, what you gain from it, and what you give up. Micronaut itself is not installed on this site, so the real framework code is read-only. The two runnable examples are plain-Java stand-ins that show the underlying mechanism: one container that finds its wiring at runtime by reflection, and one that uses wiring prepared in advance.

The problem Micronaut set out to solve

A traditional Java framework starts by scanning. It reads every class on the classpath, looks for annotations such as @Service, decides what depends on what, and builds proxies, all using reflection and bytecode generation. That work happens on every start of every instance. For one long-running monolith this cost is small. For a fleet of microservices that scale out and in, or a serverless function that must answer within a fraction of a second, it hurts:

  • Startup takes seconds because the scan repeats each time.
  • Memory use is higher, because reflection metadata and generated proxies live in the heap.
  • Native compilation with GraalVM is difficult, because reflection must be declared up front.

Micronaut moves the scan to build time. An annotation processor runs inside javac, reads your annotations, and generates a bean definition class for each bean. At runtime the container simply loads those classes. It does not scan, does not use reflection to call constructors, and does not create runtime proxies for things like AOP advice.

Two ways to wire an app

Runtime scanning Find beans and wire them on every start.
Compile-time wiring Generate the wiring once, while building.

Micronaut takes the second route.

1 / 2

What a Micronaut app looks like

Here is a complete service. You do not need to run it, just read it:

Java
package demo;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Singleton;

@Singleton
class GreetingService {
    String greet(String name) {
        return "Hello, " + name + "!";
    }
}

@Controller("/hello")
class HelloController {

    private final GreetingService service;

    HelloController(GreetingService service) {   // constructor injection
        this.service = service;
    }

    @Get("/{name}")
    String hello(String name) {
        return service.greet(name);
    }
}

@Singleton comes from jakarta.inject, the standard annotation set (JSR-330). @Controller and @Get are Micronaut's own. If you know Spring MVC, this reads almost the same. During compilation, the annotation processor writes classes such as $GreetingService$Definition and $HelloController$Definition that know how to construct each bean and which dependencies to pass in.

Micronaut's DI is built on the jakarta.inject standard, so most injection annotations (@Inject, @Singleton, @Named, @Qualifier) are shared with other frameworks. Anything Micronaut-specific lives in the io.micronaut packages.

Feature tour

Area What Micronaut provides
Dependency injection Compile-time DI, scopes, qualifiers, factories, conditional beans
HTTP server Netty-based, annotation routing, filters, reactive support
HTTP client Declarative @Client interfaces generated at compile time
Data Micronaut Data: JDBC, JPA, R2DBC, MongoDB repositories without runtime proxies
Config Typed properties, profiles, environments, application.yml
Security JWT, sessions, OAuth 2.0/OpenID Connect, LDAP
Cloud Service discovery, distributed config, Kubernetes, AWS Lambda, Azure Functions
Testing @MicronautTest, test resources, Testcontainers support
Native First-class GraalVM native image support

You pick only the modules you need. A small service might use the HTTP server, Micronaut Data and nothing else, and its startup cost reflects that.

Micronaut compared with other frameworks

Spring Boot has the largest ecosystem and the most tutorials; Micronaut and Quarkus both aim for faster startup and a smaller footprint by doing work at build time. Micronaut's distinguishing choices are its compile-time AOP (no runtime proxies, no CGLIB), its framework-agnostic annotations, and its support for many languages. The tradeoffs are a smaller community than Spring and a build that does more work, so compile times can be a little longer.

Stand-in 1: a container that scans and reflects

The first stand-in builds objects the traditional way. The ReflectiveContainer is given classes, inspects each constructor with reflection, and creates dependencies on demand. Everything it knows, it discovers while running. This is an imitation of the old approach, not Micronaut code.

Java Wiring by reflection at runtime

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Stand-in 2: wiring prepared in advance

Now the same graph, but each bean has a small definition that already knows how to build it. A real Micronaut build generates such definitions for you. Here we write them by hand so you can see what the container does with them: it only calls lambdas, with no constructor inspection.

Java Wiring from precomputed definitions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The output is the same, but the second container never looks inside a class at runtime. In real Micronaut the "No bean" error is even better: because the processor sees the whole graph during compilation, a constructor that needs a bean nobody provides fails the build with a message naming the class and parameter.

flowchart LR
    A["Your source code"] --> B["javac + Micronaut annotation processor"]
    B --> C["Your classes"]
    B --> D["Generated bean definitions"]
    C --> E["Application starts"]
    D --> E
    E --> F["Container loads definitions, no scan"]

The wiring is produced at build time and only loaded at startup.

When to choose Micronaut

Choose it when startup time, memory or native images matter: serverless functions, many small services, CLI tools, or Kubernetes workloads that scale quickly. It is also a good fit when you want one framework that supports Java, Kotlin and Groovy equally. If your team already has deep Spring knowledge and long-running servers, Spring Boot may be the more productive choice. Compare it with Spring Boot and Quarkus before committing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does Micronaut analyze your annotations and generate bean definitions?

    An annotation processor runs during javac and generates definition classes, so the work is done once at build time.
  2. 2. Which standard provides Micronaut's core injection annotations such as @Singleton and @Inject?

    Micronaut follows the jakarta.inject (JSR-330) standard; only framework-specific features live in io.micronaut.
  3. 3. What happens in Micronaut when a constructor needs a bean that does not exist?

    Because the whole bean graph is checked during compilation, unsatisfied dependencies are reported by the build.
  4. 4. Which is NOT a main benefit of compile-time DI?

    The work moves to build time, so compiling usually does a little more, not less.

Summary

  • Micronaut is a JVM framework for microservices, serverless and CLI apps in Java, Kotlin or Groovy.
  • It generates bean definitions during compilation instead of scanning and reflecting at startup.
  • That gives faster startup, lower memory, easier native images and build-time error detection.
  • Injection annotations come from jakarta.inject; HTTP, data and config features come from Micronaut modules.
  • The cost is a heavier build and a smaller ecosystem than Spring.

Next: Creating a Micronaut App