问题描述
如果我有多个ServletContextListener并且其中一些是在部署描述符中声明的,而另一些是使用注释(@WebListener)声明的,那么如何定义ServletContextListener执行应用程序初始化的顺序?
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)?
推荐答案
如果要按特定顺序执行侦听器,则应使用部署描述符来定义它们。
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,过滤器对应用程序很重要,然后必须使用部署描述符。当使用注释来定义侦听器,servlet和过滤器时,它们的调用顺序是未指定的。
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.
排序将基于它们的定义顺序在描述符和 web.xml
中的绝对排序元素或 web-fragment.xml $ c $中的排序元素c>。
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.xml
or an ordering element in the web-fragment.xml
.
在此版本规范(Java™Servlet
规范,版本3)之前,以随机
顺序调用上下文侦听器。从Servlet 3.0开始,将按
中的顺序调用侦听器,它们在web.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.
的实现javax.servlet.ServletContextListener
按照它们的声明顺序在 contextInitialized
方法中调用,并在处调用contextDestroyed
方法的顺序相反。
Implementations of javax.servlet.ServletContextListener
are invoked at their contextInitialized
method in the order in which they have been declared, and at their contextDestroyed
method in reverse order.
如果你有多个ServletContextListener,其中一些是在部署描述符中声明的和其他带注释的人,然后在 web.xml
中定义的侦听器将获得优先权。下面的语句是从servlet规范的相同部分(8.2.3)复制的:
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.xml
that will get the precedence. Below statement is copied from the same section (8.2.3) of servlet specification:
这篇关于ServletContextListener执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!