[英]ServiceLoader + EJB injection
I would like to know if I can combine the ServiceLoader
with EJB and injection from Java EE 6. 我想知道是否可以将
ServiceLoader
与EJB结合并从Java EE 6中进行注入。
Imagine I have an interface ServiceI
that can be implemented by two EJB stateless beans BeanA
and BeanB
. 想象一下,我有一个可由两个EJB无状态bean
BeanA
和BeanB
实现的ServiceI
接口。
If I register these two classes in the MEAT-INF/services
they will be instantiated in the Java SE way, rather than being managed by the Application Container (like when you use @Inject
). 如果我在
MEAT-INF/services
注册了这两个类,它们将以Java SE的方式实例化,而不是由应用程序容器管理(例如,使用@Inject
)。 This means that annotations like @Inject
or @PostConstruct
won't be resolved. 这意味着将不会解析
@Inject
或@PostConstruct
类的注释。
Is it possible to have something like a ServiceLoader
which would allow me to go through EJB beans implementing a given interface and returning one based on some criteria? 是否可能有类似
ServiceLoader
这样的东西,它使我可以通过EJB bean实现给定的接口并根据某些条件返回一个接口?
I have found a solution. 我找到了解决方案。
If both beans implements the Service
interface you can do the following: 如果两个bean都实现
Service
接口,则可以执行以下操作:
@Inject
private javax.enterprise.inject.Instance.Instance<Service> services
services
implements Iterator<Service>
and will allow to cycle through all the beans that implement the Service
interface. services
实现Iterator<Service>
,并将允许在实现Service
接口的所有bean之间循环。
Then you can choose one of the implementations based on some criteria and you have the equivalent of ServiceLoader
for EJB! 然后,您可以根据某些条件选择一种实现,并且具有与EJB相同的
ServiceLoader
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.