问题
The GraalVM system apparently cannot compile a Spring application into a native image.
Can we compile a subset of the Spring application -- say, as a separate library -- and then use that with the remainder compiled using the usual javac compiler?
Or maybe if we leave out some Spring features from our application?
Any other possibilities?
回答1:
The opening statement of this question is a bit vague so it's hard to address it properly.
GraalVM absolutely can compile Spring applications. GraalVM distibution is very similar to a normal JDK, it includes a javac
utility, a java
utility, which can be added to the path and used normally.
You can set up the $JAVA_HOME
environment variable to point to the directory you unpacked the GraalVM distribution, add $JAVA_HOME/bin
to the path, and build Spring applications the way you usually build them, with Maven or Gradle, or any other build tools.
GraalVM can also run Spring applications, compiled both by itself and other JVMs. If you're curious, here's an example of a Spring application that not only runs on GraalVM, but also uses R to visualize a plot of the data, using GraalVM polyglot capabilities.
Now, I guess what your meant is the ability of GraalVM to create executable native images of some Java programs.
Update: November 17th 2019
Some Spring applications work as GraalVM native images. There's active work on making the support better by Pivotal and GraalVM teams. Here's a session by Sébastien Deleuze from Devoxx Belgium 2019 about state of the Spring applications and GraalVM native images where he shows a small hello world Spring application working as a native image and vanilla Spring Petclinic demo using JPA and in-memory database working as a native image: https://www.youtube.com/watch?v=3eoAxphAUIg
You can follow the instructions here: https://github.com/spring-projects-experimental/spring-graal-native to build or investigate the samples.
Note this project is experimental as it is noted in its README as well.
The support for the native image is not optimized yet and it will get better, currenly if I try the spring-petclinic-jpa
example from this repository it can start in around 200 ms on my not powerful macbook:
14:13:11.990 [main] INFO o.s.s.petclinic.PetClinicApplication -
Started PetClinicApplication in 0.171 seconds (JVM running for 0.173)
Previous Update: May 17th 2019
Here's the spring-framework's wiki page for GraalVM native image support.
The spring-graal-native experimental project, created by Andy Clement, shows how it is possible to run a Spring Boot application out of the box as a GraalVM native image. It could be used as a basis for a potential upcoming official support.
All in all, you might try it, but things might not work fully as expected.
Previous answer is below:
There's a spring-fu project, an experimental Kotlin micro-framework based on functional configuration intended to test new ideas for future Spring Boot releases, which is currently experimenting with being able to get compiled to native images by GraalVM.
At the same time, GraalVM team is investigating what can be done to simplify compiling Spring apps to native images and to support more Spring apps than currently. Some of the limitations will remain, so you'll always be able t construct a Spring app that won't work as a GraalVM native image, but perhaps you'll be able to construct Spring apps which will work too.
The exact roadmap of these changes is currently unclear.
Here's a SpringFramework issue tracker ticket that one can follow to see the development.
来源:https://stackoverflow.com/questions/50911552/graalvm-and-spring-applications