Hi guys, I hope you are doing good. In this blog, we will be talking about the Micronaut framework, how it’s better than other frameworks in the same trail. What makes it different from other frameworks so let’s get started.
What is Micronaut ??
Micronaut is a modern, JVM-based, full-stack microservices framework designed for building modular, easily testable microservice applications. We can develop a full-featured microservice application using the Micronaut framework as same as with Spring. Micronaut uses Netty and has first-class support for reactive programming. It takes inspiration from Spring, Spring Boot, and Grails. It aims to avoid the downsides of the framework which we have in the Spring framework by providing –
- Fast boot time
- Less memory footprint
- Minimal use of Reflection
- Avoid AOP proxies generation
How Micronaut is doing the magic?
If I talk about Inversion of control(Dependency Injection) then the Spring framework comes to our mind, right and we also know that Micronaut is inspired from Spring, In Spring, Dependency Injection performed at runtime using reflection and proxies. Thus when a spring application is started, the classpath is scanned for annotated classes and concrete objects are instantiated and linked but here in Micronaut it’s performed at Compile time using compile-time data but the question is how it’s benefiting the framework and what is the advantage of it?
Reflection is not good for performance as Spring uses it for DI, where Spring scans the classpath at the runtime and lookup the classes via reflection, which eats up some time and makes the boot-up process a little slower. whereas Micronaut implements the JSR-330 specification for the same thing at compile-time and ASM byte code library used for auto-generate classes where Micronaut knows the injection point at compile time so there would no need to scan all methods and classes at runtime, so JVM can optimize and inline the code in terms of runtime performance and reduced memory consumption but there would be some increase in the compile time of the application.
The IOC part of Micronaut is completely independent of the framework, we can also use it in other application types. We need to only configure the build using micronaut-inject-java dependency. for example –
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<scope>provided</scope>
</dependency>
Micronaut supports the following types of dependency injection –
- Constructor injection
- Field injection
- Method parameter injection
Installation
Best way to install the Micronaut CLI on unix systems is using sdkman.
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk version
Run following commands to install the Micronaut on your system –
sdk update
sdk install micronaut <version>
Now we can access the Micronaut CLI easily using mn command. We can also make Micronaut based micro service from scratch by doing some configuration and by including dependencies.
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
Create your first server application using CLI-
We can create the new Micronaut application using the Micronaut CLI in Groovy, Java and Kotlin.
mn create-app hello-world --lang=java
Creating a simple controller –
The main class generated for us by mn command –

Now we can create and configure the controller manually but here we will use the mn command again.
mn create-controller Hello

Command generates a Java class name HelloController with one GET endpoint. Here the @Controller and @Get are Micronaut framework specific annotations.
Will get the following response from the service –

That’s all for this blog, I hope you like this blog. If you have any queries or want to know more about it, you can add your queries in the comment section. I will be happy to answer them.
Reference
