我在网上搜索了很多资料。但我仍然找不到能够准确解决我的问题的特定资源。
在tomcat中,类在启动时加载有什么顺序吗?想象一下这样的情况。
我在 webapps 中有 myApp.war 文件。在那场 war 中,我有一些库(比如说 mylib-2.5.jar 捆绑在里面)。想象一下,我还在 /lib 文件夹中放置了不同版本的相同库(比如 mylib-2.8.jar)。
因此,当我启动我的应用程序并调用特定端点时,哪个库(版本)将有机会被类加载器加载(按需)到 JVM 中?
库冲突的可能性有多大?原因是什么?
最佳答案
来自@GyroGearless 发布的链接
Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:
•Bootstrap classes of your JVM
•/WEB-INF/classes of your web application
•/WEB-INF/lib/*.jar of your web application
•System class loader classes (described above)
•Common class loader classes (described above)
If the web application class loader is configured with
<Loader delegate="true"/>
then the order becomes:•Bootstrap classes of your JVM
•System class loader classes (described above)
•Common class loader classes (described above)
•/WEB-INF/classes of your web application
•/WEB-INF/lib/*.jar of your web application
如果类加载器需要加载 X 类,那么一旦它在一个地方找到它,就不会在任何其他地方寻找它。
如果您将一个版本的依赖项(例如 MySQL
驱动程序)放置到 $JAVA_HOME/jre/lib/ext
,则可能会发生库冲突。另一个进入/WEB-INF/lib
关于java - 当存在 2 个版本的库时,按需在 tomcat 中加载类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51137047/