2012-07-16 79 views
0

有一个intefrace MyIntefraceJBoss 7 ServiceLoader

系统有几个EAR,每个EAR都有自己的intefrace实现。我需要找到没有名称的所有实现(?extends MyInterface)。

我决定用java.util.ServiceLoader,并创建了里面的META-INF /服务/ com.example.MyInterface一个罐子,蚂蚁这个jar可以在任何耳朵部署。

罐子为int的init()方法中一个servlet和下面的代码:

ServiceLoader<MyInterface > loader = ServiceLoader.load(MyInterface .class); 
Iterator<MyInterface > it = loader.iterator(); 

但仍迭代器空..

我tryed添加服务/ com.example.MyInterface耳朵META-INF ,但结果是一样的。

这种方法在jboss 6和tomcat中工作正常。

+0

你找到一个解决这个问题解决了吗? – Rhys 2013-01-02 13:48:02

+0

每个EAR都必须具有META-INF/services/com.example.MyInterface文本文件,并列举该EAR内的所有实现 – 2013-01-02 13:57:04

+0

您是否在使用ServiceLoader体系结构来发现单独的EAR部署之间的服务?即EAR1可以在EAR2中找到服务,而不会相互声明相互之间的直接依赖关系? (通过manifest或jboss-deployment-structure.xml) – Rhys 2013-01-02 14:00:54

回答

3

当我有同样的问题,它是由试图惰性加载

private static class LoaderHolder { 
    ServiceLoader<MyInterface > loader = ServiceLoader.load(MyInterface .class); 
    } 

Iterator<MyInterface > it = LoaderHolder .loader.iterator();