【问题标题】:JAVA OSGi: InstantiationException with Declarative ServicesJAVA OSGi:带有声明式服务的 InstantiationException
【发布时间】:2010-12-09 09:44:26
【问题描述】:

我是 OSGi 新手,正在构建第一个 DS 实现。

一切都是根据“书”编码的,但运行时出现此错误:

java.lang.InstantiationException: com.mine.logger.internal.udp.UdpListener
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.eclipse.equinox.internal.ds.model.ServiceComponent.createInstance(ServiceComponent.java:457)
at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.createInstance(ServiceComponentProp.java:264)
at org.eclipse.equinox.internal.ds.model.ServiceComponentProp.build(ServiceComponentProp.java:325)
at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponent(InstanceProcess.java:588)
at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponents(InstanceProcess.java:196)
at org.eclipse.equinox.internal.ds.Resolver.getEligible(Resolver.java:328)
at org.eclipse.equinox.internal.ds.SCRManager.serviceChanged(SCRManager.java:221)
at org.eclipse.osgi.internal.serviceregistry.FilteredServiceListener.serviceChanged(FilteredServiceListener.java:104)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:933)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:227)
at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:149)
at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.publishServiceEventPrivileged(ServiceRegistry.java:756)
at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.publishServiceEvent(ServiceRegistry.java:711)
at org.eclipse.osgi.internal.serviceregistry.ServiceRegistrationImpl.register(ServiceRegistrationImpl.java:130)
at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.registerService(ServiceRegistry.java:206)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.registerService(BundleContextImpl.java:507)
at org.eclipse.equinox.internal.ds.InstanceProcess.registerService(InstanceProcess.java:504)
at org.eclipse.equinox.internal.ds.InstanceProcess.buildComponents(InstanceProcess.java:212)
at org.eclipse.equinox.internal.ds.Resolver.buildNewlySatisfied(Resolver.java:441)
at org.eclipse.equinox.internal.ds.Resolver.enableComponents(Resolver.java:213)
at org.eclipse.equinox.internal.ds.SCRManager.performWork(SCRManager.java:800)
at org.eclipse.equinox.internal.ds.SCRManager$QueuedJob.dispatch(SCRManager.java:767)
at org.eclipse.equinox.internal.ds.WorkThread.run(WorkThread.java:89)
at org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor.run(Executor.java:70)

这是我要在其他模块中使用的模块的configuration.xml:

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="startup" deactivate="shutdown" immediate="true" name="com.mine.logger.storeindb">
   <implementation class="com.mine.logger.internal.storeindb.StoreLog"/>
   <service>
      <provide interface="com.mine.logger.storeindb.IStoreLog"/>
   </service>
</scr:component>

这是将使用它的模块的 configuration.xml:

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.mine.logger.udp">
   <implementation class="com.mine.logger.internal.udp.UdpListener"/>
   <reference bind="setStoreLog" interface="com.mine.logger.storeindb.IStoreLog" name="storelog" unbind="unsetStoreLog"/>
</scr:component>

绑定和解绑代码:

private IStoreLog storeLog;

public void setStoreLog(IStoreLog value)
{
    System.err.println("UdpListener > setStoreLog");
    this.storeLog = value;
}

public void unsetStoreLog(IStoreLog value)
{
    System.err.println("UdpListener > unsetStoreLog");
    if (this.storeLog == value) {
        this.storeLog = null;
    }
}

有什么想法吗? 谢谢, 弗兰克

【问题讨论】:

    标签: java osgi declarative-services


    【解决方案1】:

    InstantiationException:

    当应用程序尝试使用类 Class 中的 newInstance 方法创建类的实例,但无法实例化指定的类对象时抛出。实例化可能因多种原因而失败,包括但不限于:

    • 类对象表示抽象类、接口、数组类、原始类型或 void
    • 该类没有空构造函数

    com.mine.logger.internal.udp.UdpListener 是否有一个公共的、无参数的构造函数?

    【讨论】:

      【解决方案2】:

      感谢蒂洛!

      添加了一个无参数函数,错误消失了!

      public UdpListener() 
      {
          // public, no-args constructor needed for Declarative Services !!!
      }
      
      public UdpListener(int port) 
      {
          this.port = port;
      }
      

      【讨论】:

      • 您刚刚用评论回答了您自己的问题。这不是一个论坛。您应该只接受正确的答案和/或将 cmets 添加到其他答案或您的问题中。如果您愿意,也可以编辑您的问题。
      【解决方案3】:

      错字? IStoreLog 字段名为 storeLog,但您说的是 name="storelog"(小写 l)。

      【讨论】:

      • 不,同样的结果,名称似乎只用作标签而不用作代码。结果相同:
      【解决方案4】:

      我有类似的问题,由引用非公共类引起。比如:

      在 ServiceAImpl.java 文件中:

      class ServiceBImpl implements Service {
      ...
      }
      

      这是不幸重构的结果。

      【讨论】:

        猜你喜欢
        • 2012-04-27
        • 1970-01-01
        • 2018-05-20
        • 2013-10-09
        • 1970-01-01
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多