on-internet
← Community

Two "Singletons" of the same class

Stack Overflow ·

Hypothetically is it possible to have CDI inject exactly 2 different instances of the same class throughout my code? Two singletons of one class so to speak? And if yes, what would the annotations look like? Producing two different @Named instances with @Produces methods? Here is what I came up with: @Singleton public final class StartupBean { @Inject StartupBean(SomeManager someManager) { // nothing happening here } @Produces @Named("fooObject") public MyImplementation produceFooObject() { return new MyImplementation(); } @Produces @Named("barObject") public MyImplementation produceBarObject(

Comments

Sign in or sign up to comment and upvote.

No comments yet.