Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field injection through @Resource does not work after AOT processing #30796

Closed
okboyone opened this issue Jul 3, 2023 · 6 comments
Closed

Field injection through @Resource does not work after AOT processing #30796

okboyone opened this issue Jul 3, 2023 · 6 comments
Labels
status: duplicate A duplicate of another issue theme: aot An issue related to Ahead-of-time processing

Comments

@okboyone
Copy link

okboyone commented Jul 3, 2023

2023-07-03T14:33:26.923+08:00  WARN 20580 --- [           main] w.s.c.ServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSocketHandlerMapping': Unsatisfied dependency expressed through method 'webSocketHandlerMapping' parameter 0: Error creating bean with name 'defaultSockJsSchedulerContainer': Instantiation of supplied bean failed
2023-07-03T14:33:26.924+08:00  INFO 20580 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-07-03T14:33:26.924+08:00 ERROR 20580 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSocketHandlerMapping': Unsatisfied dependency expressed through method 'webSocketHandlerMapping' parameter 0: Error creating bean with name 'defaultSockJsSchedulerContainer': Instantiation of supplied bean failed
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArgument(BeanInstanceSupplier.java:315) ~[na:na]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArguments(BeanInstanceSupplier.java:258) ~[na:na]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:198) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:947) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1214) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1158) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[boot-test.exe:6.0.9]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[boot-test.exe:6.0.9]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[boot-test.exe:6.0.9]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[boot-test.exe:3.1.1]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[boot-test.exe:3.1.1]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[boot-test.exe:3.1.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[boot-test.exe:3.1.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[boot-test.exe:3.1.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[boot-test.exe:3.1.1]
        at com.benfei.ChangAnRefuelApplication.main(ChangAnRefuelApplication.java:11) ~[boot-test.exe:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultSockJsSchedulerContainer': Instantiation of supplied bean failed
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1220) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1158) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:888) ~[na:na]
        at org.springframework.beans.factory.support.RegisteredBean.resolveAutowiredArgument(RegisteredBean.java:219) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArgument(BeanInstanceSupplier.java:312) ~[na:na]
        ... 21 common frames omitted
Caused by: java.lang.IllegalArgumentException: WebSocketHandler must not be null
        at org.springframework.util.Assert.notNull(Assert.java:204) ~[na:na]
        at org.springframework.web.socket.config.annotation.AbstractWebSocketHandlerRegistration.addHandler(AbstractWebSocketHandlerRegistration.java:65) ~[boot-test.exe:6.0.9]
        at org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistry.addHandler(ServletWebSocketHandlerRegistry.java:59) ~[na:na]
        at com.benfei.web.socket.WebSocketConfig.registerWebSocketHandlers(WebSocketConfig.java:19) ~[boot-test.exe:na]
        at org.springframework.web.socket.config.annotation.DelegatingWebSocketConfiguration.registerWebSocketHandlers(DelegatingWebSocketConfiguration.java:52) ~[boot-test.exe:6.0.9]
        at org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport.initHandlerRegistry(WebSocketConfigurationSupport.java:58) ~[boot-test.exe:6.0.9]
        at org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport.defaultSockJsSchedulerContainer(WebSocketConfigurationSupport.java:87) ~[boot-test.exe:6.0.9]
        at org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport__BeanDefinitions.lambda$getDefaultSockJsSchedulerContainerInstanceSupplier$1(WebSocketConfigurationSupport__BeanDefinitions.java:37) ~[na:na]
        at org.springframework.util.function.ThrowingFunction.apply(ThrowingFunction.java:63) ~[boot-test.exe:6.0.9]
        at org.springframework.util.function.ThrowingFunction.apply(ThrowingFunction.java:51) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$withGenerator$0(BeanInstanceSupplier.java:167) ~[na:na]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68) ~[boot-test.exe:6.0.9]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$get$2(BeanInstanceSupplier.java:200) ~[na:na]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[boot-test.exe:6.0.9]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.invokeBeanSupplier(BeanInstanceSupplier.java:212) ~[na:na]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:200) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:947) ~[boot-test.exe:6.0.9]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1214) ~[boot-test.exe:6.0.9]
        ... 34 common frames omitted
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 3, 2023
@wilkinsona
Copy link
Member

Thanks for the report. Unfortunately, a stack trace alone isn't enough to diagnose the problem. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jul 3, 2023
@okboyone
Copy link
Author

okboyone commented Jul 3, 2023

test.zip
Thank you very much. Our team has been experimenting extensively with the combination of springboot 3+graalvm in the industrial field for several months now

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 3, 2023
@okboyone
Copy link
Author

okboyone commented Jul 3, 2023

test.zip Thank you very much. Our team has been experimenting extensively with the combination of springboot 3+graalvm in the industrial field for several months now

I used the build method provided in the official documentation and used the mvn -Pnative native:compile command to build it

@wilkinsona
Copy link
Member

Thanks for the sample, @okboyone. I've reproduced the problem. It also occurs on the JVM when the app has undergone AOT processing. You can work around it by using @Autowired instead of @Resource to inject MyWsHandler into WebSocketConfig. I'll transfer this issue to the Spring Framework team as they may want to investigate why @Resource does not work.

Generally speaking, we don't recommend the use of field injection so you may want to update WebSocketConfig to use constructor injection irrespective of any problems related to AOT.

@wilkinsona wilkinsona changed the title I used websocket in my sprintboot 3.1.1 project and reported an error when building with native-image Field injection through @Resource does not work after AOT processing Jul 3, 2023
@wilkinsona wilkinsona removed the status: feedback-provided Feedback has been provided label Jul 3, 2023
@bclozel bclozel transferred this issue from spring-projects/spring-boot Jul 3, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Jul 3, 2023

Looks like a duplicate of #29614 as far as I can tell.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Jul 3, 2023
@sdeleuze sdeleuze self-assigned this Jul 3, 2023
@snicoll

This comment was marked as outdated.

@sdeleuze sdeleuze added theme: aot An issue related to Ahead-of-time processing and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 3, 2023
@sdeleuze sdeleuze removed their assignment Jul 3, 2023
@sdeleuze sdeleuze added the status: duplicate A duplicate of another issue label Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue theme: aot An issue related to Ahead-of-time processing
Projects
None yet
Development

No branches or pull requests

5 participants