war 的库和 ear 的库之间的 wildfly 类加载器问题

wildfly classloader issue between war's lib and ear's lib

我在 Wildfly 中部署了 EAR 的这个模式:

LibInterfaceA.jar 是一个仅在 ejb 模块(A 和 B)和 WAR 中用于注入 ejb 并在 LibInterfaceB.jar 中扩展的接口库。

当我在 LibInterfaceA 中搜索自定义注释时,问题出在 java 反射:

public Annotation getAnnotazione(Method method, Class annotationType){
    Annotation annotazioni[] = method.getAnnotations();
    URL locationsearch = annotationType.getResource('/' + annotationType.getName().replace('.', '/') + ".class");
    for(Annotation a : annotazioni){
        Class klass = a.annotationType();
        URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");
        if(a.annotationType().getName().equals(annotationType.getName())){
            return a;
        }
    }
    return null;
}

我在WAR里面的一个class中调用这个方法来找到注解annotationType=@Custom。 @Custom 属于 WAR/lib/LibInterfaceA.jar

如果我调用此方法来查找 Class 实现 LibInterfaceB 的接口,该接口扩展了 LibInterfeceA 中的接口,方法上的注释 @Custom 是 jar EAR/lib/LibInterfaceA.jar 的,所以对于 class 加载问题,class 是不同的。

我该如何解决这个问题?

您可以从 war 中删除 LibInterfaceA.jar。默认情况下,ear 的子部署可以使用其 lib 目录(除非您在 ear 的 jboss-depoyment-structure.xml 中设置 ear-subdeployments-isolated = true)