spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Gateway Server MVC support for AOT

Open wisskirchenj opened this issue 1 year ago • 4 comments

EDIT: Let me add, that this "bug" (or better use of an for AOT unsupported bean definition method) presently prevents ANY Spring Boot app using spring-cloud-gateway-server-mvc from being run as a native image - as any will need to instantiate the beans during AutoConfiguration...

Describe the bug Spring Cloud Gateway 4.1.0 release (2023.0.0 train) based Spring Boot app - using new gateway-server-mvc - with minimal path-predicate routing to webservice apps, CRASHES in Gradle's processAot task on application startup with (shortened) stack-trace as shown below. Running the app (JVM w/o AOT) works fine. Same in integration testing ("solved" with @DisabledInAotMode)

Exception in thread "main" java.lang.IllegalStateException: Default code generation is not supported for bean definitions declaring an instance supplier callback: Root bean: class [org.springframework.cloud.gateway.server.mvc.config.GatewayMvcPropertiesBeanDefinitionRegistrar$RouterFunctionHolder]; scope=refresh; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null at org.springframework.beans.factory.aot.DefaultBeanRegistrationCodeFragments.getTarget(DefaultBeanRegistrationCodeFragments.java:78) at org.springframework.beans.factory.aot.BeanDefinitionMethodGenerator.generateBeanDefinitionMethod(BeanDefinitionMethodGenerator.java:85) at org.springframework.beans.factory.aot.BeanRegistrationsAotContribution.lambda$generateRegisterBeanDefinitionsMethod$2(BeanRegistrationsAotContribution.java:90)

Sample Code for above mentioned app is publicly accessible at https://github.com/wisskirchenj/gateway/tree/spring-issue-ref, where I stripped off all non-essential features in this branch for reference.

wisskirchenj avatar Dec 10 '23 14:12 wisskirchenj

I managed to workaround - so I can run my Gateway app as native image again - by

  • replacing registerBeanDefinition callback with a factoryMethod
  • defining a bean for the Registrar class in the AutoConfiguration
  • not setting refresh scope on the RouterFunctionHolder bean (should be configurable to support spring cloud config in real project of course) You can see my changes here: https://github.com/wisskirchenj/gateway/tree/spring-issue-workaround/src/main/java

I can offer to improve on this solution (configure the requestScope toggle) and send a Pull Request - if there is interest.

wisskirchenj avatar Dec 11 '23 00:12 wisskirchenj

A PR would be most welcome!

/cc @joshlong

spencergibb avatar Dec 20 '23 00:12 spencergibb

I love this !

joshlong avatar Dec 20 '23 05:12 joshlong

@spencergibb @joshlong Just added my announced PR https://github.com/spring-cloud/spring-cloud-gateway/pull/3193. I am happy to elaborate on your review comments and also add test(s) if you need. Just instruct me :-)

wisskirchenj avatar Dec 23 '23 08:12 wisskirchenj