我是 OSGi 的新手,我尝试使用 DS 创建一个简单的 EventPublisher/-Admin 应用程序以确保 EventAdmin 不为空。但我不确定如何以正确的方式使用 DS。
Activator类:
package publishertest;
import java.util.HashMap;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
public class Activator implements BundleActivator {
private static BundleContext context;
@Reference
EventAdmin eventAdmin;
static BundleContext getContext() {
return context;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Event event = new Event("test", new HashMap<String, Object>());
eventAdmin.postEvent(event);
System.out.println("event posted");
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
EventHandler类:
package publishertest;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
@Component(
property = {
"event.topics=org/osgi/framework/BundleEvent/STARTED,test"
}
)
public class ServiceComponent implements EventHandler {
public void handleEvent(Event event) {
System.out.println(event.getTopic());
}
}
添加 @Reference 注释会导致 BundleException。有人可以帮忙吗?谢谢:)