I am running into several problems with guice and hoping that someone
could help me out.
Basically what I want to do is be able to get two different instances
of an Object injected with different dependencies. For Example say I
have the following two interfaces.
public interface GlueApi
{
public void doSomething();
}
public interface Manager
{
public void sendMessage();
}
In the implementation classes as you can see there is a dependency
between GlueApiImpl and a Manager.
public class GlueApiImpl implements GlueApi
{
private final Manager theMessageManager;
@Inject
public GlueApiImpl(Manager aMessageManager)
{
theMessageManager = aMessageManager;
}
public void doSomething()
{
System.out.println("Doing Something");
}
}
public class LocalManager implements Manager
{
public void sendMessage()
{
System.out.println(getClass().getName() + " Says hello");
}
}
public class RemoteManager implements Manager
{
public void sendMessage()
{
System.out.println(getClass().getName() + " Says hello");
}
}
what I want to try and do is configure two separate instances of
GlueApiImpl one with a LocalManager and one with a RemoteManager and
then get them from the injector.
To achieve this I have created two new annotations Local and Remote
and added the following module and main class with provider classes
annotated with field level injection.
ublic class Module extends AbstractModule
{
@Override
protected void configure()
{
bind(GlueApi.class).annotatedWith(Local.class)
.toProvider(new Provider<GlueApi>(){
@Inject @Local Manager messageManager;
@Override
public GlueApi get()
{
return new GlueApiImpl(messageManager);
}
});
bind(GlueApi.class).annotatedWith(Remote.class)
.toProvider(new Provider<GlueApi>(){
@Inject @Remote Manager messageManager;
@Override
public GlueApi get()
{
return new GlueApiImpl(messageManager);
}
});
bind(Manager.class).annotatedWith(Remote.class).to(RemoteManager.class);
bind(Manager.class).annotatedWith(Local.class).to(LocalManager.class);
}
public static void main(String[] args)
{
Injector myInjector = Guice.createInjector(new Module());
@Remote GlueApi myApi =
myInjector.getInstance(GlueApi.class);
}
}
Is the above even possible and how would I go about it?
--
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.