1. 构建一个简单的 Spring Boot 应用

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
publicclassDemoApplication {

publicstaticvoidmain(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RestController
publicclassHelloController {

        @GetMapping("/hello")
public Stringhello() {
return "Hello, Native Image!";
        }
    }
}

2. 配置 Native Image 构建支持

在 pom.xml 中添加 Native Image 插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.graalvm.nativeimage</groupId>
            <artifactId>native-image-maven-plugin</artifactId>
            <version>${graalvm.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>native-image</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3. 生成原生镜像

运行以下 Maven 命令来生成原生镜像:

mvn package -Pnative

4. 运行原生镜像

生成的可执行文件位于 target 目录下,可以直接运行:

./target/demo

Spring Boot 3.3原生镜像支持再升级:性能爆发,内存占用显著优化!