使用 Spring Boot 和 GraalVM 实现原生(系列二)
Your first Spring Native application: JPA, Spring MVC, and H2
开始使用 Spring Native 就像开始使用 Spring 一样:转到 Spring Initializr。点击cmd + B或Ctrl + B或单击添加依赖项并选择Spring Native。
Spring Initializr 配置 Apache Maven 和 Gradle 构建。然后,只需添加必要的依赖项。让我们从一些典型的事情开始。将项目名称更改为 jpa。接下来,添加以下依赖项:和 。确保指定 Java 17。你可以使用Java 11,就像你可以挥舞着橡胶鸡跑圈一样。但是你看起来很傻,不是吗?单击生成。解压缩项目并将项目导入到您喜欢的 IDE 中。Spring Native, Spring Web, Lombok, H2 Database
Spring Data JPA
这个例子是一个微不足道但典型的例子。将类更改为如下所示:JpaApplication.java
package com.example.jpa;
import lombok.*;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import javax.persistence.*;
import java.util.Collection;
import java.util.stream.Stream;
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
@Component
record Initializr(CustomerRepository repository)
implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
Stream.of("A", "B", "C", "D")
.map(c -> new Customer(null, c))
.map(this.repository::save)
.forEach(System.out::println);
}
}
@RestController
record CustomerRestController(CustomerRepository repository) {
@GetMapping("/customers")
Collection<Customer> customers() {
return this.repository.findAll();
}
}
interface CustomerRepository extends JpaRepository<Customer, Integer> {
}
@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Table (name = "customer")
class Customer {
@Id
@GeneratedValue
private Integer id;
private String name;
}
也可以将测试作为本机可执行文件进行编译和运行。请注意,有些东西,比如Mockito,在那里还不是特别好。将测试 更改为如下所示:JpaApplicationTests.java
package com.example.jpa;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.Assert;
@SpringBootTest
class JpaApplicationTests {
private final CustomerRepository customerRepository;
@Autowired
JpaApplicationTests(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
@Test
void contextLoads() {
var size = this.customerRepository.findAll().size();
Assert.isTrue(size > 0, () -> "there should be more than one result!");
}
}
您可以按照常规方式在 JRE 上运行应用程序和测试,例如从终端运行。运行示例是一个好主意,原因无非是为了确保应用程序正常工作。但这不是我们在这里的原因。相反,我们希望将应用程序及其测试编译为 GraalVM 本机应用程序。mvn spring-boot:run
如果您已经检查过pom.xml,那么您已经注意到了设置GraalVM本机映像并添加Maven配置文件(称为)以支持构建本机可执行文件的广泛额外配置。您可以像往常一样使用 编译应用程序。使用 以本机方式编译应用程序。请记住,您需要在此处将 GraalVM 设置为您的 JDK!这个过程需要几分钟,所以现在是喝杯茶、咖啡、水或其他任何东西的好时机。我做了。我需要它。当我回来时,我在输出中看到了这一点:native
mvn
clean package
mvn -Pnative clean package
...
13.9s (16.9% of total time) in 71 GCs | Peak RSS: 10.25GB | CPU load: 5.66
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:00 min
[INFO] Finished at: 2022-04-28T17:57:56-07:00
[INFO] ------------------------------------------------------------------------
- 点赞
- 收藏
- 关注作者
评论(0)