With WebApplicationInitializer I can easily add a filter to ServletContext as part of the onStartup() method.
How to add a filter using WebMvcConfigurerAdapter ? Do I need to use XML?
ADD 1
To make it easier for other users to understand Springβs more web-friendly configuration, I draw the following illustration.
Now you need to first understand the rational web configuration behind Spring. And then select which configuration class is inherited and which method to override from below.
It is less painful to look at him than to remember so many things.

And a good article on Spring Website Initialization:
http://www.kubrynski.com/2014/01/understanding-spring-web-initialization.html
ADD 2
Based on Tunaki answer Tunaki I checked AbstractDispatcherServletInitializer . Filter registration takes place in the following code:

Even I override the green getServletFilters() method, I still cannot get the result of Dyanmic registerServletFilter() . So, how do I configure the filter on addMappingForUrlPatterns() ?
I seem have to override the entire registerDispatcherServlet() method.
source share