2009/7/15 Ulli Hafner <[email protected]>

>
> Hi,
>
> I'm a newbie on guice and tried to find the inverse annotation of
> @ImplementedBy where a class can define that it provides the default
> implementation of an interface?
>
> An example:
>
> I have a class that requires a Component for a field:
> @Inject
> private Component component;
>
> Then I have the Component interface that specifies the implementing
> class:
> @ImplementedBy(ComponentImpl.class)
> public interface Component() { ... }
>
> This approach has a major drawback, the interface has a dependency to
> the implementation class. In order to avoid that design problem, you
> need the inverse annotation, something like:
> @Provides(Component.class)
> public class ComponentImpl() { ... }
>
> But that seems to be not possible in guice. The only thing I found is
> to provide a factory method with the @Provides annotation.
>
> Am I missing something here? Or is that feature already planned in a
> future release?
>

well you'd still need to tell Guice about this implementation class,
otherwise how can it know it's out there - for the @ImplementedBy
case this is trivial as Guice already has the interface class when
it's doing the injection

for example, when you ask Guice to inject a "Foo" it can look to
see if there's an @ImplementedBy on Foo and use that to create
the actual instance to inject

now consider the opposite case, you ask Guice to inject Foo and
there's a @Provides(Foo.class) on both FooImpl and FooBarImpl
...but Guice doesn't know anything about these classes!

so right now the way you tell Guice about them is via bindings:

   bind(Foo.class).to(FooImpl.class);

you typically write these bindings explicitly in Java, but there's no
reason why you couldn't describe them in XML and write code to
turn them into bindings at runtime

you could even write code that does classpath scanning (with an
annotation like @Provides) which you then use to create bindings

the idea (afaik) is that the core Guice code isn't tied down to a
particular concept like classpath scanning (which doesn't work
well with OSGi for example) but is agnostic about where these
bindings come from

in summary, it would be possible for someone to write a small
Guice extension that does what you're asking for (ie. scans for
implementations with a given annotation and configures Guice
accordingly) but afaik there isn't one available... yet

it would make a cool project though for anyone interested ;)

Ulli
> >

-- 
Cheers, Stuart

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to