简体   繁体   English

多个Spring WebApplicationInitializer

[英]Multiple Spring WebApplicationInitializer

While working on a spring project I realized that I had three classes that were all in effect implementing WebApplicationInitializer 在进行春季项目时,我意识到我拥有三个有效实现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. 如果没有,诸如addInterceptors或ResourceResolvers之类的任何方法的主体,最终都会从其他方法中清除设置。

So Thanks to M.Deinum's comment as it led me to the answer. 因此,感谢M.Deinum的评论,它使我找到了答案。 After playing around the @Order tag is the mechanism by which you control the order in which they load. 在@Order标签周围玩耍之后,您可以通过这种机制来控制它们的加载顺序。 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. 如果您阅读了AbstractHttpSessionApplicationInitializer的文档,它指出必须在其他注册过滤器的初始化程序之前对其进行排序。 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. 它还默认将其Order设置为100。必须首先使用它的原因是它使用过滤器从缓存数据库中检索会话。 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. 例如,如果您将AbstractSecurityWebApplicationInitializer设置为50,则切换服务器时将不再进行身份验证,因为spring安全过滤器将在从数据库获取会话的过滤器之前执行。

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. 另一方面,我删除了一个让Spring-Tomcat Web应用程序java.lang.IllegalStateException出现的错误,该错误是通过使我的主初始化器AnnotationWebAppInitializer优先于其他实现而解决的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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