简体   繁体   中英

JBoss Weld CDI : Inject the same instance in two different Objects

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

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 {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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