Named annotaion is not working

34 views
Skip to first unread message

Yunfeng

unread,
Jul 10, 2009, 3:19:27 AM7/10/09
to google-gin
I tried to use annotation to identify different instances. However, it
is not working, GIN only creates one instance for different names.
Here's the code.

module:

protected void configure() {
bind(HistoryManager.class).annotatedWith(Names.named("hm1")).to
(HistoryManager.class);
bind(HistoryManager.class).annotatedWith(Names.named("hm2")).to
(HistoryManager.class);
}

================================================
code to get injected:

@Inject
public HistoryManagers(@Named("hm1") HistoryManager manager1,
@Named("hm2") HistoryManager manager2) {
super();
this.manager1 = manager1;
this.manager2 = manager2;
}

===============================================
test case:

public void testGetInstance() {
injector = GWT.create(ClientGInjector.class);
assertNotNull(injector.getAdderService());

HistoryManagers managers = injector.getHistoryManagers();
assertFalse(managers.getManager1() == managers.getManager2());
}

===============================================
The test fails, and if I add some print in constructor of
HistoryManager, it shows that the constructor only invoked once.

Did I missed anything or this is really a bug?

Yunfeng Hou

Peter Schmitt

unread,
Jul 10, 2009, 2:48:41 PM7/10/09
to googl...@googlegroups.com
Hi Yunfeng,

  protected void configure() {
   bind(HistoryManager.class).annotatedWith(Names.named("hm1")).to
(HistoryManager.class);
   bind(HistoryManager.class).annotatedWith(Names.named("hm2")).to
(HistoryManager.class);
 }

These bindings configure nothing about the number of times that the constructor of your HistoryManager class is called. All that you say here is that you want any instance of HistoryManager injected when it is annotated with @Named("hm1") or @Named("hm2"), versus maybe injecting a subclass of HistoryManager if it is not annotated (a binding for that is missin though, so I'd say the two bindings are superfluous).

The reason why your history manager constructor might only be called once is that you might have @Singleton annotation on the class or you bind it in singleton scope elsewhere (i.e. you're calling bind(HistoryManager.class).in(Singleton.class)).

If you would like to have two distinct HistoryManager instances injected into your HistoryManagers constructor, remove the singleton scoping and either
  • Use the constructor you are now using, maybe without annotations or
  • Inject a provider (Provider<HistoryManager> managerProvider) and get two instances from it.
Hope this helps!

Peter
 

Yunfeng

unread,
Jul 13, 2009, 10:58:20 PM7/13/09
to google-gin
You're right, somehow I forgot to remove the Named annotation from the
class. It is working now. Thanks.

Yunfeng Hou

On Jul 11, 2:48 am, Peter Schmitt <ara...@gmail.com> wrote:
> Hi Yunfeng,
>
>   protected void configure() {
>
> >    bind(HistoryManager.class).annotatedWith(Names.named("hm1")).to
> > (HistoryManager.class);
> >    bind(HistoryManager.class).annotatedWith(Names.named("hm2")).to
> > (HistoryManager.class);
> >  }
>
> These bindings configure nothing about the number of times that the
> constructor of your HistoryManager class is called. All that you say here is
> that you want *any *instance of HistoryManager injected when it is annotated
> with @Named("hm1") or @Named("hm2"), versus maybe injecting a subclass of
> HistoryManager if it is not annotated (a binding for that is missin though,
> so I'd say the two bindings are superfluous).
>
> The reason why your history manager constructor might only be called once is
> that you might have @Singleton annotation on the class or you bind it in
> singleton scope elsewhere (i.e. you're calling
> bind(HistoryManager.class).in(Singleton.class)).
>
> If you would like to have two distinct HistoryManager instances injected
> into your HistoryManagers constructor, remove the singleton scoping and
> either
>
>    - Use the constructor you are now using, maybe without annotations or
>    - Inject a provider (Provider<HistoryManager> managerProvider) and get
Reply all
Reply to author
Forward
0 new messages