SpringBoot3-14.AOT-SpringBoot 整合

2024年01月11日 18:34 · 阅读(214) ·

开发环境

名称 版本
操作系统 Windows 11 X64
Visual Studio Communty 2022
GraalVM graalvm-community-jdk-17.0.9_windows-x64_bin.zip
Native-Image native-image-installable-svm-java17-windows-amd64-22.3.2.jar

概述

  1. /**
  2. * 打包成本地镜像
  3. * 1.打成 Jar 包:注意修改 jar 包内的 MANIFEST.MF 文件,指定 Main-Class 的全类名
  4. * - java -jar xxx.jar 就可以执行。
  5. * - 切换机器,安装 java 环境,默认解释执行,启动速度慢,运行速度慢。
  6. * 2.打成本地镜像(可执行文件):
  7. * - Windows: exe
  8. * - native-image cp 你的jar包/class路径 主类 -o 输出的文件名
  9. * - native-image -cp boot3-15-aot-common-1.0-SNAPSHOT.jar com.atguigu.MainApp -o Haha
  10. * 并不是所有的 Java 代码都能支持本地打包: SpringBoot 保证 Spring 应用的所有程序都能在 AOT 的时候提前告知 GraalVM 怎么处理?
  11. * - 动态能力损失: 反射的代码-动态获取够朝气,反射创建对象、反射调用方法都不可以。
  12. * 解决方案: 额外处理(SpringBoot 提供的了一些注解): 提前告知 GraalVM 会用到的反射的方法、构造器
  13. * - 配置文件损失:
  14. * 解决方案: 额外处理(从相对路径读取、配置中心): 提前告知 GraalVM 配置文件怎么处理
  15. *
  16. * 二进制里面不能包含的,不能动态的,都得提前处理
  17. *
  18. * 不是所有框架都适配了 AOT 特性: Spring 全系列适配 OK
  19. */

新建 SpringBoot 项目-boot3-16-aot-springboot

  • 新建模块 boot3-16-aot-springboot

  • 创建模块

名称 boot3-16-aot-springboot
位置 D:\Study-Java\2023\SpringBoot3-Study
语言 Java
类型 Maven
com.atguigu
工件 boot3-16-aot-springboot
软件包名称 com.atguigu.boot3.aot
JDK graalvm-17
GraalVM version 17.0.9
Java 17
  • SpringBoot 3.2.0
  • 依赖项目
  1. Developer Tools
  2. -- GraalVM Native Support
  3. Web
  4. -- Spring Web

pom.xml

  • 此依赖会自动导入
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.graalvm.buildtools</groupId>
  5. <artifactId>native-maven-plugin</artifactId>
  6. </plugin>
  7. <plugin>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-maven-plugin</artifactId>
  10. </plugin>
  11. </plugins>
  12. </build>

新建测试控制器-HelloController

  1. package com.atguigu.boot3.aot.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloController {
  6. @GetMapping("hello")
  7. public String hello() {
  8. return "Hello,SpringBoot AOT!";
  9. }
  10. }

生成 Native-Image

使用 Maven 插件生成

  • 点击 Maven-boot3-16-aot-springboot-生命周期-clear
  • 点击 Maven-boot3-16-aot-springboot-生命周期-compile

  • 点击 Maven-boot3-16-aot-springboot-插件-spring-boot-spring-boot:process-aot

  • 生成了下面两个文件夹

  • 点击 Maven-boot3-16-aot-springboot-native-native:build

运行报错-Error: Please specify class

  1. 'native:build' goal is deprecated. Use 'native:compile-no-fork' instead.
  2. [INFO]
  3. [INFO] ----------------< com.atguigu:boot3-16-aot-springboot >-----------------
  4. [INFO] Building boot3-16-aot-springboot 0.0.1-SNAPSHOT
  5. [INFO] from pom.xml
  6. [INFO] --------------------------------[ jar ]---------------------------------
  7. [INFO]
  8. [INFO] --- native:0.9.28:build (default-cli) @ boot3-16-aot-springboot ---
  9. [WARNING] 'native:build' goal is deprecated. Use 'native:compile-no-fork' instead.
  10. [INFO] Found GraalVM installation from JAVA_HOME variable.
  11. [INFO] Executing: C:\Program Files\Java\graalvm-community-openjdk-17.0.9+9.1\bin\native-image.cmd @target\tmp\native-image-8889678704731965610.args
  12. Error: Please specify class (or <module>/<mainclass>) containing the main entry point method. (see --help)

配置环境变量

  • 配置环境变量后,重启 IDEA
环境变量
Path # 这里的路径和 VS2022 安装路径有关,需要根据自己的情况进行修改
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x64
INCLUDE C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt

# 这里的路径和 VS2022 安装路径有关,需要根据自己的情况进行修改
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\include;
# 这里的路径和 Windows 不同版本有关,需要根据自己的情况进行修改
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared;
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um;
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt
lib C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64

# 这里的路径和 VS2022 安装路径有关,需要根据自己的情况进行修改
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\lib\x64;
# 这里的路径和 Windows 不同版本有关,需要根据自己的情况进行修改
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64

再次运行报错-args returned non-zero result

点击 Maven-boot3-16-aot-springboot-native-native:build

报错

  1. [INFO] ------------------------------------------------------------------------
  2. [INFO] BUILD FAILURE
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Total time: 3.652 s
  5. [INFO] Finished at: 2024-01-12T09:51:34+08:00
  6. [INFO] ------------------------------------------------------------------------
  7. [ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.28:build (default-cli) on project boot3-16-aot-springboot: Execution of C:\Program Files\Java\graalvm-community-openjdk-17.0.9+9.1\bin\native-image.cmd @target\tmp\native-image-4544108742480805183.args returned non-zero result -> [Help 1]
  8. [ERROR]
  9. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  10. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  11. [ERROR]
  12. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  13. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
  • 这个报错的原因是我的项目目录 D:\luoma\学习\Code\2023\Java\SpringBoot3-Study\boot3-16-aot-springboot 带有中文

  • 把项目 SpringBoot3-Study 复制到 D:\data\SpringBoot3-Study

运行成功

  • 点击 Maven-boot3-16-aot-springboot-native-native:build

运行成功

  1. Produced artifacts:
  2. D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target\boot3-16-aot-springboot.exe (executable)
  3. ========================================================================================================================
  4. Finished generating 'boot3-16-aot-springboot' in 1m 28s.
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] BUILD SUCCESS
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] Total time: 01:33 min
  9. [INFO] Finished at: 2024-01-12T10:13:44+08:00
  10. [INFO] ------------------------------------------------------------------------

测试

  • cmd 进入目录 D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target

  • 输入 boot3-16-aot-springboot.exe

  1. D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target>boot3-16-aot-springboot.exe
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v3.2.1)
  9. %PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]

使用 Maven 命令生成

  • 点击 Maven-boot3-16-aot-springboot-生命周期-clear
  • 点击 Maven-boot3-16-aot-springboot-生命周期-compile

  • 点击 Maven-boot3-16-aot-springboot-插件-spring-boot-spring-boot:process-aot

  • 生成了下面两个文件夹

  • 打开工具 x64 Native Tools Command Prompt for VS 2022

  • 进行项目目录 D:\data\SpringBoot3-Study\boot3-16-aot-springboot>

  • 运行 native 打包

  1. # 推荐加上 -Pnative
  2. mvn -Pnative native:build -f pom.xml
  3. mvn native:build -f pom.xml

打包成功

  1. Produced artifacts:
  2. D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target\boot3-16-aot-springboot.exe (executable)
  3. ========================================================================================================================
  4. Finished generating 'boot3-16-aot-springboot' in 1m 29s.
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] BUILD SUCCESS
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] Total time: 01:34 min
  9. [INFO] Finished at: 2024-01-12T10:23:29+08:00
  10. [INFO] ------------------------------------------------------------------------

测试

  • cmd 进入目录 D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target

  • 输入 boot3-16-aot-springboot.exe

  1. D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target>boot3-16-aot-springboot.exe
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v3.2.1)
  9. 2024-01-12T10:24:02.354+08:00 INFO 1756 --- [ main] c.a.b.a.Boot316AotSpringbootApplication : Starting AOT-processed Boot316AotSpringbootApplication using Java 17.0.9 with PID 1756 (D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target\boot3-16-aot-springboot.exe started by v_hwhao in D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target)
  10. 2024-01-12T10:24:02.355+08:00 INFO 1756 --- [ main] c.a.b.a.Boot316AotSpringbootApplication : No active profile set, falling back to 1 default profile: "default"
  11. 2024-01-12T10:24:02.429+08:00 INFO 1756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
  12. 2024-01-12T10:24:02.430+08:00 INFO 1756 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2024-01-12T10:24:02.431+08:00 INFO 1756 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.17]
  14. 2024-01-12T10:24:02.445+08:00 INFO 1756 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  15. 2024-01-12T10:24:02.445+08:00 INFO 1756 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 90 ms
  16. 2024-01-12T10:24:02.476+08:00 INFO 1756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
  17. 2024-01-12T10:24:02.476+08:00 INFO 1756 --- [ main] c.a.b.a.Boot316AotSpringbootApplication : Started Boot316AotSpringbootApplication in 0.133 seconds (process running for 0.139)