Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Servlet 3.0 scanning and auto loading of components

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

One instance is JerseyServletContainerInitializer and the other is SpringServletContainerInitializer. These classes from Jersey and Spring seem to "take over" our servlet context messing with our mappings and filters and such.

We really need to explicitly configure our servlet container's web.xml and this auto scanning is driving us insane. By simply pulling in a dependency in our pom.xml our runtime ServletContext configurations such as Servlets/Filters/ContextListeners are mutated because the servlet container finds these libraries on the classpath.

Is there a way to use Servlet 3 but disable its annoying auto classpath scanning "feature"?

like image 230
benstpierre Avatar asked Apr 29 '16 19:04

benstpierre


People also ask

What is @ServletComponentScan?

Annotation Type ServletComponentScanEnables scanning for Servlet components ( filters , servlets , and listeners ). Scanning is only performed when using an embedded web server. Typically, one of value , basePackages , or basePackageClasses should be specified to control the packages to be scanned for components.

What is absolute ordering?

The absolute order is a natural partial order on a Coxeter group W. It can be viewed as an analogue of the weak order on W in which the role of the generating set of simple reflections in W is played by the set of all reflections in W.

How do I create an absolute order in web xml?

Your specify absolute ordering using the <absolute-ordering> element in the web. xml file. You specify relative ordering with an <ordering> element in the web-fragment. xml file.

Where do I put metadata complete true in web xml?

If metadata-complete is set to "true", the deployment tool only examines the web. xml file and must ignore annotations such as @WebServlet, @WebFilter, and @WebListener present in the class files of the application, and must also ignore any web-fragment. xml descriptor packaged in a jar file in WEB-INF/lib.


1 Answers

From https://wiki.apache.org/tomcat/HowTo/FasterStartUp

There are two options that can be specified in your WEB-INF/web.xml file:

  • Set metadata-complete="true" attribute on the <web-app> element.
  • Add an empty <absolute-ordering /> element.

Setting metadata-complete="true" disables scanning your web application and its libraries for classes that use annotations to define components of a web application (Servlets etc.). The metadata-complete option is not enough to disable all of annotation scanning. If there is a SCI with a @HandlesTypes annotation, Tomcat has to scan your application for classes that use annotations or interfaces specified in that annotation.

The <absolute-ordering> element specifies which web fragment JARs (according to the names in their WEB-INF/web-fragment.xml files) have to be scanned for SCIs, fragments and annotations. An empty element configures that none are to be scanned.

In Tomcat 7 the absolute-ordering option affects discovery both of SCIs provided by web application and ones provided by the container (i.e. by the libraries in $CATALINA_HOME/lib). In Tomcat 8 the option affects the web application ones only, while the container-provided SCIs are always discovered, regardless of absolute-ordering. In such case the absolute-ordering option alone does not prevent scanning for annotations, but the list of JARs to be scanned will be empty, and thus the scanning will complete quickly. The classes in WEB-INF/classes are always scanned regardless of absolute-ordering.

Scanning for web application resources and TLD scanning are not affected by these options.

like image 181
Leonard Brünings Avatar answered Sep 28 '22 09:09

Leonard Brünings