Search in sources :

Example 1 with DefaultBindingScopingVisitor

use of com.google.inject.spi.DefaultBindingScopingVisitor 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)1 Provider (com.google.inject.Provider)1 Scope (com.google.inject.Scope)1 ProviderMethod (com.google.inject.internal.ProviderMethod)1 DefaultBindingScopingVisitor (com.google.inject.spi.DefaultBindingScopingVisitor)1 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)1