Search in sources :

Example 6 with ProviderInstanceBinding

use of com.google.inject.spi.ProviderInstanceBinding in project ninja by ninjaframework.

the class LifecycleServiceImpl method start.

@Override
public void start() {
    startTime = System.currentTimeMillis();
    log.info("Starting Ninja application...");
    state = State.STARTING;
    // until they are instantiated that LifecycleSupport has an opportunity to register them.
    for (final Binding binding : injector.getBindings().values()) {
        binding.acceptScopingVisitor(new DefaultBindingScopingVisitor() {

            @Override
            public Object visitEagerSingleton() {
                injector.getInstance(binding.getKey());
                return null;
            }

            @Override
            public Object visitScope(Scope scope) {
                if (scope.equals(Scopes.SINGLETON)) {
                    Object target = injector.getInstance(binding.getKey());
                    if (binding instanceof ProviderInstanceBinding) {
                        Provider providerInstance = ((ProviderInstanceBinding) binding).getProviderInstance();
                        if (providerInstance instanceof ProviderMethod) {
                            // @Provides methods don't get picked up by TypeListeners, so we need to manually register them
                            if (lifecycleSupport.hasLifecycleMethod(target.getClass())) {
                                lifecycleSupport.registerLifecycle(target);
                            }
                        }
                    }
                }
                return null;
            }
        });
    }
    lifecycleRegister.start();
    long time = System.currentTimeMillis() - startTime;
    log.info("Ninja application started in {}ms", time);
    state = lifecycleRegister.isStarted() ? State.STARTED : State.STOPPED;
}
Also used : ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Binding(com.google.inject.Binding) Scope(com.google.inject.Scope) DefaultBindingScopingVisitor(com.google.inject.spi.DefaultBindingScopingVisitor) ProviderMethod(com.google.inject.internal.ProviderMethod) ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Provider(com.google.inject.Provider)

Aggregations

Binding (com.google.inject.Binding)6 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)6 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)5 InstanceBinding (com.google.inject.spi.InstanceBinding)5 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)5 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 Injector (com.google.inject.Injector)3 IndexedBinding (com.google.inject.internal.Indexer.IndexedBinding)3 ProviderMapEntry (com.google.inject.internal.RealMapBinder.ProviderMapEntry)3 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)3 MultibinderBinding (com.google.inject.multibindings.MultibinderBinding)3 MultibindingsTargetVisitor (com.google.inject.multibindings.MultibindingsTargetVisitor)3 OptionalBinderBinding (com.google.inject.multibindings.OptionalBinderBinding)3 Key (com.google.inject.Key)2 IndexedBinding (com.google.inject.multibindings.Indexer.IndexedBinding)2 ProviderMapEntry (com.google.inject.multibindings.MapBinder.RealMapBinder.ProviderMapEntry)2 Element (com.google.inject.spi.Element)2 ProviderLookup (com.google.inject.spi.ProviderLookup)2