How to disable Servlet 3.0 scanning and auto loading of components

前端 未结 3 1053
南笙
南笙 2021-01-18 23:17

We have an application which keeps loading instances of ServletContainerInitializer from our 3rd party libs.

One instance is JerseyServletContainerInitializer and th

3条回答
  •  庸人自扰
    2021-01-18 23:42

    The specification statement regarding absolute-ordering, per the Servlet 3.1 specification, is found in Section 8.2.2 "Ordering of web.xml and web-fragment.xml", part 1.d.:

    The element may contain zero or one element. The required action for this element is described below. If the element does not contain an element, any web-fragment not specifically mentioned within elements MUST be ignored. Excluded jars are not scanned for annotated servlets, filters or listeners. However, if a servlet, filter or listener from an excluded jar is listed in web.xml or a non-excluded web-fragment.xml, then it's annotations will apply unless otherwise excluded by metadata-complete.

    A key take-away is that using an empty absolute-ordering element will not just turn off scanning for servlet-container initializers. Scanning for all component defining annotations, such as @WebServlet, is turned off. Use of an empty absolute-ordering element is only advised if you really want to turn off all annotations processing of JAR files within a web application.

    The scanning can be more finely tuned: Use an absolute-ordering element, and include the names of JARs which are to be scanned using name elements, and omit JARs which you wish to have skipped. DO NOT use an others element, as this will put back into the ordering all of the JARs which are not explicitly listed, and will turn scanning back on for those JARs.

    As a general practice, we've found that turning off all annotation processing is dangerous, as a web application which is enabled for annotation scanning has some classes which must be scanned, which means that turning off the scanning entirely would break the web application.

提交回复
热议问题