Spring Boot Version: 2.4.2
Here's an abbreviated ./gradlew bootRun
log from a sample app I created to replicate the issue:
2021-01-30 18:31:03.430 DEBUG 2609 --- [ main] o.apache.catalina.core.StandardContext : Configuring application event listeners
2021-01-30 18:31:03.430 DEBUG 2609 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Configuring event listener class 'com.example.springbootlistenerbug.Listener'
2021-01-30 18:31:03.431 ERROR 2609 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Error configuring application listener of class [com.example.springbootlistenerbug.Listener]
java.lang.NoSuchMethodException: com.example.springbootlistenerbug.Listener.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3427) ~[na:na]
at java.base/java.lang.Class.getConstructor(Class.java:2165) ~[na:na]
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:151) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4640) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5177) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
You can find the entirety of the log in this Github action from the repo.
We noticed this issue in our application when attempting to upgrade from 2.3.5.RELEASE to 2.4.2. In our case, the arguments passed to the class annotated with @WebListener
are @Component
classes, but in the example repo I was able to reproduce the error with or without that annotation. Spring starts just fine when you remove the argument to the constructor.
I configured the app to use the Spring Boot 2.5 snapshot and got the same error, for what it's worth.
Comment From: philwebb
This is probably related to #18303
I'm a little surprised that @WebListener
constructor injection worked in 2.4, it was probably by accident rather than design. @bencalegari If you change your SessionListener
class so that it's annotated with @Component
and not @WebListener
do things work as expected? All HttpSessionListener
beans in the context should be automatically added to the servlet container.
Comment From: bencalegari
Thanks! That fixed it, both in the sample app and ours. Is there a reason why constructor injection in @WebListener
s should've been avoided at the outset?
Comment From: philwebb
Usually the lifecycle of @WebListeners
is handled directly by the Servlet Containers and as such they don't know anything about Spring.
Comment From: wilkinsona
It's a requirement of the Servlet spec that listeners, filters, and servlets must have a zero-args constructor. This applies whether they're registered via web.xml
or the various annotations.