0

I have read about ahead of time compilation and that there is Micronaut, a Java framework to do so.

I just wanted to know if it's possible to do that in Spring Boot.

tmarwen
  • 15,750
  • 5
  • 43
  • 62
Atulya
  • 93
  • 1
  • 9
  • 4
    No, and maybe. Spring have recently announced a beta of Spring Native: https://spring.io/blog/2021/03/11/announcing-spring-native-beta Part of the beta appears to be the introduction of some AOT. – Gavin Apr 20 '21 at 11:38

2 Answers2

5

You can use Spring Native:

Spring Native provides support for compiling Spring applications to native executables using the GraalVM native-image compiler.

Spring Native is still beta, at version 0.9.2 as of Apr 20, 2021.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Just by using Java and Spring boot, I do not think so. Since the AOT compiled code will be platform dependent, it will contradict what we currently have with Java. Java achieves platform independency using java bytecode. With AOT, it will have bypass the step for generating java bytecodes and with the current java architecture in place, it is not possible. The red line on the diagram below shows what AOT compilation in Java with the current architecture will have to do.

enter image description here

However new techs are emerging, such as GraalVM. GraalVM will create a native image of the code, which means the code will be compiled to machine code directly (with GraalVM's compiler) and will be executed on the GraalVM platform. This provide the opportunity to have multiple languages executed at runtime. Like we can have an application whose codebase is in Java and Python. We have some limitations on these platforms for now, which makes them kinda experimental. But I won't be surprised to see AOT compiled java code in the future running on a different platform.

atish.s
  • 1,534
  • 11
  • 19
  • 1
    I am not sure AOT makes the code platform dependent, my simplistic understanding is that rather than making use of reflection and byte code weaving at runtime, using AOT the injected dependencies etc are figured out at compile time, producing standard bytecode (in the case of Java). I believe the GraalVM is a separate technology with a similar goal of reducing the memory footprint and start up times of applications. For example you do not need to make use of GraalVM to use Micronauts, which is compiled using AOT to standard (JVM) bytecode. – Gavin Apr 20 '21 at 14:22
  • 1
    "I am not sure AOT makes the code platform dependent" - It doesn't. You could certainly use AOT to generate platform dependent code, but platform dependent code is definitely not inherent when doing AOT. For example, Micronaut uses AOT to do many things, all of them are platform independent. – Jeff Scott Brown Apr 20 '21 at 14:36
  • I was highlighting why AOT is not possible with using just Java in general. I'm not sure if Micronaut is doing AOT compilation. Personally I feel Micronaut seems to optimize the classes to be compiled such as to remove the overhead of Spring and other dependencies in general. e.g Micronaut optimize the code base by removing proxies and overheads, then use basic java bytecode compiler to compile the optimized code? Please enlighten me. I am also new to these technologies however familiar with docker and containerization in general. – atish.s Apr 20 '21 at 15:54
  • 1
    I think that the confusion here is that in the context of Micronaut (and Spring Native) AOT has a slightly different meaning than it perhaps has when referring to compilation of a language to a machine code. In this context AOT refers to the process of moving the frameworks "building" of the application at runtime (i.e mapping dependency) to making those determinations during the build phase. Perhaps micronaut offer a better explanation: https://micronaut.io and https://micronaut.io/2018/10/08/micronaut-1-0-rc2-and-the-power-of-ahead-of-time-compilation/ – Gavin Apr 21 '21 at 12:45