gpt4 book ai didi

java - ServletContextListener 执行顺序

转载 作者:IT老高 更新时间:2023-10-28 20:53:06 35 4
gpt4 key购买 nike

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

最佳答案

如果你想以特定顺序执行监听器,你应该使用部署描述符来定义它们。

以下语句复制自 Servlet Specification :

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.xml or 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.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) 复制而来的:

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

关于java - ServletContextListener 执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19729370/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com