Page Contents

Dependency Injection Decorator

@inject is a decorator to annotate class properties or constructor arguments for automatic injection by LoopBack’s IoC container.

The injected values are applied to a constructed instance, so it can only be used on non-static properties or constructor parameters of a Class.

The @inject decorator allows you to inject dependencies bound to any implementation of the Context object, such as an Application instance or a request context instance. You can bind values, class definitions, and provider functions to those contexts and then resolve the values (or the results of functions that return those values!) in other areas of your code.


import {Application} from '@loopback/core';
import * as fs from 'fs-extra';
import * as path from 'path';

export class MyApp extends RestApplication {
  constructor() {
    const app = this;
    const widgetConf = JSON.parse(
    function logInfo(info: string) {

Now that we’ve bound the ‘config.widget’ key to our configuration object, and the ‘logger.widget’ key to the function logInfo(), we can inject them in our WidgetController:


import {inject} from '@loopback/context';

export class WidgetController {
  // injection for property
  private logger: Function;

  // injection for constructor parameter
    @inject('config.widget') protected widget: any, // This will be resolved at runtime!
  ) {}
  // etc...

A few variants of @inject are provided to declare special forms of dependencies:

  • @inject.getter: inject a getter function that returns a promise of the bound value of the key

Syntax: @inject.getter(bindingKey: string).

import {inject, Getter} from '@loopback/context';
import {UserProfile} from '@loopback/authentication';
import {get} from '@loopback/rest';

export class HelloController {
    private userGetter: Getter<UserProfile>,
  ) {}

  async greet() {
    const user = await this.userGetter();
    return `Hello, ${}`;
  • @inject.setter: inject a setter function to set the bound value of the key

Syntax: @inject.setter(bindingKey: string).

export class HelloController {
    @inject.setter('greeting') private greetingSetter: Setter<string>,
  ) {}

  greet() {
    const defaultGreet = 'Greetings!';
    this.greetingSetter(defaultGreet); // key 'greeting' is now bound to 'Greetings!'
    return defaultGreet;
  • @inject.tag: inject an array of values by a pattern or regexp to match binding tags

Syntax: @inject.tag(tag: string | RegExp).

class Store {
  constructor(@inject.tag('store:location') public locations: string[]) {}

const ctx = new Context();
  .to('San Francisco')
  .to('San Jose')
const store = ctx.getSync<Store>('store');
console.log(store.locations); // ['San Francisco', 'San Jose']
  • @inject.context: inject the current context

Syntax: @inject.context().

class MyComponent {
  constructor(@inject.context() public ctx: Context) {}

const ctx = new Context();
const component = ctx.getSync<MyComponent>('my-component');
// `component.ctx` should be the same as `ctx`

NOTE: It’s recommended to use @inject with specific keys for dependency injection if possible. Use @inject.context only when the code needs to access the current context object for advanced use cases.

For more information, see the Dependency Injection section under LoopBack Core Concepts.