简体   繁体   English

tomcat lib文件夹中的接口实现无法引用web-inf / classes文件夹中的接口

[英]interface implemetation in tomcat lib folder not able to refer to interface present in web-inf/classes folder

I have a interface defined in a war file under WEB-INF/classes folder. 我在WEB-INF / classes文件夹下的war文件中定义了一个接口。 The idea is to have an implementation to be placed in class path without disturbing the war file. 想法是将实现放在类路径中,而不会打扰war文件。 The solution I thought was to place the implementation in tomcat_home/lib directory but due to possible use of different classloader for implementation and interface, the interface is not getting resolved and ClassNotFound is being thrown. 我认为的解决方案是将实现放置在tomcat_home / lib目录中,但是由于可能使用不同的类加载器来实现和接口,因此无法解析该接口,并抛出了ClassNotFound。 Is this scenario possible to achieve? 这种情况有可能实现吗?

1) The common class loader , which loads from CATALINA_HOME/lib has no knowledge of classes inside individual webapp. 1)从CATALINA_HOME/lib加载的通用类加载器不了解单个webapp中的类。

2) The webapp classloader responsible for loading your webapp's classes has no knowledge of CATALINA_HOME/lib . 2)负责加载Webapp的类的webapp类加载器不了解CATALINA_HOME/lib If it can't load a class, it delegates up the chain. 如果无法加载类,则会委托该链。

The solution is both have to be loaded using the same class loader. 解决方案都必须使用相同的类加载器来加载。 You have to keep both the classes under CATALINA_HOME/lib so that the common classloader can resolve them. 您必须将两个类都保留在CATALINA_HOME/lib以便公共类加载器可以解析它们。

You are getting this error because the your implementation is loading in memory before the interface because Class loaders in the Application Server runtime follow a delegation hierarchy where classes in tomcat lib is loaded before classes in web-inf/classes. 之所以收到此错误,是因为您的实现在接口之前先在内存中加载,因为Application Server运行时中的类加载器遵循委派层次结构,其中,tomcat lib中的类先于web-inf / classs中的类加载。 So if you want to remove the error keep both in tomcat lib, so that both will load at same time. 因此,如果要消除错误,请将两者都保留在tomcat lib中,以便两者都将同时加载。

在此处输入图片说明

In the delegation design, a class loader delegates classloading to its parent before attempting to load a class itself. 在委托设计中,类加载器在尝试加载类本身之前将类加载委托给其父级。 A class loader parent can be either the System class loader or another custom class loader. 类加载器的父级可以是System类加载器,也可以是其他自定义类加载器。 If the parent class loader cannot load a class, the class loader attempts to load the class itself. 如果父类加载器无法加载类,则类加载器将尝试加载类本身。 In effect, a class loader is responsible for loading only the classes not available to the parent. 实际上,类加载器负责仅加载父级不可用的类。 Classes loaded by a class loader higher in the hierarchy cannot refer to classes available lower in the hierarchy. 由层次结构中较高级别的类加载器加载的类不能引用层次结构中较低的可用类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM