How Tomcat Classloader separates different Webapps object scope in same JVM?

前端 未结 7 698
庸人自扰
庸人自扰 2020-12-05 03:35

Since Tomcat can load more than one webapp at once, and those webapps can work separately, and do not disturb each other, and they work in same JVM. So I am very confused a

7条回答
  •  佛祖请我去吃肉
    2020-12-05 03:42

    In normal Java applications when a classloader is asked to load a class it forst delegates the request to it's parent class loader and then loads it if parent class loaders cannot find the requested class.

    For web application servers this slightly differs. There are generally different class loader for each web app deployed in a web application server like tomcat. For Tomcat it looks like below -

    enter image description here

    So for web apps class loading resource happens in following order -

    1. Bootstrap classes of your JVM (Core java classes)
    2. /WEB-INF/classes of your web application
    3. /WEB-INF/lib/*.jar of your web application
    4. System class loader classes (Tomcat / Classpath specific classes)
    5. Common class loader classes (classes common to all web apps)

    But note if web application class loader is configured with delegate="true" then order is changed -

    1. Bootstrap classes of your JVM (Core java classes)
    2. System class loader classes (Tomcat / Classpath specific classes)
    3. Common class loader classes (classes common to all web apps)
    4. /WEB-INF/classes of your web application
    5. /WEB-INF/lib/*.jar of your web application

    For more details you can check Apache Tomcat's Class Loader HOW-TO page.

提交回复
热议问题