Spring Native Beta Release

The Spring porting team for GraalVM recently released their first major release, the Spring Native Beta. Together with the creators of GraalVM, they were able to fix many bugs both in the compiler itself and in the spring. Now the project has official support, its own release cycle and you can feel it.






The most important obstacle when porting code from JVM to binaries is the problem of using chips inherent only in java - reflection, working with the classpath, dynamically loading classes, etc. 





According to the documentation, the key differences between a regular JVM and a native implementation are as follows:





  • Static analysis of the entire application is performed at build time.





  • Unused components are removed during assembly.





  • Reflection, resources and dynamic proxies can only be configured with additional configurations.





  • At build time, all components are fixed in the Classpath.





  • No lazy loading of the class: on load everything that comes in the executables will be loaded into memory. For example, for the call to Class.forName ("myClass") to work correctly, you need to have myClass in the configuration file. If no class is found in the configuration file that is requested to dynamically load the class, a ClassNotFoundException will be thrown





  • Some of the code will be run at build time to properly link the components. For example, tests.





, , , - .





Spring AOT, Graal VM .





Spring AOT native-image.properties



, reflection-config.json



, proxy-config.json



resource-config.json



.





Graal VM , META-INF/native-image







, Spring AOT. maven spring-aot-maven-plugin



, gradle - spring-aot-gradle-plugin.



, gradle :





plugins {id 'org.springframework.experimental.aot' version '0.9.0'}







, , .





, , . , .





, WebClient



org.springframework.nativex.hint



, :





@TypeHint(types = Data.class, typeNames = "com.example.webclient.Data$SuperHero")
@SpringBootApplication
public class WebClientApplication {
	// ...
}
      
      



, Data



, SuperHero



. , .





graavlvm , java.lang.reflect.Proxy



@ProxyHint



.





, , :





@ProxyHint(types = {
     org.hibernate.Session.class,
     org.springframework.orm.jpa.EntityManagerProxy.class
})
      
      



- ,  @ResourceHint.



, :





@ResourceHint(patterns = "com/mysql/cj/TlsSettings.properties")
      
      



/ , @InitializationHint:







@InitializationHint(types = org.h2.util.Bits.class,
								    initTime = InitializationTime.BUILD)
      
      



, @NativeHint



:





@Repeatable(NativeHints.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface NativeHint
      
      



, , :





@NativeHint(
    trigger = Driver.class,
    options = "--enable-all-security-services",
    types = @TypeHint(types = {
       FailoverConnectionUrl.class,
       FailoverDnsSrvConnectionUrl.class,
       // ...
    }), resources = {
	@ResourceHint(patterns = "com/mysql/cj/TlsSettings.properties"),
	@ResourceHint(patterns = "com.mysql.cj.LocalizedErrorMessages",
                      isBundle = true)
})
      
      



, , classpath .





Graal VM Spring AOT. 





Spring Native , start.spring.io. JPA spring , CRUD . Graal VM , BP_NATIVE_IMAGE_BUILD_ARGUMENTS



Spring AOT, Buildpacks, “<buildArgs>



” pom.xml



, native-image-maven-plugin



.





Actually, we execute the commands mvn spring-boot: build-image or gradle bootBuildImage



- and the build of the image will begin. It should be noted that the collector needs more than 7 GB of memory, for that the assembly was completed successfully. On my machine, the assembly, together with the images download, took no more than 5 minutes. At the same time, the image turned out to be very compact, only 60 MB. The application started in 0.022 seconds! This is an incredible result. Considering that more and more companies are switching to K8s and starting an application, as well as the resources used are very important in the modern world, this technology allows Spring to make the number one framework for all types of microservices, even for FaaS implementations, where cold speed is very important. start.








All Articles