@Inject(optional = true) @Named("hello world") doesn't work

2,473 views
Skip to first unread message

Eduardo Nunes

unread,
Apr 21, 2009, 9:14:04 PM4/21/09
to google...@googlegroups.com
Hello guys,

I tried the following source code and it's not working, am I right?

@Inject(optional = true)
@Named("hello world")
private String message;

Guice is throwing the exception bellow:

Caused by: com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for java.lang.String annotated with
@com.google.inject.name.Named(value=SignOutPage.redirectPage) was
bound.
while locating java.lang.String annotated with
@com.google.inject.name.Named(value=SignOutPage.redirectPage)


Thanks,
Eduardo S. Nunes

limpb...@gmail.com

unread,
Apr 21, 2009, 11:52:23 PM4/21/09
to google-guice
I couldn't reproduce this. Could you try again with the latest
snapshot?
http://code.google.com/p/google-guice/downloads/list

If you can get it to fail reliably, please send a JUnit test case.

Cheers,
Jesse

Eduardo Nunes

unread,
Apr 23, 2009, 9:07:23 PM4/23/09
to google...@googlegroups.com
I tried to reproduce it in a test case but I couldn't. I think the
error is inside wicket-guice library. This is part of the stacktrace:

Caused by: com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for java.lang.String annotated with
@com.google.inject.name.Named(value=SignOutPage.redirectPage) was
bound.
while locating java.lang.String annotated with
@com.google.inject.name.Named(value=SignOutPage.redirectPage)

1 error
at com.google.inject.InjectorImpl.getProvider(InjectorImpl.java:784)
at com.google.inject.InjectorImpl.getInstance(InjectorImpl.java:789)
at org.apache.wicket.guice.GuiceProxyTargetLocator.locateProxyTarget(GuiceProxyTargetLocator.java:67)
at org.apache.wicket.proxy.LazyInitProxyFactory.createProxy(LazyInitProxyFactory.java:129)
at org.apache.wicket.guice.GuiceComponentInjector.inject(GuiceComponentInjector.java:114)
at org.apache.wicket.guice.GuiceComponentInjector.onInstantiation(GuiceComponentInjector.java:196)
at org.apache.wicket.Application.notifyComponentInstantiationListeners(Application.java:1027)

Eduardo Nunes

unread,
Apr 23, 2009, 11:21:52 PM4/23/09
to google...@googlegroups.com
Hi,

I found the problem. It is because wicket-guice library creates a
proxy of every @inject attribute and uses a locator to get the
instance. The main problem consist in the algorithm of this locator,
it makes something like this:

- keeps a reference to a possible BindingAnnotation (Named for example)
- keeps a reference to the field type (String.class for example)
- generates a key = Key.get(TypeLiteral.get(String.class), annotation.class);
- calls injector.getInstance(key);

So, the information about the optional @Inject parameter is lost. I
took a look in the google-guice source code trying to find a solution
for this problem but it stills complex for me (I'm new to guice). I
realized that
it should use the same algorithm of SingleFieldInjector (member
injection). I saw that the most part of guice classes are not public.
Do you have any suggestions? Do you mind some way to get the bound
value with the Class definition + field definition?

Thanks anyway,
Eduardo S. Nunes

limpb...@gmail.com

unread,
Apr 24, 2009, 1:21:40 AM4/24/09
to google-guice
I've opened an issue in Wicket's issue tracker.
https://issues.apache.org/jira/browse/WICKET-2241

Guðmundur Bjarni

unread,
Apr 25, 2009, 9:03:34 AM4/25/09
to google-guice
I wrote a simple fix that I've attached a patch to the issue that you
created on the Wicket issue tracker. I changed the wicket-guice unit
test so that it contains an optional binding, and that seems to be
working just fine now.

regards,
Guðmundur Bjarni

Eduardo Nunes

unread,
Apr 26, 2009, 11:25:51 PM4/26/09
to google...@googlegroups.com
This solution doesn't work for the trunk version of google-guice. I
posted a comment in the jira issue.

2009/4/25 Guðmundur Bjarni <gudmundu...@gmail.com>:

Guðmundur Bjarni

unread,
Apr 27, 2009, 4:49:31 AM4/27/09
to google-guice
Thanks for pointing this out. I originally only tried it with Guice
1.0. I've tried it with trunk just now, and as you rightly point out,
it doesn't work. I've written and posted another patch that fixes it
though. :)

regards,
Guðmundur Bjarni


On Apr 27, 5:25 am, Eduardo Nunes <esnu...@gmail.com> wrote:
> This solution doesn't work for the trunk version of google-guice. I
> posted a comment in the jira issue.
>
> 2009/4/25 Guðmundur Bjarni <gudmundur.bja...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages