Issue
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"?
Solution
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.
Answered By - Leonard Brünings
Answer Checked By - Timothy Miller (JavaFixing Admin)