我有一个捆绑组件,
package ipojo;
import ipojo.service.Hello;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Validate;
@Component(name="hello-factory")
@Provides
public class HelloImpl implements Hello{
@Override
public void shoutHello() {
System.out.println("HellooOOOOoooOooo!");
}
@Validate
public void start() throws Exception {
System.out.println("Hello started :)");
}
@Invalidate
public void stop() throws Exception {
System.out.println("Hello Stopped :(");
}
}
在我的 Java 应用程序中,我嵌入了 Apache Felix,并部署了 iPOJO API。然后,我尝试使用工厂服务创建上述组件的实例,如下所示:
myBundle= context.installBundle("myBundlePath");
myBundle.start();
ServiceReference[] references = myBundle.getBundleContext().getServiceReferences(Factory.class.getName(), "(factory.name=hello-factory)");
if (references == null) {
System.out.println("No references!");
}
else {
System.out.println(references[0].toString());
Factory factory = myBundle.getBundleContext().getService(references[0]);
ComponentInstance instance= factory.createComponentInstance(null);
instance.start();
}
我成功获得了对工厂服务的引用,但在以下行:
Factory factory = myBundle.getBundleContext().getService(references[0]);
我得到以下 ClassCastException:
java.lang.ClassCastException: org.apache.felix.ipojo.ComponentFactory cannot be cast to org.apache.felix.ipojo.Factory`
我将此行更改为:
Factory factory = (ComponentFactory) myBundle.getBundleContext().getService(references[0]);
然后我得到:
java.lang.ClassCastException: org.apache.felix.ipojo.ComponentFactory cannot be cast to org.apache.felix.ipojo.ComponentFactory
我该如何解决我的问题?谢谢你。