BT

InfoQ Homepage News Micronaut for Spring Allows Spring Boot Apps to Run as Micronaut Apps

Micronaut for Spring Allows Spring Boot Apps to Run as Micronaut Apps

Bookmarks

In conjunction with the minor release of Micronaut 1.0.1, Object Computing, Inc. (OCI) also released Micronaut for Spring 1.0 M1. As stated in the press release:

Micronaut for Spring adds the ability to use the long-established Spring annotation-based programming model to build Micronaut applications and libraries that work with both Micronaut and Spring.

Using Ahead-of-Time compilation, Micronaut for Spring allows for: integrating Spring components into a Micronaut application; running Spring applications as Micronaut applications; and exposing Micronaut beans to a Spring application.

Jonathan Giles, senior software engineer at Microsoft, recently discussed why developers should consider migrating to Micronaut:

Micronaut takes a different approach to enabling everything we developers take for granted in Spring Boot and MicroProfile. Rather than do runtime annotation processing as Spring Boot and MicroProfile, Micronaut uses annotation processors at compile time to generate additional classes that are compiled alongside your code. This means startup time is reduced due to the substantially lower amount of overhead that is required to scan the classpath of your project. In fact, Micronaut tries to avoid reflection as much as possible, only using it where absolutely necessary.

Formerly known as Project Particle, Micronaut is a full-stack JVM-based framework for creating microservice-based, cloud-native and serverless applications that can be written in Java, Groovy, and Kotlin. Graeme Rocher, principal software engineer and Grails and Micronaut product lead at OCI, first introduced Micronaut at the Greach conference in March 2018. The road to version 1.0 included three release candidates within a less-than-three-week timeframe just before the official release of version 1.0 in late October 2018.

Example Application

An example application, included in the Micronaut for Spring repository, demonstrates how to use Micronaut for Spring including tools to convert the application to a GraalVM native image.

The Application.java file, shown below, takes a Spring Boot application at the source code level that may be executed as a Micronaut application:

    
1 @SpringBootApplication
2 public class Application {
3
4    public static void main(String... args) {
5        Micronaut.run(Application.class);
6        // SpringApplication.run(Application.class);
7    }
    

The only difference between a Spring Boot application and a Micronaut application is shown in line 5. Line 6, commented for comparison, would normally be used for a Spring Boot application.

The command:

    
./gradlew bootRun
    

builds and initiates the Micronaut server, where:

    
curl -X GET http://localhost:8080/greeting?name=Mike
    

will return:

    
{"id":1,"content":"Hola, Mike!"}
    

GraalVM

It is now possible to execute Spring applications on GraalVM that are fully compliant with the Spring annotation-based programming model. The example application can be converted to a GraalVM native image with Micronaut for Spring.

The following two commands starts the process:

    
./gradlew assemble
java -cp build/libs/greeting-service-all.jar io.micronaut.graal.reflect.GraalClassLoadingAnalyzer
    

The Micronaut GraalClassLoadingAnalyzer class is used to analyze the classloading requirements of a Micronaut application.

The GraalVM Java distribution ships with the utility, native-image, used to build a native image of an application. There are a number of configurable parameters as shown in a partial listing of the call to native-image:

    
native-image --no-server \
         --allow-incomplete-classpath \
         --class-path build/libs/greeting-service-all.jar \
         -H:ReflectionConfigurationFiles=build/reflect.json \
         -H:EnableURLProtocols=http \
         -H:Name=greeting-service \
         -H:Class=greeting.example.Application \
    

A convenient shell script, build-native-image.sh, is included in the example application to automate the process. The generated native image executable file, greeting-service, may be invoked to start the server:

    
./greeting-service
    

Designed for an instantaneous startup, the native image of the application was initiated in 42ms:

Resources

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Grails Rebranded

    by Owen Rubel /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Micronaut is just Grails rebranded and has an even smaller community and even less plugins. Not ready for primetime or adoption.

  • Re: Grails Rebranded

    by Michael Redlich /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Hi Owen:

    Have you even tried experimenting with Micronaut? While I understand and respect your concern that this is a rebranding of Grails, there's more to Micronaut than Grails in my experience. It hasn't even been out a year yet, so let's see how the Java (JVM) community adopts to Micronaut this year. I know that I will be watching it.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.