Help Angular by taking a 1 minute survey!Go to surveyHome

ReflectiveInjector

A ReflectiveDependency injection container used for instantiating objects and resolving dependencies.

See more...

Deprecated: from v5 - slow and brings in a lot of code, Use Injector.create instead.

      
      abstract class ReflectiveInjector implements Injector {
  static resolve(providers: Provider[]): ResolvedReflectiveProvider[]
  static resolveAndCreate(providers: Provider[], parent?: Injector): ReflectiveInjector
  static fromResolvedProviders(providers: ResolvedReflectiveProvider[], parent?: Injector): ReflectiveInjector
  abstract parent: Injector | null
  abstract resolveAndCreateChild(providers: Provider[]): ReflectiveInjector
  abstract createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector
  abstract resolveAndInstantiate(provider: Provider): any
  abstract instantiateResolved(provider: ResolvedReflectiveProvider): any
  abstract get(token: any, notFoundValue?: any): any
}
    

Description

An Injector is a replacement for a new operator, which can automatically resolve the constructor dependencies.

In typical use, application code asks for the dependencies in the constructor and they are resolved by the Injector.

Static methods

Turns an array of provider definitions into an array of resolved providers.

static resolve(providers: Provider[]): ResolvedReflectiveProvider[]
      
      static resolve(providers: Provider[]): ResolvedReflectiveProvider[]
    
Parameters
providers Provider[]
Returns

ResolvedReflectiveProvider[]

A resolution is a process of flattening multiple nested arrays and converting individual providers into an array of ResolvedReflectiveProviders.

Usage Notes

Example
@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var providers = ReflectiveInjector.resolve([Car, [[Engine]]]); expect(providers.length).toEqual(2); expect(providers[0] instanceof ResolvedReflectiveProvider).toBe(true); expect(providers[0].key.displayName).toBe("Car"); expect(providers[0].dependencies.length).toEqual(1); expect(providers[0].factory).toBeDefined(); expect(providers[1].key.displayName).toBe("Engine"); });
      
      
  1. @Injectable()
  2. class Engine {
  3. }
  4.  
  5. @Injectable()
  6. class Car {
  7. constructor(public engine:Engine) {}
  8. }
  9.  
  10. var providers = ReflectiveInjector.resolve([Car, [[Engine]]]);
  11.  
  12. expect(providers.length).toEqual(2);
  13.  
  14. expect(providers[0] instanceof ResolvedReflectiveProvider).toBe(true);
  15. expect(providers[0].key.displayName).toBe("Car");
  16. expect(providers[0].dependencies.length).toEqual(1);
  17. expect(providers[0].factory).toBeDefined();
  18.  
  19. expect(providers[1].key.displayName).toBe("Engine");
  20. });

Resolves an array of providers and creates an injector from those providers.

static resolveAndCreate(providers: Provider[], parent?: Injector): ReflectiveInjector
      
      static resolveAndCreate(providers: Provider[], parent?: Injector): ReflectiveInjector
    
Parameters
providers Provider[]
parent Injector

Optional. Default is undefined.

Returns

ReflectiveInjector

The passed-in providers can be an array of Type, Provider, or a recursive array of more providers.

Usage Notes

Example
@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]); expect(injector.get(Car) instanceof Car).toBe(true);
      
      @Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);
expect(injector.get(Car) instanceof Car).toBe(true);
    

Creates an injector from previously resolved providers.

static fromResolvedProviders(providers: ResolvedReflectiveProvider[], parent?: Injector): ReflectiveInjector
      
      static fromResolvedProviders(providers: ResolvedReflectiveProvider[], parent?: Injector): ReflectiveInjector
    
Parameters
providers ResolvedReflectiveProvider[]
parent Injector

Optional. Default is undefined.

Returns

ReflectiveInjector

This API is the recommended way to construct injectors in performance-sensitive parts.

Usage Notes

Example
@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var providers = ReflectiveInjector.resolve([Car, Engine]); var injector = ReflectiveInjector.fromResolvedProviders(providers); expect(injector.get(Car) instanceof Car).toBe(true);
      
      
  1. @Injectable()
  2. class Engine {
  3. }
  4.  
  5. @Injectable()
  6. class Car {
  7. constructor(public engine:Engine) {}
  8. }
  9.  
  10. var providers = ReflectiveInjector.resolve([Car, Engine]);
  11. var injector = ReflectiveInjector.fromResolvedProviders(providers);
  12. expect(injector.get(Car) instanceof Car).toBe(true);

Properties

Property Description
abstract parent: Injector | null Read-only.

Parent of this injector.

Methods

Resolves an array of providers and creates a child injector from those providers.

abstract resolveAndCreateChild(providers: Provider[]): ReflectiveInjector
      
      abstract resolveAndCreateChild(providers: Provider[]): ReflectiveInjector
    
Parameters
providers Provider[]
Returns

ReflectiveInjector

The passed-in providers can be an array of Type, Provider, or a recursive array of more providers.

Usage Notes

Example
class ParentProvider {} class ChildProvider {} var parent = ReflectiveInjector.resolveAndCreate([ParentProvider]); var child = parent.resolveAndCreateChild([ChildProvider]); expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true); expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true); expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));
      
      class ParentProvider {}
class ChildProvider {}

var parent = ReflectiveInjector.resolveAndCreate([ParentProvider]);
var child = parent.resolveAndCreateChild([ChildProvider]);

expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true);
expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true);
expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));
    

Creates a child injector from previously resolved providers.

abstract createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector
      
      abstract createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector
    
Parameters
providers ResolvedReflectiveProvider[]
Returns

ReflectiveInjector

This API is the recommended way to construct injectors in performance-sensitive parts.

Usage Notes

Example
class ParentProvider {} class ChildProvider {} var parentProviders = ReflectiveInjector.resolve([ParentProvider]); var childProviders = ReflectiveInjector.resolve([ChildProvider]); var parent = ReflectiveInjector.fromResolvedProviders(parentProviders); var child = parent.createChildFromResolved(childProviders); expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true); expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true); expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));
      
      
  1. class ParentProvider {}
  2. class ChildProvider {}
  3.  
  4. var parentProviders = ReflectiveInjector.resolve([ParentProvider]);
  5. var childProviders = ReflectiveInjector.resolve([ChildProvider]);
  6.  
  7. var parent = ReflectiveInjector.fromResolvedProviders(parentProviders);
  8. var child = parent.createChildFromResolved(childProviders);
  9.  
  10. expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true);
  11. expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true);
  12. expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));

Resolves a provider and instantiates an object in the context of the injector.

abstract resolveAndInstantiate(provider: Provider): any
      
      abstract resolveAndInstantiate(provider: Provider): any
    
Parameters
provider Provider
Returns

any

The created object does not get cached by the injector.

Usage Notes

Example
@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var injector = ReflectiveInjector.resolveAndCreate([Engine]); var car = injector.resolveAndInstantiate(Car); expect(car.engine).toBe(injector.get(Engine)); expect(car).not.toBe(injector.resolveAndInstantiate(Car));
      
      
  1. @Injectable()
  2. class Engine {
  3. }
  4.  
  5. @Injectable()
  6. class Car {
  7. constructor(public engine:Engine) {}
  8. }
  9.  
  10. var injector = ReflectiveInjector.resolveAndCreate([Engine]);
  11.  
  12. var car = injector.resolveAndInstantiate(Car);
  13. expect(car.engine).toBe(injector.get(Engine));
  14. expect(car).not.toBe(injector.resolveAndInstantiate(Car));

Instantiates an object using a resolved provider in the context of the injector.

abstract instantiateResolved(provider: ResolvedReflectiveProvider): any
      
      abstract instantiateResolved(provider: ResolvedReflectiveProvider): any
    
Parameters
provider ResolvedReflectiveProvider
Returns

any

The created object does not get cached by the injector.

Usage Notes

Example
@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var injector = ReflectiveInjector.resolveAndCreate([Engine]); var carProvider = ReflectiveInjector.resolve([Car])[0]; var car = injector.instantiateResolved(carProvider); expect(car.engine).toBe(injector.get(Engine)); expect(car).not.toBe(injector.instantiateResolved(carProvider));
      
      
  1. @Injectable()
  2. class Engine {
  3. }
  4.  
  5. @Injectable()
  6. class Car {
  7. constructor(public engine:Engine) {}
  8. }
  9.  
  10. var injector = ReflectiveInjector.resolveAndCreate([Engine]);
  11. var carProvider = ReflectiveInjector.resolve([Car])[0];
  12. var car = injector.instantiateResolved(carProvider);
  13. expect(car.engine).toBe(injector.get(Engine));
  14. expect(car).not.toBe(injector.instantiateResolved(carProvider));
abstract get(token: any, notFoundValue?: any): any
      
      abstract get(token: any, notFoundValue?: any): any
    
Parameters
token any
notFoundValue any

Optional. Default is undefined.

Returns

any

Usage notes

Example

The following example creates an Injector configured to create Engine and Car.

@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]); var car = injector.get(Car); expect(car instanceof Car).toBe(true); expect(car.engine instanceof Engine).toBe(true);
      
      
  1. @Injectable()
  2. class Engine {
  3. }
  4.  
  5. @Injectable()
  6. class Car {
  7. constructor(public engine:Engine) {}
  8. }
  9.  
  10. var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);
  11. var car = injector.get(Car);
  12. expect(car instanceof Car).toBe(true);
  13. expect(car.engine instanceof Engine).toBe(true);

Notice, we don't use the new operator because we explicitly want to have the Injector resolve all of the object's dependencies automatically.