2010-12-09 109 views
1

我是OSGi的新手,正在構建第一個DS實現。JAVA OSGi:聲明式服務的InstantiationException

一切都在按「書」,但編碼當運行我得到這個錯誤:

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; 
    } 
} 

任何想法? 謝謝, 弗蘭克

回答

2

InstantiationException

Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated. The instantiation can fail for a variety of reasons including but not limited to:

  • the class object represents an abstract class, an interface, an array class, a primitive type, or void
  • the class has no nullary constructor

是否有一個公共的無參數的構造爲com.mine.logger.internal.udp.UdpListener

0

錯字? IStoreLog字段被命名爲storeLog,但是您說name="storelog"(小寫字母l)。

+0

nope,相同的結果,該名稱似乎只被用作標籤而不是代碼。同樣的結果: Frank 2010-12-09 10:00:49

1

感謝Thilo!

添加了無參數函數,錯誤消失!

public UdpListener() 
{ 
    // public, no-args constructor needed for Declarative Services !!! 
} 

public UdpListener(int port) 
{ 
    this.port = port; 
} 
+1

您剛剛回答了您自己的問題並附帶評論。這不是一個論壇。你應該接受正確的答案和/或添加評論給其他答案或你的問題。如果你喜歡,你也可以編輯你的問題。 – musiKk 2010-12-09 10:19:08

0

我有類似的問題,由引用非公共類造成的。喜歡的東西:

在ServiceAImpl.java文件:

class ServiceBImpl implements Service { 
... 
} 

這是不幸的重構效果。

相關問題