简体   繁体   中英

Multiple Spring WebApplicationInitializer

While working on a spring project I realized that I had three classes that were all in effect implementing WebApplicationInitializer

public class SpringSessionInitializer extends AbstractHttpSessionApplicationInitializer

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer 

public class AnnotationWebAppInitializer implements WebApplicationInitializer 

After looking at the three uses I saw that there was not a way for me to combine them so I had a few questions.

  1. Is the order in which these will execute consistent?
  2. If so is there a way to order them?
  3. If not will the body of any of the methods such as addInterceptors or ResourceResolvers, end up wiping out the settings from the others.

So Thanks to M.Deinum's comment as it led me to the answer. After playing around the @Order tag is the mechanism by which you control the order in which they load. The second part however is that yes the Order does matter. If you read the documentation of AbstractHttpSessionApplicationInitializer it states that it must me ordered before any other initializers that register filters. It also defaults its Order to 100. The reason it has to go first is it uses filters to retrieve the session from the cache database. If for example you order your AbstractSecurityWebApplicationInitializer to 50 you will no longer authenticate when switching servers because the spring security filters will execute before the filters that get the session from the database.

On the other side of this I removed a bug I was getting Spring-Tomcat Web Application java.lang.IllegalStateException , was solved by making my main initializer AnnotationWebAppInitializer go before the others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM