Java ServletContextListener 执行顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19729370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:45:10  来源:igfitidea点击:

ServletContextListener execution order

javaservletsservletcontextlistener

提问by Timur Samkharadze

How to define order of ServletContextListener's execution due application initialization, if i have multiple ServletContextListener's and some of them declared in deployment descriptor and other with annotation (@WebListener)?

如果我有多个 ServletContextListener 并且其中一些在部署描述符和其他带有注释 (@WebListener) 中声明,如何定义 ServletContextListener 执行应用程序初始化的顺序?

采纳答案by Debojit Saikia

If you want to execute listeners in a particular order, you should use deployment descriptor to define them.

如果要以特定顺序执行侦听器,则应使用部署描述符来定义它们。

Below statements are copied from Servlet Specification:

以下语句复制自Servlet 规范

8.2.3:

If the order in which the listeners, servlets, filters are invoked is important to an application then a deployment descriptor must be used. When using annotations to define the listeners, servlets and filters, the order in which they are invoked is unspecified.

The ordering will be based on the order in which they are defined in the descriptor and on the absolute-ordering element in the web.xmlor an ordering element in the web-fragment.xml.

Prior to this release of the specification (Java? Servlet Specification, version 3), context listeners were invoked in random order. As of Servlet 3.0, the listeners are invoked in the order in which they are declared in the web.xml.

Implementations of javax.servlet.ServletContextListenerare invoked at their contextInitializedmethod in the order in which they have been declared, and at their contextDestroyedmethod in reverse order.

8.2.3:

如果调用侦听器、servlet、过滤器的顺序对应用程序很重要,则必须使用部署描述符。使用注解定义侦听器、servlet 和过滤器时,未指定调用它们的顺序。

排序将基于它们在描述符中定义的顺序以及 中的绝对排序元素web.xml或 中的排序元素web-fragment.xml

在此规范发布之前(Java?Servlet 规范,版本 3),上下文侦听器以随机顺序调用。从 Servlet 3.0 开始,监听器按照它们在 web.xml 中声明的顺序被调用。

的实现按照它们被声明的顺序在javax.servlet.ServletContextListener它们的contextInitialized方法中被调用,在它们的contextDestroyed方法中以相反的顺序被调用。

If you have multiple ServletContextListeners and some of them are declared in deployment descriptor and others with annotation, then its the listeners defined in web.xmlthat will get the precedence. Below statement is copied from the same section (8.2.3) of servlet specification:

如果您有多个 ServletContextListeners 并且其中一些在部署描述符中声明,而另一些则带有注释,则其中定义的侦听器web.xml将获得优先级。以下语句复制自 servlet 规范的同一部分 (8.2.3):

Configuration specified in the main web.xml or a web fragment takes precedence over the configuration specified via annotations.

在主 web.xml 或 web 片段中指定的配置优先于通过注释指定的配置。

回答by Mr Spark

It seems you know the order of execution but you want to know what will it be if some listeners are declared in deployment descriptor and some using annotation so the preference is taken by deployment descriptor and then annotation.

似乎您知道执行的顺序,但您想知道如果在部署描述符中声明了一些侦听器,而另一些使用注释,那么优先级由部署描述符和注释获取。

Note the constructors of all listeners will be called first in order of deployment descriptor & then annotation and then the life cycle methods in same order.

请注意,所有侦听器的构造函数将首先按部署描述符和注释的顺序调用,然后按相同的顺序调用生命周期方法。