Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject Injector?

Situation: i need lazy dependency instantiation in some FooClass, so i pass Injector to class as a constructor parameter.

private final Injector m_injector;  public FooClass(@Named("FooInjector") Injector injector) { m_injector = injector; } 

But guice doesn't permit to bind core classes (injectors, modules and etc). What is the solution?

like image 589
Alex M Avatar asked Feb 01 '10 11:02

Alex M


People also ask

What is @inject in Angular?

@Inject() is a manual mechanism for letting Angular know that a parameter must be injected. It can be used like so: import { Component, Inject } from '@angular/core'; import { ChatWidget } from '../components/chat-widget'; ​

Why do we use injector in Angular?

Injectors. Injectors are data structures that store instructions detailing where and how services form. They act as intermediaries within the Angular DI system. Module, directive, and component classes contain metadata specific to injectors.

What does injector getInstance do?

getInstance. Returns the appropriate instance for the given injection type; equivalent to getProvider(type).

What is root injector in Angular?

We call this root Injector. This Injector becomes the root of the ElementInjector tree. The Root Component contains all other components. Angular App will create child components under the Root Component. All these child component can have their own child components creating a tree of components.


1 Answers

You should not be using the Injector directly. Rather pass in the Provider<FooClass> instead. Also, you should be injecting the provider in the places where you use FooClass.

private final Provider<FooClass> provider;  @Inject public ClassWhereFooIsUsed(Provider<FooClass> provider) {     this.provider = provider; }  .... somewhere else FooClass f = provider.get(); // This is lazy 
like image 148
gpampara Avatar answered Oct 03 '22 03:10

gpampara