I have CDI+OSGI javase application.CDI-Weld,OSGI-felix 和 pax-cdi。我在"CDI-main"中有以下代码
@ApplicationScoped
public class Foo{
public void postCreate(@Observes ContainerInitialized event, BundleContext ctx) throws Exception {
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
System.out.println("$Number of print services: " + printServices.length);
for (PrintService printer : printServices)
System.out.println("$Printer: " + printer.getName());
}
}
当我运行此应用程序时,我将获得以下输出(尽管我有带有正确驱动程序的打印机!
打印服务$Number:0
注意,第一个符号是$;如果我将以下代码添加到捆绑激活器并启动它
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
System.out.println("#Number of print services: " + printServices.length);
for (PrintService printer : printServices)
System.out.println("#Printer: " + printer.getName());
}
public void stop(BundleContext context) throws Exception {
}
}
注意,第一个符号是#。然后检测到我所有的打印机:
#Number of print services: 1
#Printer: MF3110
Jun 14, 2015 1:47:34 PM org.jboss.weld.bootstrap.WeldStartup startContainer...
....
$Number of print services: 1
$Printer: MF3110
怎么解释呢?
PrintServiceLookup 是在单独的捆绑包中定义的,还是使用来自单独 OSGI Service 的代码?它可能与 osgi 服务基数有关吗?
在第一个代码片段中,PrintServiceLookup.lookupPrintServices
在与第二个代码段不同的生命周期阶段调用。
在第一个示例中,容器或扩展器可能未满足调用lookupPrintServices
时PrintServiceLookup
的所有依赖项。
在第二个示例中,这些依赖项可能得到满足,因为lookupPrintServices
是在捆绑激活器的start
方法中调用的 - 该方法在 START 阶段由容器调用。在 START 阶段,容器已解析捆绑包的所有依赖项。
希望我能帮上忙。