我试图从另一个捆绑包的osgi-over-slf4j(slf4j v1.7.7)捆绑包中获取日志服务,但只有在调用getServiceReference(…)时才得到null。
以下是另一个捆绑包中的代码,它们试图获得日志服务。
public void start(BundleContext context) throws Exception {
System.out.println(" ID State Name");
Bundle[] bundles = context.getBundles();
for (Bundle bundle : bundles) {
printBundle(bundle.getBundleId(),
getStateString(bundle.getState()), (String) bundle
.getHeaders().get(Constants.BUNDLE_NAME),
bundle.getLocation(), bundle.getSymbolicName());
}
ServiceReference ref = context.getServiceReference(LogService.class.getName());
if (ref == null) {
System.out.println("service ref. is null");
} else {
LogService ls = (LogService) context.getService(ref);
if (ls == null) {
System.out.println(" Log is Null");
} else {
ls.log(LogService.LOG_DEBUG, "aaaaHahahahahaha");
}
}
}
这是捆绑包的MANIFEST.MF的一部分
Import-Package: org.osgi.framework;version="1.5.0",
org.osgi.service.log;version="1.3.0",
org.osgi.util.tracker;version="1.4.0"
Bundle-Activator: com.ktbcs.cjmf.log.factory.activator.LogServiceFactoryActivator
我确信osgi-over-slf4j已经处于活动状态。我的环境是windows 8上的Websphere 8.5。
欢迎提出任何建议。
谢谢,
anurak
您的捆绑包比osgi-over-slf4j更早启动。在这种情况下,在Activator的启动功能中找不到服务。
为了避免此问题,您可以使用声明服务或其替代方案,而不是Activator。