According to the official documentation, there are two ways to use servlet, filter, or listener in your spring boot app.
One: Register a servlet, filter, or listener as a spring Bean.
Attention:
Because these three are related to containers, they are not delayed to initialize! So, in general, you can't inject other spring beans--but not that you can't get them, you can use some means to inject them before you call them!
Servlets, filter can use Servletregistrationbean, Filterregistrationbean to set initialization parameters and mapping paths.
By default, if you do not specify a filter's Dispatchertype, the forward, include, and request are matched. If async is enabled, it will also match async.
If you want to use only beans, and you don't want to install them in the container, registration.setenabled (false).
Second: Use classpath scanning, specifically using @servletcomponentscan on the @configuration class.
Attention:
Need to be used in conjunction with @webservlet, @WebFilter or @weblistener;
The default is to scan the current configuration file for the package and the child package.
Ps:
If it is a spring MVC project, and it is Java Config, it can be selected in the first way;
or set filter in the Webapplicationinitializer implementation class-There should be a way to add multiple filter, but only for dispatcherservlet!
Alternatively, you can get ServletContext in the Onstartup () method of the Webapplicationinitializer implementation class and register your own servlet, filter, or listener.
It is also necessary to add that this is a feature supported by the servlet 3.0+.
Spring Boot uses a servlet, filter, or listener method