Created
July 7, 2014 18:13
Migrated attachment for Guice issue 353, comment 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.faulab.fmsgenerator; | |
import com.google.inject.*; | |
import com.google.inject.assistedinject.Assisted; | |
import com.google.inject.assistedinject.FactoryProvider; | |
import ru.faulab.fmsgenerator.core.XUI; | |
import ru.faulab.fmsgenerator.gui.GUI; | |
/** | |
* User: Dr.Faust | |
* Date: 11.04.2009 | |
* Time: 13:38:05 | |
*/ | |
public class GuiceBagExample | |
{ | |
static interface IRootService | |
{ | |
} | |
static interface IRootService2 | |
{ | |
} | |
static class IRootService2Impl implements IRootService2 | |
{ | |
@Inject | |
public IRootService2Impl(IRootServiceFactory rootService) | |
{ | |
} | |
} | |
static interface IRootServiceFactory | |
{ | |
IRootService create(String foo); | |
} | |
static class IRootServiceImpl implements IRootService | |
{ | |
@Inject | |
public IRootServiceImpl(@Assisted String foo) | |
{ | |
} | |
} | |
static class RootSubModule1 extends AbstractModule | |
{ | |
protected void configure() | |
{ | |
this.bind(IRootServiceFactory.class).toProvider(FactoryProvider.newFactory(IRootServiceFactory.class, IRootServiceImpl.class)).in(Singleton.class); | |
} | |
} | |
static class RootSubModule2 extends AbstractModule | |
{ | |
protected void configure() | |
{ | |
this.bind(IRootService2.class).to(IRootService2Impl.class).in(Singleton.class); | |
} | |
} | |
static class RootModuleForTest extends AbstractModule | |
{ | |
@Override | |
protected void configure() | |
{ | |
this.install(new RootSubModule1()); | |
this.install(new RootSubModule2()); | |
} | |
} | |
static class ChildService2Impl | |
{ | |
public ChildService2Impl() | |
{ | |
} | |
} | |
static interface ChildService1 | |
{ | |
} | |
static class ChildService1Impl implements ChildService1 | |
{ | |
@Inject | |
// public ChildService1Impl(Injector injector) /*BAG*/ | |
public ChildService1Impl(Injector injector,ChildService2Impl childService2) /*ALL WORK*/ | |
{ | |
System.out.println(injector.getParent()); | |
} | |
} | |
static class ChildModuleForTest extends AbstractModule | |
{ | |
@Override | |
protected void configure() | |
{ | |
this.bind(ChildService2Impl.class).in(Singleton.class); | |
this.bind(ChildService1.class).to(ChildService1Impl.class).in(Singleton.class); | |
} | |
} | |
private final Injector injector; | |
public GuiceBagExample(Injector injector) | |
{ | |
this.injector = injector.createChildInjector(new ChildModuleForTest()); | |
} | |
public void run() | |
{ | |
ChildService1 instance = injector.getInstance(ChildService1.class); | |
} | |
public static void main(String[] args) | |
{ | |
GuiceBagExample bagExample = new GuiceBagExample(Guice.createInjector(Stage.DEVELOPMENT, new RootModuleForTest())); | |
bagExample.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment