Tomcat Classloader如何在同一个JVM中分隔不同的Webapps对象作用域? [英] How Tomcat Classloader separates different Webapps object scope in same JVM?

查看:227
本文介绍了Tomcat Classloader如何在同一个JVM中分隔不同的Webapps对象作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Tomcat一次可以加载多个Web应用程序,并且这些Web应用程序可以独立运行,并且不会互相干扰,因此它们可以在同一JVM中运行. 因此,我对于tomcat如何在同一JVM中处理Object范围感到非常困惑.

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 about how tomcat handle The Object scope in the same JVM.

例如,我在两个不同的Webapp中都有一个单例对象,并且tomcat将为每个生成两个不同的单例对象.我一直认为单例对象在同一个JVM中只有一个对象,但是在tomcat JVM中可能有两个或更多对象.

For example I have a singleton object in both of the two different Webapps, and tomcat will generate two different singleton Object for each. I always thought that the singleton object have only one object in the same JVM, but in tomcat JVM there maybe has two or more.

我已经阅读了有关ClassLoader的一些信息,Tomcat具有自己的WebAppClassLoader来加载webapp.这是否意味着这里的Object Scope是ClassLoader还是我错了.有人知道吗,或者可以给我一些有关tomcat工作存储器布局的信息吗?

I have read some info about the ClassLoader, Tomcat has its own WebAppClassLoader to load webapps. So does it mean the Object Scope here is the ClassLoader or am I wrong. Does anyone know about this or can give me some info about tomcat work memory layout?

推荐答案

所有秘密都在这些ClassLoader实例的后面.

All the secrets is behind those ClassLoader instances.

类的状态(如所有静态变量,字节码等)由类加载器确定,该类加载器加载该类(JVM中的类通过其完全限定的名称标识,而类加载器则加载该类.这不完全是一个范围,但作为范围的思考通常有助于更好地理解这一点.

The state of the class (like all static variables, byte code and so on) is scoped by the class loader which loads that class (a class is identified in JVM by its fully qualiflied name and the class loader loading the class. This is not exactly a scope, but thinking as scope usually helps understanding this better).

因此,如果一个类由两个不同的类加载器加载,则该类在VM中存在两次,它具有两组静态字段,可以具有不同的字节码(如不同的方法实现)等等.请注意,即使两个对象的名称相同,也无法将它们强制转换为彼此. 普通" Java应用程序具有通过类加载器层次结构加载的所有类,并且每个类仅加载一次.

So if a class is loaded by two different class loaders, this class exists twice within the VM, it has two sets of static fields, can have different byte code (like different method implementations) and all such. Note that these two objects cannot be cast to each other even their names are identical. "Normal" Java applications have all classes loaded by a class loader hierarchy and every class is only loaded once.

对于更复杂的情况,您将需要不同的行为.有时,您希望将库与混乱的代码隔离开来(例如eclipse中的插件或应用程序服务器中的Web应用程序).

For more complex scenarios, you will need different behaviours. Sometimes you want to isolate a library from messing with your code (like plugins in eclipse or web applications in an application server).

将程序与其他类隔离的基本思想是使用额外的类加载器加载那些类并进行大量反射.如果您想阅读有关内容,请参阅 OSGI .

The basic idea to isolate your program from other classes is to load those with an extra class loader and use a lot of reflection. If you want to read up on this have a look at Oracle's documentation on ClassLoaders or OSGI.

Tomcat(以及许多其他Web容器/应用程序服务器)使用单独的ClassLoader层次结构加载应用程序.这样可以将所有类与其他(Web)应用程序隔离开,从而确保单例,不同的类版本以及所有这些东西不会冲突.

Tomcat (and a lot of other web containers / application servers) load the application with separate ClassLoader hierarchies. This isolates all classes against other (web) applications and thus also makes sure, that singletons, different class versions and all this stuff does not collide.

这篇关于Tomcat Classloader如何在同一个JVM中分隔不同的Webapps对象作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆