20 Şubat 2023 Pazartesi

Spring 3 İçin SpringNative - GraalVM

Giriş
Spring 3 için açıklaması şöyle
It started as an experimental module called Spring Native, to be added to Spring Boot 2.7 apps. But with Spring Boot 3.0, GraalVM support has been brought into the portfolio projects themselves.

No Spring Native project needed.
Şöyle yaparız
For AOT generation, there's no need to include separate plugins, we can just use a new goal of the spring-boot-maven-plugin

mvn spring-boot:aot-generateCopy
1. GraalVM kurulur
2. Projeye GraalVM Native Support eklenir
3. native-maven-plugin eklenir
Örnek
Şöyle yaparız
<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.graalvm.buildtools</groupId>
      <artifactId>native-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
Örnek
Tüm pom.xml şöyle. native-maven-plugin bazen farklı bir profile'a da ekleniyor. 
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.1</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>17</java.version>
  </properties>
  <dependencies>
    ...
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.hibernate.orm.tooling</groupId>
	<artifactId>hibernate-enhance-maven-plugin</artifactId>
	<version>${hibernate.version}</version>
	<executions>
	  <execution>
	    <id>enhance</id>
	      <goals>
	        <goal>enhance</goal>
	      </goals>
	      <configuration>
	        <enableLazyInitialization>true</enableLazyInitialization>
		<enableDirtyTracking>true</enableDirtyTracking>
		<enableAssociationManagement>true</enableAssociationManagement>
	      </configuration>
	    </execution>
	  </executions>
	</plugin>
      <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>
</project>
4. mvn clean package -Pnative çalıştırılır. Açıklaması şöyle. AOT kısaltması Ahead-of-Time anlamına gelir.
Maven has three different goals for AOT processing and building the image:

mvn spring-boot:process-aot

mvn spring-boot:process-test-aot

mvn spring-boot:build-image

But these three commands are combined in the mvn clean package -Pnative.

Quick tip: the profile native is predefined in Spring Boot 3 for the native image creation. The same applies to the profile nativeTest as a testing profile.
Örneğin ./target/demo diye bir dosya oluşur. Bu çalıştırılır
veya şöyle yapılir
./mvnw -Pnative native:compile

./target/<native-executable>
RuntimeHints
@ImportRuntimeHints Anotasyonu yazısına taşıdım

@RegisterReflectionForBinding Anotasyonu

AOT
Şöyle yaparız
$ java -Dspring.aot.enabled=true -jar ldap-service.jar



Hiç yorum yok:

Yorum Gönder