问题
I have two basis class A and B. B is injected in A. I have a third class C injected in A and B, as follow :
class A {
@Inject B b;
@Inject C c;
}
class B {
@Inject C c;
}
class C {
}
I'd like the instance of C contained in A and in B is the same. I could use a setC() method in B, but that's not at all the philosophia of injection. Should I use Weld scopes ? If yes, how should I do ?
Thanks
回答1:
The solution is simply annotate my classes and injections with @Singleton Annotation
class A {
@Inject B b;
@Inject @Singleton C c;
}
class B {
@Inject @Singleton C c;
}
@Singleton
class C {
}
来源:https://stackoverflow.com/questions/8502836/jboss-weld-cdi-inject-the-same-instance-in-two-different-objects