简体   繁体   English

在JBoss应用程序服务器中,如何确保部署中的所有模块对jar库使用相同的Classloader?

[英]In JBoss application server, how do I make sure that all modules in a deployment use the same Classloader for a jar library?

I have a deployable ear that I'm deploying to a JBoss application server. 我有一个可部署到JBoss应用程序服务器的耳朵。 I am in the process of refactoring my code so that my entity classes are declared in a separate jar library that can be shared by the different modules in the ear. 我正在重构代码,以便在一个单独的jar库中声明我的实体类,该类库可以由耳朵中的不同模块共享。

However, I am now getting the following error: 但是,我现在收到以下错误:

java.lang.ClassCastException: model.project.Project cannot be cast to model.EntityBase

EntityBase is a public abstract class. EntityBase是一个公共抽象类。 Project is a public class that extends EntityBase . Project是扩展EntityBase的公共类。 Both classes have been moved to my new jar library. 这两个类都已移到我的新jar库中。 This error leads me to the conclusion that there is an issue with the classloader. 该错误使我得出结论,即类加载器存在问题。

My ear is structured as follows: 我的耳朵的结构如下:

myapp.ear
 |
 |--- webportal.war
 |--- restapi.war
 |--- dal.jar

My entities had been defined in the dal.jar which is used by the war libraries to access my database. 我的实体已在dal.jar中定义,战争图书馆使用它来访问我的数据库。 I have now removed the entities from the dal.jar and placed them in a separate model.jar. 现在,我从dal.jar中删除了实体,并将其放置在单独的model.jar中。 I have listed the model.jar as a dependency in the pom.xml for myapp.ear. 我已经将model.jar列为myapp.ear的pom.xml中的依赖项。 It is also listed as a dependency in the pom.xml files for webportal.war, restapi.war, and dal.jar. 它在webportal.war,restapi.war和dal.jar的pom.xml文件中也作为依赖项列出。 As a result the model.jar is now deployed in the lib directory within myapp.ear. 结果,现在将model.jar部署在myapp.ear的lib目录中。

Is my understanding of the cause of the ClassCastException correct? 我对ClassCastException的原因的理解正确吗? If so, how do I package my application to ensure that all of the classes in model.jar are loaded with the same classloader? 如果是这样,我如何打包我的应用程序以确保用相同的类加载器加载model.jar中的所有类?

It turned out to be a relatively simple solution. 事实证明这是一个相对简单的解决方案。 I modified the pom.xml files for webportal.war, restapi.war, and dal.jar by adding a scope tag with a value of provided for the model dependency: 我通过添加scope标签为模型依赖项provided了一个值来修改webportal.war,restapi.war和dal.jar的pom.xml文件:

<dependency>
    <groupId>myproject</groupId>
    <artifactiId>model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>provided</scope>
</dependency>

I then modified the pom.xml for myapp.ear by adding a dependency to model.jar with no scope tag (this will give the dependency the default scope which I believe is compile ): 然后,我通过将没有范围标记的依赖项添加到model.jar来修改myapp.ear的pom.xml(这将为该依赖项提供我认为是compile的默认范围):

<dependency>
    <groupId>myproject</groupId>
    <artifactiId>model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保类加载器可以访问临时jar文件 - how to make sure that the temporary jar file is accessible by the classloader 如何强制JBoss部署使用特定版本的依赖性而不是已安装的模块? - How can I force a JBoss deployment to use a specific version of a dependency instead of installed modules? Websphere Application Server Extension Classloader jar冲突 - Websphere Application Server Extension Classloader jar conflict Jar hell:如何在运行时使用类加载器将一个jar库版本替换为另一个jar库版本 - Jar hell: how to use a classloader to replace one jar library version with another at runtime 如何在jboss部署中检查类加载器类层次结构(优先级) - How to check the classloader class hierarchy (precendence) in jboss deployment java有条件的我如何确保它检查所有的if语句 - java conditional how do I make sure it checks all if statements 如何在Java项目中包含.jar库,并在编译后使应用程序使用该库在应用程序内部? - How to include .jar library, that is inside java project and make application use this library that is inside application after compilation? 如何使用类加载器在model.zip文件上获取输入流,其中zip文件包装在classpath中的.jar文件中 - How do I use a classloader to get inputstream on a model.zip file where the zip file is wrapped in a .jar file in classpath 具有相同jar的Java类加载器 - Java classloader with same jar 如何在jar文件中使用ClassLoader.getResources() - How to use ClassLoader.getResources() in jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM