Let’s say you find yourself modernizing a legacy Java web application, not just the UI libraries but also the middleware. And let’s say that Spring Boot was the selected framework to get the job done because of its many advantages:
- Allows teams getting started quickly reducing boilerplate code.
- Provides starter projects which extremely reduces Maven configuration, especially dependencies management.
- Production ready actuator endpoints reporting metrics, health check.
- Provides opiniated autoconfiguration out of the box flexible enough to be overriden when needed.
- Seamless integration with the Spring ecosystem.
How can you add a Filter, a Servlet, and a Listener to a Spring Boot application now that it lacks the
web.xml file descriptor? This post answers this question also covering injecting dependencies to a Filter, Servlet, or Listener.
- Java 7+.
- Maven 3.2+.
- Familiarity with Spring Framework.
3. APPLICATION DEPENDENCIES
spring-boot-starter-web transitively includes dependencies required to implement a Filter, Servlet or Listener.
spring-boot-maven-plugin allows Spring Boot applications to be packages as a jar or war archive and it from command line using Maven or Gradle. Visit the spring-boot-maven-plugin page to learn more.
4.1. IMPLEMENTING A SERVLET
A very simple Servlet.
4.2. REGISTERING A SERVLET
Let’s register it using Spring Boot’s ServletRegistrationBean:
Spring Boot beans are instantiated in @Configuration-annotated classes. If more than one servlet is going to be registered then the name for each ServletRegistrationBean bean would have to be explicitly set in @Bean annotations.
5.1. IMPLEMENTING A FILTER
An ordinary Servlet Filter.
5.2. REGISTERING A FILTER
Let’s now register a couple of Filters using Spring Boot’s FilterRegistrationBean:
Again, if more than one filter is going to be registered then the name for each FilterRegistrationBean bean would have to be explicitly set in @Bean annotations. Also notice that similar to how they could be configured in
web.xml, Filters could be mapped to url patterns as well as to servlets in Spring Boot web applications.
6.1. IMPLEMENTING A LISTENER
Another simple Servlet Listener implementation.
6.2. REGISTERING A LISTENER
A Java web application Servlet Listener could be registered using Spring Boot’s ServletListenerRegistrationBean:
As a reminder, if more than one listener is going to be registered then the name for each ServletListenerRegistrationBean bean would have to be set in each listener @Bean annotation.
Also worth mentioning if you need to use a common set of Filters, Servlets and / or Listeners in multiple Spring Boot applications, you could pack them in their own custom Spring Boot starter and include it in Spring Boot applications with minimal configuration.
Thanks for reading and as always, feedback is very much appreciated. If you found this post helpful and would like to receive updates when content like this gets published, sign up to the newsletter.