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

SpringBoot/Tomcat fails to start when a @WebListener has a constructor with an argument #25059

Closed
bencalegari opened this issue Jan 30, 2021 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@bencalegari
Copy link

bencalegari commented Jan 30, 2021

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 30, 2021
@philwebb
Copy link
Member

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.

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Jan 30, 2021
@bencalegari
Copy link
Author

Thanks! That fixed it, both in the sample app and ours. Is there a reason why constructor injection in @WebListeners should've been avoided at the outset?

@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 Jan 30, 2021
@philwebb
Copy link
Member

Usually the lifecycle of @WebListeners is handled directly by the Servlet Containers and as such they don't know anything about Spring.

@philwebb philwebb added status: invalid An issue that we don't feel is valid and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Jan 31, 2021
@wilkinsona
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants