2021年3月26日星期五

Spring 5 WebApplicationInitializer not initialised when using Java 16

I have a very simple Gradle (7.0-rc-1) script to initialise a single Spring 5 "hello world" endpoint using an embedded Tomcat instance. The original code is taken from a random example I found on the internet. My example Gradle project can be found here.

I'm not sure how it works but somehow the web server knows to call the WebApplicationInitializer.onStartup(ServletContext) method on startup so that Spring is intialised.

This works correctly on Java 8; but fails when I change the following toolchain specification in the Gradle build definition to Java 16.

java {      toolchain {  //        languageVersion = JavaLanguageVersion.of(16)          languageVersion = JavaLanguageVersion.of(8)      }  }  

When using Java 8, Spring is intialised correctly and the endpoint works.

When using Java 16, The onStartup() method is not called, so Spring is not initialised and the endpoint doesn't work (though Tomcat is still started and responds with an error).

The logging shows a message like, there is no stacktrace though:

INFO: No Spring WebApplicationInitializer types detected on classpath  

What do I need to do to make this work on Java 16?

Note that I don't want to use spring-boot, please don't suggest it.

https://stackoverflow.com/questions/66826051/spring-5-webapplicationinitializer-not-initialised-when-using-java-16 March 27, 2021 at 08:55AM

没有评论:

发表评论