Many Java developers have turned to Micronaut as a framework for lightweight, modular application development. Though Micronaut is still considered a new framework, the community of developers who maintain it have made notable improvements with each new iteration, which now includes Micronaut version 3.0.
Micronaut version 3 includes thousands of updates, such as changes to object lifecycles, error handling and injection patterns. This article covers a brief history of Micronaut, the major features of Micronaut 3.0 and the changes that may influence a developer's decision to make the jump from older versions of the framework.
Brief history of Micronaut
Micronaut is a Java execution software framework designed to operate in a way that resembles a lightweight JVM. This open source framework suits low-memory, message-driven microservices, thanks to its support for OpenAPI documentation and lambda functions. It also features readily available integrations with management tools like Nats.io and Kafka.
The first version of Micronaut was introduced in May 2018 as an open source and free framework for building microservices. It was specifically designed for compatibility with GraalVM, and aims to provide developers looking to avoid the longer startup times associated with some of the more sophisticated Java-based frameworks, such as Spring Boot.
What to know about Micronaut 3.0
Micronaut 2.0.0 was released in 2020, with subsequent dot releases adding improvements to startup performance, the ability to isolate bugs from nullable annotations, and increased bean customization. Eventually, Micronaut 3.0.0 was released in 2021 and contained major improvements focused on developer interactions with annotations.
It's worth noting that version 3.0 still allowed developers to transfer legacy annotations from previous versions of the framework. A migration from older versions to Micronaut version 3 should not severely impact existing projects residing in older versions frameworks, provided developers use the OpenRewrite code manager to make the switch.
While there is an extensive list of differences between Micronaut's latest versions, most notable are object management, error handling and injection patterns. The release of Micronaut 3 also changed how developers interact with inherited annotations, which now require an @Inherited label to work properly (except for annotations connected to bean scope).
In Micronaut, beans -- which closely resemble JavaBeans -- are objects that hold source code information meant for reuse across multiple applications or services. This is a handy feature for developers to avoid repetitive work. However, unnecessary beans -- typically referred to as undefined beans -- must be removed from the codebase to allow room for new objects and to avoid redundant code.
Beans typically become undefined in cases where another active bean marked with the same priority level performs the same function. Developers usually address this issue by providing each bean a set of parameters that govern its lifecycle known as a scope. However, it's possible that beans may end up losing their scope information and start to crowd the central codebase.
To fix this issue, a standout addition in Micronaut version 3 is the @PreDestroy command. This command enables a mechanism that destroys beans that don't contain a scope to dictate their lifecycle. Developers do not have to manually go into the system to find and delete similar or dead beans, which can save a lot of time in the overall development process.
In Micronaut, injections provide object properties to beans; rather than beans connecting straight to the main system, the injection connects to the central codebase. Injections define a bean further as they are used to classify beans within a string of coded commands. Injections allow dependencies to run in their specified environment adding to the stability of the codebase's overall structure.
Version 3 of Micronaut provides improvements to factory pattern implementations, which allow objects to operate through classes outside of the main codebase. Because the application can only locate a bean through return type and parent class annotations, those beans can operate under a maintainable system of defined and related classes.
Micronaut has also replaced the javax.inject package with jakarta.inject, setting jakarta.inject as the default annotation. The change in name is attributed to a conflict with the Javax trademark. However, these annotations still perform the same functions, and all existing Javax annotations are still transferable to version 3.
Error path redirection
Those that find errors while creating microservices and applications with Micronaut 3.0 can now glean more insight from annotations. @Error annotations specify the error path before redirecting to the original HTTP status. Developers can use this feature to observe the path and glean information about the error message's cause.
Micronaut 3.0 also provides an update to error route priority and redirects error annotations to specific exceptions before searching for the HTTP status. Exceptions trigger redirection of the original HTTP status, while errors that do not have a status redirect to an original status by default.
Upcoming Micronaut versions
In May 2022, The Micronaut Framework put out a poll on Twitter regarding changes to support Java (JDK) applications in Micronaut 4.0.0. Although there is no official release date for Micronaut 4, the team is actively asking the community for input. The latest polls suggest that those interested in Micronaut would prefer that version 4.0.0 support version 17 of Java rather than earlier versions like 8 and 11.