繁体   English   中英

在WildFly / JBoss中设置TCCL

[英]Setting the TCCL in WildFly/JBoss

我遇到此处讨论的问题,该问题导致java.lang.ClassNotFoundException: __redirected/__DatatypeFactory错误。

在上面的线程中,Jason Greene说:“ ...确保将TCCL设置为指向模块类加载器(并在finally块中将其重置为原始)。” 我想尝试一下,但无法弄清楚如何设置TCCL或获取模块类加载器。 如何将TCCL设置为模块类加载器?

附加信息:

  • 使用JAXB封送XML时遇到错误
  • 我正在使用WildFly 8.0.0.CR1

答案最终变得非常简单(就我的上下文而言!),我有一个带有正确TCCL上下文的类。

这将是初始化时具有正确加载程序的类

public class GoodClass {

    private ClassLoader goldenLoader;

    public GoodClass() {
        // the class loader must be the 'correct' one at this point. We save the 'correct' one for later
        this.goldenLoader = Thread.currentThread().getContextClassLoader();
    }

    public static ClassLoader getGoodClassLoader() {
        return goldenLoader;
    }
}

这将是具有TCCL和__redirected / __ DatatypeFactory问题的类

public class BadClass {
    public void myMethod() {

        ClassLoader origLoader = Thread.currentThread().getContextClassLoader();

        try {
            // you can NOT do stuff that relies on the set TCCL here
            ClassLoader goldenLoader = GoodClass.getGoodClassClassLoader().getGoldenLoader();
            Thread.currentThread().setContextClassLoader(goldenLoader);
            // you CAN do stuff that relies on the set TCCL here
        } finally {
            // use this if you need to restore the orignal loader
            Thread.currentThread().setContextClassLoader(origLoader);
        }
    }
}

暂无
暂无

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

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