with Spring Boot 3 release there is lots of advertisement about GraalVM and native-images, so I decided experiment little bit..
With Spring Boot Initializer I quickly created a really simple gradle project (one single rest service that is doing nothing special). With the help of the gradle 'native-image' and JiB plugin, I could quickly get a running state with Spring Boot Application as a normal Java Applicaton and native-image Application (and seen the drastic startup time changes).
While JiB created a Docker Image for me, I tried to deploy the SB3 application to my minikube, there I got the first surprise, while I was working with MacOS M1, the image that I created (based on alpine) was complaining the wrong 'exec format' after little bit reading, I learned while my local GraalVM is MacOS based aarch64 it would not run in alpine and I have to create cross platform image and I have to create Docker images based on destination platform GraalVM (in this case linux/alpine) which I did, I bypassed the original problem and Spring Boot 3 application starts but encounters following problem in the stacktrace.
16:43:45.808 [main] ERROR org.springframework.boot.SpringApplication - Application
run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'resourceHandlerMapping': Instantiation of supplied bean failed
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainInstan ceFromSupplier(AbstractAutowireCapableBeanFactory.java:1236)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1157)
Caused by: java.lang.IllegalStateException: No ServletContext set
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:581)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport__BeanDefinitions.lambda$getResourceHandlerMappingInstanceSupplier$7(WebMvcConfigurationSupport__BeanDefinitions.java:166)
at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68)
at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54)
Now I tried to look as much as possible what 'native-image' plugin does (while native-image and Spring Boot 3 application runs on my Mac Book), I can't figure out what is the Delta. I think it has to do with 'reflection-config'. 'native-image' plugin created a 'reflect-config.json' and it contains the following lines.
{
"name": "resourceHandlerMapping",
"parameterTypes": [
"org.springframework.web.accept.ContentNegotiationManager",
"org.springframework.format.support.FormattingConversionService",
"org.springframework.web.servlet.resource.ResourceUrlProvider"
]
}
I think should be relevant for the problem, but this file is in the classpath and it is not changing the outcome(I can also see that 'WebMvcConfigurationSupport__BeanDefinitions.java' is also generated and is in the classpath).
One thing I have to say, while the shear number of dependencies Spring Boot needs, I didn't place the whole dependencies in the classpath but I created a fat jar (shadowJar plugin), which includes all my code and dependencies, may be this causing the problem.
'native-image' in my cross platform docker image, is able to create the application with this fat jar but it throws the above mentioned exception.
Anyway, can anybody give any tips to solve this problem.
Or how can diagnose such a problem in GraalVM and native-image?