public class MyAppModule extends AbstractModule { protected void configure() { bind(MyService.class).to(MyServiceImpl.class); bind(AnotherService.class).to(AnotherServiceImpl.class); } @Provides public static List<Class<?>> provideBeans() { return Arrays.asList(MyBean.class, AnotherBean.class); } } public class MyApp { public static void main(String[] args) { Injector injector = Guice.createInjector(new MyAppModule()); MyBean myBean = injector.getInstance(MyBean.class); AnotherBean anotherBean = injector.getInstance(AnotherBean.class); myBean.doSomething(); anotherBean.doSomethingElse(); } }


上一篇:
下一篇:
切换中文