【问题标题】:ServiceLoader usage in Axis2 web service code deployed on Tomcat web server部署在 Tomcat Web 服务器上的 Axis2 Web 服务代码中的 ServiceLoader 使用
【发布时间】:2013-01-26 15:07:42
【问题描述】:

我在 Tomcat 上部署了一个 .aar 文件,Ubuntu 的 Axis2 平台。 我正在使用 Oracle JDK1.7 Tomcat 7.0.35 和 Axis2 1.6.2

最近我做了一个重构,使用 java.util.ServiceLoader 类来加载 一些类是动态的。

这是我的 aar 文件结构:

META-INF folder

services.xml Contains info about the web service

services folder
    com.companyname...interfacename1 (contains implementation class name)
    com.companyname...interfacename2 (contains implementation class name)
    com.companyname...interfacename3 (contains implementation class name)

com folder
    companyname folder
        bla bla.class compiled class files here

在我的 Eclipse 开发环境中,我的方法运行时没有任何问题,但是当它们运行时 在 Axis2 Tomcat 平台中,ServiceLoader 类无法加载实现类。 可能是什么问题? 任何帮助表示赞赏。 谢谢。

【问题讨论】:

  • 在 Apache Axis2 常见问题页面 link,有一个使用 Axis API 访问服务类加载器的建议,如下所示:AxisService myService = messageContext.getAxisConfiguration().getAxisService("serviceName"); AxisService myService = msgCtx.getAxisService();类加载器 clsLoader = myService.getServiceClassLoader();然后我可以这样调用: ServiceLoader.load(service,clsLoader) 感谢 Deepal Jayasinghe 的推荐!但是,此代码为我的代码带来了 Axis2 包依赖项。有没有改进的方法来做到这一点?

标签: java tomcat axis2 serviceloader


【解决方案1】:

只需使用你的一个类来获取它们的类加载器,然后使用它:

    ClassLoader classLoader = com.companyname.blabla.MyClass1.getClassLoader();

    java.util.ServiceLoader.load(blabla.class,classLoader);

它在 Tomcat、Axis2 部署代码和开发环境单元测试代码中都有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多