简体   繁体   English

JavaEE和OSGI:如何为某些osgi软件包获取BeanManager

[英]JavaEE and OSGI:How to get BeanManager for certain osgi bundle

As I understand right every osgi bundle has its own cdi container. 据我了解,每个osgi捆绑包都有自己的cdi容器。 So I have the following situation - I have two bundles: A and B. Bundles are deployed in glassfish4. 因此,我遇到以下情况-我有两个捆绑包:A和B。捆绑包部署在glassfish4中。 In bundle AI have cdi beans. AI捆绑包中有cdi bean。 In bundle BI don't have beans.xml -> no cdi container for the bundle B is created. 在捆绑软件BI中没有bean.xml->没有为捆绑软件B创建cdi容器。

In bundle BI have a reference to bundle A (Bundle bundleA). 捆绑软件BI中有对捆绑软件A(捆绑软件bundleA)的引用。 How can I get in bundle B reference to bundle A cdi container. 我如何获得捆绑B中对捆绑A cdi容器的引用。 My current solution (in bundle B): 我当前的解决方案(在捆绑包B中):

BeanManager bm = CDI.current().getBeanManager();

throws exception: 引发异常:

StandardWrapperValve[default]: Servlet.service() for servlet default threw exception

org.jboss.weld.exceptions.IllegalStateException: WELD-001328: Unable to identify the correct BeanManager. The calling class com.temp.MyClass is not placed in bean archive
    at org.jboss.weld.SimpleCDI.unsatisfiedBeanManager(SimpleCDI.java:89)
    at org.glassfish.weld.GlassFishWeldProvider$GlassFishEnhancedWeld.unsatisfiedBeanManager(GlassFishWeldProvider.java:89)
    at org.jboss.weld.SimpleCDI$ClassNameToBeanManager.findBeanManager(SimpleCDI.java:67)
    at org.jboss.weld.SimpleCDI$ClassNameToBeanManager.load(SimpleCDI.java:47)
    at org.jboss.weld.SimpleCDI$ClassNameToBeanManager.load(SimpleCDI.java:40)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3589)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2374)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2337)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2252)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3990)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3994)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4878)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4884)
    at org.jboss.weld.SimpleCDI.getBeanManager(SimpleCDI.java:105)
    at org.jboss.weld.SimpleCDI.getBeanManager(SimpleCDI.java:38)

Calling BeanManager bm = CDI.current().getBeanManager(); 调用BeanManager bm = CDI.current().getBeanManager(); is the correct way to obtain a BeanManager for current bean archive. 是获取当前bean存档的BeanManager的正确方法。 Eg the bean archive to which the class where you invoke this belongs. 例如,调用它的类所属的bean档案。 That would be bundle B in your case. 在您的情况下,这就是bundle B And you do not have a beans.xml there hence the exception. 而且那里没有beans.xml ,因此是例外。

As I understand right every osgi bundle has its own cdi container. 据我了解,每个osgi捆绑包都有自己的cdi容器。

I am not much familiar with OSGI but from Weld perspective that would work. 我对OSGI不太熟悉,但是从Weld的角度来看是可行的。 Weld sees any form of archive (jar, war, ...) as bean archive and for one bean archive you have one manager. Weld将任何形式的存档(jar,war,...)视为Bean存档,对于一个Bean存档,您只有一个管理员。

If you want a reference to BM from bundle A , obtain it there and expose a getter I suppose. 如果要从bundle A引用BM,请在那里获取它,并公开我认为的吸气剂。 The point is you need to invoke getBeanManager() from the actual archive it belongs to. 关键是您需要从它所属的实际存档中调用getBeanManager()

Maybe if you elaborated on what do you aim to achieve with that BM, I might help you further. 也许,如果您详细说明了该BM的目标,我可能会进一步帮助您。 Generaly speaking I would refrain from obtaining references to BMs from other archives. 一般来说,我将避免从其他档案中获取有关BM的参考。 You can always have an exposed API and let the original archive deliver the services to you. 您始终可以使用公开的API,并让原始存档向您提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM