0.07 秒启动一个 SpringBoot 项目!
写一段简单的 Java 程序。
public class Hello { public static void main(String[] args) { System.out.println("hello world"); }}通常我们想运行它要这样。 [root@flash ~]# javac Hello.java[root@flash ~]# java Hellohello world但运行起来需要 jre。
[root@flash ~]# native-image Hello[hello:11725] classlist: 1,031.19 ms, 0.96 GB[hello:11725] (cap): 2,624.14 ms, 0.96 GB[hello:11725] setup: 3,960.95 ms, 0.96 GB[hello:11725] (clinit): 288.49 ms, 1.72 GB[hello:11725] (typeflow): 2,642.38 ms, 1.72 GB[hello:11725] (objects): 3,803.54 ms, 1.72 GB[hello:11725] (features): 1,176.79 ms, 1.72 GB[hello:11725] analysis: 8,288.82 ms, 1.72 GB[hello:11725] universe: 909.14 ms, 1.75 GB[hello:11725] (parse): 801.67 ms, 1.75 GB[hello:11725] (inline): 1,096.07 ms, 2.32 GB[hello:11725] (compile): 7,352.50 ms, 2.37 GB[hello:11725] compile: 10,146.59 ms, 2.37 GB[hello:11725] image: 1,639.93 ms, 2.37 GB[hello:11725] write: 682.24 ms, 2.37 GB[hello:11725] [total]: 26,855.67 ms, 2.37 GB# Printing build artifacts to: .../hello.build_artifacts.txt
[root@flash ~]# ./hellohello world
-
通过新的 JIT 技术使 Java 程序更快运行 -
多语言支持 -
构建 JVM 无关的本地镜像
[root@flash ~]# ll-rw-r--r-- 1 flash staff 415B 10 27 15:50 Hello.class-rwxr-xr-x 1 flash staff 10M 10 27 15:51 hello[root@flash ~]# time java Hellohello worldjava Hello 0.09s user 0.03s system 113% cpu 0.106 total[root@flash ~]# time ./hellohello world./hello 0.00s user 0.01s system 34% cpu 0.032 total
[root@flash ~]# mvn package -Pnative...[INFO] Executing: ...native-image -cp ... -H:Name=demo-1...[demo-1:7725] classlist: 1,695.81 ms, 0.94 GB[demo-1:7725] (cap): 1,932.48 ms, 0.94 GB[demo-1:7725] setup: 3,287.65 ms, 0.94 GB[demo-1:7725] (clinit): 2,256.61 ms, 5.68 GB[demo-1:7725] (typeflow): 18,462.41 ms, 5.68 GB[demo-1:7725] (objects): 17,848.47 ms, 5.68 GB[demo-1:7725] (features): 4,646.24 ms, 5.68 GB[demo-1:7725] analysis: 45,521.71 ms, 5.68 GB[demo-1:7725] universe: 2,624.03 ms, 5.68 GB[demo-1:7725] (parse): 1,917.71 ms, 5.68 GB[demo-1:7725] (inline): 6,021.71 ms, 5.93 GB[demo-1:7725] (compile): 30,497.99 ms, 6.06 GB[demo-1:7725] compile: 42,184.66 ms, 6.06 GB[demo-1:7725] image: 8,700.31 ms, 5.90 GB[demo-1:7725] write: 1,647.51 ms, 5.90 GB[demo-1:7725] [total]: 106,412.95 ms, 5.90 GB# Printing build artifacts to: .../demo-1.build_artifacts.txt
-rwxr-xr-x 1 flash staff 66M Nov 2 16:11 demo-1-rw-r--r-- 1 flash staff 17M Nov 2 16:09 demo-1-exec.jar
[root@flash ~]# java -jar demo-1-exec.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.6)2021-11-02 16:36:11.192 INFO 9468 --- [main] com.example.demo1.Demo1Application : Starting Demo1Application v0.0.1-SNAPSHOT using Java 11.0.12 on sunyiming07deMacBook-Pro.local with PID 9468 (/Users/sunyiming07/IdeaProjects/graalvm-demos/springboot/demo/demo-1/target/demo-1-0.0.1-SNAPSHOT-exec.jar started by sunyiming07 in /Users/sunyiming07/IdeaProjects/graalvm-demos/springboot/demo/demo-1/target)2021-11-02 16:36:11.195 INFO 9468 --- [main] com.example.demo1.Demo1Application : No active profile set, falling back to default profiles: default2021-11-02 16:36:12.097 INFO 9468 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2021-11-02 16:36:12.110 INFO 9468 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2021-11-02 16:36:12.110 INFO 9468 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]2021-11-02 16:36:12.164 INFO 9468 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2021-11-02 16:36:12.164 INFO 9468 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 917 ms2021-11-02 16:36:12.484 INFO 9468 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2021-11-02 16:36:12.494 INFO 9468 --- [main] com.example.demo1.Demo1Application : Started Demo1Application in 2.033 seconds (JVM running for 2.504)
[root@flash ~]# ./demo-12021-11-02 16:38:33.141 INFO 9724 --- [main] o.s.nativex.NativeListener : This application is bootstrapped with code generated with Spring AOT . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.6)2021-11-02 16:38:33.143 INFO 9724 --- [main] com.example.demo1.Demo1Application : Starting Demo1Application v0.0.1-SNAPSHOT using Java 11.0.12 on sunyiming07deMacBook-Pro.local with PID 9724 (/Users/sunyiming07/IdeaProjects/graalvm-demos/springboot/demo/demo-1/target/demo-1 started by sunyiming07 in /Users/sunyiming07/IdeaProjects/graalvm-demos/springboot/demo/demo-1/target)2021-11-02 16:38:33.143 INFO 9724 --- [main] com.example.demo1.Demo1Application : No active profile set, falling back to default profiles: default2021-11-02 16:38:33.178 INFO 9724 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2021-11-02 16:38:33.178 INFO 9724 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2021-11-02 16:38:33.178 INFO 9724 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]2021-11-02 16:38:33.184 INFO 9724 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2021-11-02 16:38:33.184 INFO 9724 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 41 ms2021-11-02 16:38:33.204 INFO 9724 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2021-11-02 16:38:33.204 INFO 9724 --- [main] com.example.demo1.Demo1Application : Started Demo1Application in 0.078 seconds (JVM running for 0.08)
我去!0.078 秒!!!
Class.forName("com.flash.Student")
[ { name: "com.flash.Student", allDeclaredConstructors: true, allPublicMethods: true }, { name: "com.flash.Teacher", fileds: [{name: "teach"}, {name: "talk"}], methods: [{ name: "<init>", parameterTypes: ["char[]"] }] }, // ……]