title: Spring jars in tomcat lib folder
body: <p>I am completely confused over how the class loading happens in tomcat. So please bear with me if my question sounds stupid.</p>
  
  <p>We deploy multiple spring webapps on single tomcat server. To reduce the memory footprint, we thought of having spring, hibernate and database driver jars in tomcat lib folder so that those are shared by all webapps. </p>
  
  <p>I started first by marking spring dependencies as <code>provided</code> and copied those jars to tomcat lib. But on server startup I started getting multiple <code>ClassNotFoundErrors</code>, like for <code>commons-logging</code>, <code>commons-fileupload</code>, <code>jackson</code>, so I had to move these jars as well to tomcat lib.</p>
  
  <p>But then this started feeling fishy. What if in future I add another spring dependency to my project, say spring-data-cassandra. Would I need to move it's dependent jars as well? This might be unending. Also I might get CNF errors at runtime.</p>
  
  <p>I tried to follow <a href="https://stackoverflow.com/questions/267953/does-tomcat-load-the-same-library-file-into-memory-twice-if-they-are-in-two-web">this</a> link and brought back spring-context and spring-web back to application war. But it didn't work, got ClassNotFound on some class during <code>WebApplicationIntializer</code> initialization. I tried to understand the order of <a href="http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html" rel="noreferrer">classes getting loaded in tomcat</a>, but could not understand much. </p>
  
  <p>Then I found a complete different explanation for <a href="https://stackoverflow.com/questions/6981564/why-must-the-jdbc-driver-be-put-in-tomcat-home-lib-folder">JDBC driver loading</a> which kinda contradicts to all other explanations and left me completely confused.</p>
  
  <p><br>
  <br>
  As I read more, I think it is not a right approach to move spring jars to tomcat lib, but still haven't got a good reasoning. And then why JDBC driver works? Can someone please explain? Also does classloader for each webapp creates a copy of each class? </p>
  
  <p>Edit 1: I came to know that few dependencies are <code>optional</code> in spring jars, and will be required if are in use in my webapp. So spring-web depends on <code>jackson</code> libraries but is optional for my app. So I need to find out which all jars are required for my project and are also required by spring, those jars need to be moved to tomcat lib.</p>