public static LifecycleInjector createInjector( Stage stage, Collection<? extends Module> modules) { final LifecycleManager manager = new LifecycleManager(); Injector injector; List<Module> l = new ArrayList<>(); try { l.add(new LifecycleModule()); l.add( new AbstractModule() { @Override protected void configure() { bind(LifecycleManager.class).toInstance(manager); requestInjection(manager); } }); l.addAll(modules); injector = Guice.createInjector(stage, l); } catch (Exception e) { try { manager.notifyStartFailed(e); } catch (Exception e2) { System.err.println("Failed to notify LifecycleManager"); e.printStackTrace(); } throw e; } try { manager.notifyStarted(); return new LifecycleInjector(injector, manager); } catch (Exception e) { manager.notifyShutdown(); throw e; } }
private LifecycleInjector create() { // The logger is intentionally created here to avoid early static initialization // of SLF4J/LOG4J which may be customized using one of the bootstrap modules Logger LOG = LoggerFactory.getLogger(Governator.class); LOG.info("Using profiles : " + profiles); // Generate a single list of all discovered modules // TODO: Duplicates? final Set<Module> loadedModules = new HashSet<>(); for (ModuleListProvider loader : moduleProviders) { loadedModules.addAll(loader.get()); } final LifecycleManager manager = new LifecycleManager(); Injector injector; try { injector = Guice.createInjector( stage, new LifecycleModule(), new AbstractModule() { @Override protected void configure() { bind(LifecycleManager.class).toInstance(manager); requestInjection(manager); } }, create( LOG, manager, loadedModules, modules, false, // First, auto load the bootstrap modules (usually deal with configuration and // logging) and // use to load the main module. create( LOG, manager, loadedModules, bootstrapModules, true, new DefaultModule() { @Provides PropertySource getPropertySource() { return new DefaultPropertySource(); } }))); } catch (Throwable e) { try { manager.notifyStartFailed(e); } catch (Exception e2) { System.err.println("Failed to notify injector creation failure!"); e2.printStackTrace(System.err); } throw new RuntimeException(e); } try { manager.notifyStarted(); return new LifecycleInjector(injector, manager); } catch (Exception e) { manager.notifyShutdown(); throw e; } }