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…
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.