JBoss Weld:根据上下文切换注入实例并在多个bean中注入实例
我有一个对象,我想将其注入多个不同类型的对象中。这个对象必须是相同的:
class A {}
class B {@Inject A a;}
class C {@Inject A a;}
class D {@Inject A a;}
A 实例 a 必须在 B、C、D 类型的对象之间共享。
此外,我需要能够在这些对象中切换 A 的实例,而不破坏它。我什至需要能够将 A 的所有实例保存在一个容器中。 A 的每个实例都与一个文档相关,并且我的应用程序应该能够处理 n 个文档(不是同时进行,但应该能够在每个文档之间切换)。当用户选择另一个文档时,A 的相应实例应替换 B、C、D 实例中的前一个文档。
是否有一种简单的方法可以使用 JBoss Weld 来做到这一点?
我有另一个解决方案的想法(使用单例管理器对文档选择做出反应,并在需要时替换 A 的实例),但我认为这个解决方案并不真正干净。也许我错了...
I have an object that I'd like to inject in several objects of different type. This object must be the same :
class A {}
class B {@Inject A a;}
class C {@Inject A a;}
class D {@Inject A a;}
The A instance a must be shared between the objects of type B, C, D.
In addition, I need to be able to switch the instance of A in those objects, without destructing it. I even need to be able to keep all the instances of A in a container. Each instance of A is related to a document, and my application should be able to work on n documents (not in the same time, but should be able to switch between each of them). When the user selects another document, the corresponding instance of A should replace the previous one in the instances of B, C, D.
Is there a simple way to do it with JBoss Weld ?
I have another idea of solution (using a singleton manager reacting on the document selection, and replacing the instances of A where needed), but I don't find this solution really clean. Maybe i'm wrong...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到的解决方案是使用 Manager 而不是直接使用 A,例如 :
并根据上下文切换管理器中的 currentA。
The solution I found was using a Manager instead of directly using A, such as :
and switching the currentA in the manager, depending on the context.