想知道Spring的新特性吗?那就进来看看吧

Spring6.0新特性

一、Spring的发展历史

image.png

二、AOT

  AOT是Spring6.0提供的一个新特性,Ahead of Time 提前编译。

image.png

1.AOT概述

1.1 JIT和AOT的关系

1.1.1 JIT

   JIT(Just-in-time) 动态编译,即时编译,也就是边运行边编译,也就是在程序运行时,动态生成代码,启动比较慢,编译时需要占用运行时的资源。

1.1.2 AOT

  AOT,Ahead Of Time 指的是运行前编译,预先编译,AOT 编译能直接将源代码转化为机器码,内存占用低,启动速度快,可以无需 runtime 运行,直接将 runtime 静态链接至最终的程序中,但是无运行时性能加成,不能根据程序运行情况做进一步的优化,AOT 缺点就是在程序运行前编译会使程序安装的时间增加。

简单来讲:JIT即时编译的是在程序的运行过程中,将字节码转换为可在硬件上直接运行的机器码,并部署至托管环境中的过程。而 AOT 编译指的则是,在程序运行之前,便将字节码转换为机器码的过程。

image.png

三、GraalVM

GraalVM即支持AOT也支持JIT。支持多种开发语言。

  Spring6 支持的 AOT 技术,这个 GraalVM 就是底层的支持,Spring 也对 GraalVM 本机映像提供了一流的支持。GraalVM 是一种高性能 JDK,旨在加速用 Java 和其他 JVM 语言编写的应用程序的执行,同时还为 JavaScript、Python 和许多其他流行语言提供运行时。 GraalVM 提供两种运行 Java 应用程序的方法:在 HotSpot JVM 上使用 Graal 即时 (JIT) 编译器或作为提前 (AOT) 编译的本机可执行文件。 GraalVM 的多语言能力使得在单个应用程序中混合多种编程语言成为可能,同时消除了外语调用成本。GraalVM 向 HotSpot Java 虚拟机添加了一个用 Java 编写的高级即时 (JIT) 优化编译器。

GraalVM 具有以下特性:

(1)一种高级优化编译器,它生成更快、更精简的代码,需要更少的计算资源

(2)AOT 本机图像编译提前将 Java 应用程序编译为本机二进制文件,立即启动,无需预热即可实现最高性能

(3)Polyglot 编程在单个应用程序中利用流行语言的最佳功能和库,无需额外开销

(4)高级工具在 Java 和多种语言中调试、监视、分析和优化资源消耗

1.GraalVM安装

1.1 下载GraalVM

下载地址:https://www.graalvm.org/downloads/image.png

下载社区版本即可,点击进入选择相关的版本:https://github.com/graalvm/graalvm-ce-builds/releases

image.png

下载好后解压缩出来

image.png

1.2 配置环境变量

添加:GRAALVM_HOME

编辑用户变量

image.png

把JAVA_HOME修改为graalvm的位置

image.png

检查是否配置成功

image.png

1.3 安装native-image插件

使用命令 gu install native-image 下载安装插件,因为社区版默认不提供支持。需要手动下载

image.png

image.png

1.4 Native Image

  Native image(本地镜像)是一种在Java平台上构建本地应用程序的技术。它将Java应用程序编译成本地机器代码,以便在不需要Java虚拟机(JVM)的情况下运行。这使得应用程序可以更快地启动,更高效地执行,并且占用更少的内存。

  Native image使用GraalVM编译器技术,可以将Java应用程序转换为本地可执行文件,支持Windows、Linux和MacOS等多个操作系统平台。此外,Native image还可以将Java应用程序打包成单个可执行文件,从而方便部署和分发。

  使用Native image,开发人员可以将Java应用程序作为本地应用程序来构建和部署,从而获得更好的性能和更好的用户体验。

2.安装C++的编译环境

2.1 下载Visual Studio

https://visualstudio.microsoft.com/zh-hans/downloads/

image.png

同样我们下载社区版本即可

2.2 安装Visual Studio

下载后双击直接安装即可

image.png

等待在线下载

image.png

image.png

image.png

注意安装选项,然后继续等待

image.png

创建一个普通Hello.java文件

public class Hello{
    
    

	public static void main(String[] args){
    
    
		System.out.println("Hello World ...");
	}
}

然后通过 javac Hello.java 编译

image.png

通过native-image Hello 执行

image.png

通过 native-image 生成了 Hello.exe 文件,我们就可以直接生成了。

image.png

四、SpringBoot实战

  我们同样可以在SpringBoot项目中通过AOT来提前编译我们的项目,新建一个Maven项目。然后添加相关的依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

同时我们还需要添加相关的SpringBoot插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

然后我们编写一点简单的代码测试即可

image.png

然后我们打开 x64 Native Tools Command Prompt for VS 2019 。然后我们需要切换到工程目录下

image.png

然后执行 mvn -Pnative native:compile 进行编译就可以了,编译成功就会在target目录下生成 EXE 文件。后续执行该文件就可以了

image.png

image.png

编译成功

image.png

然后我们双击执行exe文件即可。你会发现速度会快很多

image.png

五、RuntimeHints

  与常规 JVM 运行时相比,将应用程序作为本机映像运行需要额外的信息。例如,GraalVM 需要提前知道组件是否使用反射。同样,除非明确指定,否则类路径资源不会在本机映像中提供。因此,如果应用程序需要加载资源,则必须从相应的 GraalVM 原生图像配置文件中引用它。

APIRuntimeHints在运行时收集反射、资源加载、序列化和 JDK 代理的需求。

1.案例分析

声明个普通的实体类型

public class UserEntity {
    
    
    public String hello(){
    
    
        return "hello ...";
    }
}

然后我们在控制器中通过反射来操作处理

    @GetMapping("/hello")
    public String hello(){
    
    
        String res = "hello";
        try {
    
    
            Method hello = UserEntity.class.getMethod("hello");
            res =  (String)hello.invoke(UserEntity.class.newInstance(),null);
        } catch (NoSuchMethodException e) {
    
    
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
    
    
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
    
    
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
    
    
            throw new RuntimeException(e);
        }
        return res;
    }

然后通过命令编译为 exe 文件

image.png

运行exe文件后。我们通过浏览器发起请求

image.png

在HelloController中。我们通过反射的方式使用到了UserEntity的无参构造方法。如果不做任何处理。那么打成二进制可执行文件后是执行不了的。上面是具体的报错信息。针对这种情况。我们可以通过 Runtime Hints 机制来处理。

2. RuntimeHintsRegistrar

官网提供的解决方案。我们自定义一个RuntimeHintsRegistrar接口的实现类,然后把该实现类注入到Spring中

image.png

我们自己的实现

@RestController
@ImportRuntimeHints(HelloController.UserEntityRuntimeHints.class)
public class HelloController {
    
    

    @GetMapping("/hello")
    public String hello(){
    
    
        String res = "hello";
        try {
    
    
            Method hello = UserEntity.class.getMethod("hello");
            res =  (String)hello.invoke(UserEntity.class.newInstance(),null);
        } catch (NoSuchMethodException e) {
    
    
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
    
    
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
    
    
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
    
    
            throw new RuntimeException(e);
        }
        return res;
    }

    static class UserEntityRuntimeHints implements RuntimeHintsRegistrar{
    
    

        @Override
        public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
    
    
            try {
    
    
                hints.reflection().registerConstructor(UserEntity.class.getConstructor(), ExecutableMode.INVOKE);
            } catch (NoSuchMethodException e) {
    
    
                throw new RuntimeException(e);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/jbjmh/article/details/142371664