ChildInjector
ChildInjectorã¯ãä»»æã®Injectorããããã®Injectorãæã¤ã¢ã¸ã¥ã¼ã«ã®å 容ãç¶æ¿ããåInjectorãä½æããæ©è½ã§ãã
- åInjectorã§ã¯è¦ªã®ã³ã³ãã¼ãã³ããåç §ã§ããã¤ã³ã¹ã¿ã³ã¹ãåå¾ããããInjectãããã§ãã¾ãã
- åInjectorä½ææã«ã¯Moduleã渡ããã¨ãã§ããã®ã§ãã親ã®ã¢ã¸ã¥ã¼ã«+åã®ã¢ã¸ã¥ã¼ã«ããåã§ä½¿ãããã¨ã«ãªãã¾ãã
- åã§è¦ªã®ã¢ã¸ã¥ã¼ã«ã使ããã¨ã¯å¯è½ã§ãããé(親ã§åã®ã¢ã¸ã¥ã¼ã«ã使ã)ã¯ä¸å¯ã§ãã
// 親Injector Injector parent = Guice.createInjector( new AbstractModule(){ protected void configure() { binder().bind( String.class ).toInstance( "mii" ); } }); // åInjector Injector child = parent.createChildInjector( new AbstractModule(){ protected void configure() { binder().bind( Integer.class ).toInstance( 100 ); binder().bind( Foo.class ); } }); // åã§ã¯ãã親+åãã®ã³ã³ãã¼ãã³ããå©ç¨å¯è½ System.out.println( child.getInstance( String.class ) ); System.out.println( child.getInstance( Integer.class ) ); // Injectãã§ããã Foo foo = child.getInstance( Foo.class ); System.out.println( foo.str ); System.out.println( foo.i ); .... // Fooã®å®è£ static class Foo { @Inject String str; @Inject Integer i; }
å®è¡çµæã§ãã
mii 100 mii 100
親ããåã®ã³ã³ãã¼ãã³ãã¯åç §ã§ããªãã®ã§ã以ä¸ã¯ã¨ã©ã¼ã«ãªãã¾ãã
// 親Injector Injector parent = Guice.createInjector( new AbstractModule(){ protected void configure() { binder().bind( String.class ).toInstance( "mii" ); binder().bind( Foo.class ); } }); // åInjector Injector child = parent.createChildInjector( new AbstractModule(){ protected void configure() { binder().bind( Integer.class ).toInstance( 100 ); } });
å®è¡çµæã§ãã
Exception in thread "main" com.google.inject.CreationException: Guice creation errors: ....