# Class InjectorService

Class
Module
import { InjectorService } from "@tsed/di"
Source/packages/di/src/services/InjectorService.ts

# Overview

class InjectorService extends Container {
    settings: TsED.Configuration & DIConfiguration;
    logger: IDILogger;
<span class="token keyword">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
get <span class="token function">resolvers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">import</span><span class="token punctuation">(</span>"../interfaces"<span class="token punctuation">)</span>.<a href="/api/di/interfaces/IDIResolver.html"><span class="token">IDIResolver</span></a><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
get <span class="token function">scopes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token punctuation">[</span>key<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/ProviderScope.html"><span class="token">ProviderScope</span></a><span class="token punctuation">;</span>
<span class="token punctuation">}</span> & <span class="token punctuation">{</span>
    <span class="token punctuation">[</span>key<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/ProviderScope.html"><span class="token">ProviderScope</span></a><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token function">scopeOf</span><span class="token punctuation">(</span>provider<span class="token punctuation">:</span> <a href="/api/di/class/Provider.html"><span class="token">Provider</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/ProviderScope.html"><span class="token">ProviderScope</span></a><span class="token punctuation">;</span>

<span class="token function">forkProvider</span><span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> settings?<span class="token punctuation">:</span> Partial&lt;<a href="/api/di/interfaces/IProvider.html"><span class="token">IProvider</span></a>&lt;<span class="token keyword">any</span>&gt;&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/class/Provider.html"><span class="token">Provider</span></a><span class="token punctuation">;</span>

<span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

get&lt;T<span class="token punctuation"> = </span><span class="token keyword">any</span>&gt;<span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> options?<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> T | undefined<span class="token punctuation">;</span>

<span class="token function">has</span><span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">boolean</span><span class="token punctuation">;</span>

invoke&lt;T&gt;<span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> locals?<span class="token punctuation">:</span> Map&lt;<a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> <span class="token keyword">any</span>&gt;<span class="token punctuation">,</span> options?<span class="token punctuation">:</span> Partial&lt;<a href="/api/di/interfaces/InvokeOptions.html"><span class="token">InvokeOptions</span></a>&lt;T&gt;&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> T<span class="token punctuation">;</span>

<span class="token function">loadAsync</span><span class="token punctuation">(</span>locals?<span class="token punctuation">:</span> <a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> Promise&lt;<a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;&gt;<span class="token punctuation">;</span>
<span class="token function">loadSync</span><span class="token punctuation">(</span>locals?<span class="token punctuation">:</span> <a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">;</span>

<span class="token function">bootstrap</span><span class="token punctuation">(</span>container?<span class="token punctuation">:</span> <a href="/api/di/class/Container.html"><span class="token">Container</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>

<span class="token function">load</span><span class="token punctuation">(</span>container?<span class="token punctuation">:</span> <a href="/api/di/class/Container.html"><span class="token">Container</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> Promise&lt;<a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;&gt;<span class="token punctuation">;</span>

<span class="token function">resolveConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

<span class="token function">bindInjectableProperties</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> locals<span class="token punctuation">:</span> Map&lt;<a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> <span class="token keyword">any</span>&gt;<span class="token punctuation">,</span> options<span class="token punctuation">:</span> Partial&lt;<a href="/api/di/interfaces/InvokeOptions.html"><span class="token">InvokeOptions</span></a>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

<span class="token function">bindMethod</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyService.html"><span class="token">IInjectablePropertyService</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

<span class="token function">bindProperty</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> useType<span class="token punctuation">,</span> onGet<span class="token punctuation">,</span> options <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyService.html"><span class="token">IInjectablePropertyService</span></a><span class="token punctuation">,</span> locals<span class="token punctuation">:</span> Map&lt;<a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> <span class="token keyword">any</span>&gt;<span class="token punctuation">,</span> invokeOptions<span class="token punctuation">:</span> Partial&lt;<a href="/api/di/interfaces/InvokeOptions.html"><span class="token">InvokeOptions</span></a>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

<span class="token function">bindValue</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> expression<span class="token punctuation">,</span> defaultValue <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyValue.html"><span class="token">IInjectablePropertyValue</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

<span class="token function">bindConstant</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> expression<span class="token punctuation">,</span> defaultValue <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyValue.html"><span class="token">IInjectablePropertyValue</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> PropertyDescriptor<span class="token punctuation">;</span>

<span class="token function">bindInterceptor</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> useType<span class="token punctuation">,</span> options <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyService.html"><span class="token">IInjectablePropertyService</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
<span class="token keyword">protected</span> <span class="token function">ensureProvider</span><span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/class/Provider.html"><span class="token">Provider</span></a> | undefined<span class="token punctuation">;</span>

}

# Description

This service contain all services collected by @Service or services declared manually with InjectorService.factory() or InjectorService.service().

# Example:

import {InjectorService} from "@tsed/di";

// Import the services (all services are decorated with @Service()";
import MyService1 from "./services/service1";
import MyService2 from "./services/service2";
import MyService3 from "./services/service3";

// When all services is imported you can load InjectorService.
const injector = new InjectorService()

await injector.load();

const myService1 = injector.get<MyService1>(MyServcice1);
1
2
3
4
5
6
7
8
9
10
11
12
13

# Members

settings: TsED.Configuration & DIConfiguration;

logger: IDILogger;

get resolvers(): import("../interfaces").IDIResolver[];

get scopes(): {
     [key: string]: ProviderScope;
 } & {
     [key: string]: ProviderScope;
 };

scopeOf(provider: Provider<any>): ProviderScope;

Retrieve default scope for a given provider.


forkProvider(token: TokenProvider, settings?: Partial<IProvider<any>>): Provider;

Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.


toArray(): any[];

Return a list of instance build by the injector.


get<T = any>(token: TokenProvider, options?: any): T | undefined;
Param Type Description
token TokenProvider The class or symbol registered in InjectorService.

Get a service or factory already constructed from his symbol or class.

# Example

import {InjectorService} from "@tsed/di";
import MyService from "./services";

class OtherService {
     constructor(injectorService: InjectorService) {
         const myService = injectorService.get<MyService>(MyService);
     }
}
1
2
3
4
5
6
7
8

has(token: TokenProvider): boolean;

The has() method returns a boolean indicating whether an element with the specified key exists or not.


invoke<T>(token: TokenProvider, locals?: Map<TokenProvider, any>, options?: Partial<InvokeOptions<T>>): T;
Param Type Description
token TokenProvider The injectable class to invoke. Class parameters are injected according constructor signature. locals

Invoke the class and inject all services that required by the class constructor.

# Example

import {InjectorService} from "@tsed/di";
import MyService from "./services";

class OtherService {
    constructor(injectorService: InjectorService) {
         const myService = injectorService.invoke<MyService>(MyService);
     }
 }
1
2
3
4
5
6
7
8

loadAsync(locals?: LocalsContainer<any>): Promise<LocalsContainer<any>>;

Build only providers which are asynchronous.


loadSync(locals?: LocalsContainer<any>): LocalsContainer<any>;

bootstrap(container?: Container): this;

Boostrap injector from container and resolve configuration.


load(container?: Container): Promise<LocalsContainer<any>>;

Build all providers from given container (or GlobalProviders) and emit $onInit event.


resolveConfiguration(): void;

Load all configurations registered on providers


bindInjectableProperties(instance: any, locals: Map<TokenProvider, any>, options: Partial<InvokeOptions>): void;

bindMethod(instance: any, { propertyKey }: IInjectablePropertyService): void;

bindProperty(instance: any, { propertyKey, useType, onGet, options }: IInjectablePropertyService, locals: Map<TokenProvider, any>, invokeOptions: Partial<InvokeOptions>): void;

Create an injectable property.


bindValue(instance: any, { propertyKey, expression, defaultValue }: IInjectablePropertyValue): void;

bindConstant(instance: any, { propertyKey, expression, defaultValue }: IInjectablePropertyValue): PropertyDescriptor;

bindInterceptor(instance: any, { propertyKey, useType, options }: IInjectablePropertyService): void;

protected ensureProvider(token: TokenProvider): Provider | undefined;

Other topics