在JBoss中部署2个带有公共类的war文件
我在单个 JBoss 实例中部署了两个 war 文件 app1.war 和 app2.war。两个 war 文件的 java 类的包名称均以 com.myapp 开头。
进一步补充一下,两个应用程序之间有一些通用的类,而有些类具有相同的完全限定类名但不同(源代码已更改)。
我想知道这是否会对部署场景构成任何类型的威胁?
I have two war file app1.war and app2.war deployed in a single JBoss instance. Package names for java classes for both war files starts with com.myapp
To add further, there are some Classes that are common between the two apps while there are some that have same fully qualified class names but are different (Source Code has changed).
I want to know, if this could pose threat of any kind to the deployment scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的应用程序不是隔离的,即有自己的类加载存储库和类加载器,您可能会遇到类加载问题。如果您将 JBoss 配置为将应用程序彼此隔离,那么应该没问题(我不知道您的版本的默认设置是什么,但我们使用的 4.2.3 默认情况下不会隔离应用程序)。
为了澄清这一点:
如果您有两个具有不同实现但具有相同 FQCN 的类,您可能会从第二个加载的应用程序的类加载器中获取错误的类。即使实现相同,如果一个应用程序从另一个应用程序获取类,您也可能会遇到类转换异常或其他奇怪的行为。
You could get class loading problems if your applications are not isolated, i.e. have their own class loading repository and class loaders. If you configure JBoss to isolate the applications from each other you should be fine (I don't know what is the default for your version but 4.2.3 that we use does not isolate apps by default).
To clarify that a bit:
If you have two classes with different implementations but the same FQCN you could get the wrong class from the class loader for the application that is loaded second. Even if the implementation was the same you could get class cast exceptions or other strange behavior if one app gets the class from the other app.
我在多个应用程序中遇到了类似的情况。请查看我的此处的解决方案
I had a similar situation with multiple apps.Look at my solution here
最好的方法是隔离应用程序档案的类加载。
对于 JBoss 5.1.0 GA,以下内容对我有用。
向该文件添加了以下几行
这里,
导出全部=“NON_EMPTY”=>确保为此应用程序加载的类未导出
导入所有=“真”=>导入并使用所有可用的类定义。
父级优先=“假”=>如果发现多个同名类,则首先使用应用程序下定义的类。
供参考。这也帮助我将 log4j 的日志配置嵌入到应用程序 war 文件中。需要将 log4j.xml 放置在 WEB-INF/classes 中,并在 WEB-INF/lib 文件夹中放置 log4j.jar。
Best way is to isolate class loading for your application archives.
For JBoss 5.1.0 GA following worked for me.
Added following lines to this file
Here,
export-all="NON_EMPTY" => Makes sure the classes loaded for this app is not exported
import-all="true" => Imports and uses all of the class definition available.
parent-first="false" => If more than one class with same name is found, one defined under the application will be used first.
FYI. This also helped me embedding the log configuration of log4j in the application war file. Will need to place log4j.xml in WEB-INF/classes and have a log4j.jar in WEB-INF/lib folder.
每个应用程序或独立模块都会有一个类加载器实例。换句话说,app1.war 中的类将与 app2.war 中的类加载到不同的类加载器中。这是任何 Java EE 服务器的默认行为;因此,具有相同包/名称和/或不同内容的类实际上并不重要。这是任何 Java EE 服务器的默认行为。
话虽如此,如果您调整服务器的类加载器策略或尝试使用 Thread.currentThread().getContextClassLoader() 以外的任何内容加载类(反射),您可能会遇到麻烦。
There will be one class loader instance for each application or standalone module. In other words, classes in app1.war will be loaded in different class loader than the classes in app2.war. This is the default behavior of any Java EE server; So it really doesn't matter about having classes with the same package/names and/or different content. This is the default behavior of any Java EE server.
Having said that, if you tweak the class loader policy of the server or try to load classes (reflect) using anything other than
Thread.currentThread().getContextClassLoader()
, you could be asking for trouble.