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

前端 未结 7 677
庸人自扰
庸人自扰 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

    1. Different App in tomcat using different classloader to seperate. For example app1 using ClassLoaderA, app2 using classloaderB.
    2. Each class will use its own classloader to load other classes. So if ClassA.class references ClassB.class then ClassB needs to be on the classpath of the classloader of ClassA, or its parents. For example In app1, com.exmaple.test1 loaded from ClassLoaderA. And com.exmaple.test1 want to new com.exmaple.test2(). By default It use its own classloader ClassLoaderA to load com.exmaple.test2. So In the view of com.exmaple.test1 it can only see its own classpath's class(app1/webapp/classes or app1/webapp/lib). And In app2, It will see a different view.
    3. In summary learn classloader you must understand the delegation model. And the visibility is the child can see the parent. but the parent can not see the child and the sibling can not see the sibling. So we can isolate different app.

提交回复
热议问题