Dependecy Injection

Nestjs VS Spring

Spring Nest
@Component @Injectable
@Autowired Constructor Injection
ApplicationContext Nest Container
Singleton Default scope

App startup flow:

  1. Nest creates DI container
  2. ConfigModule loads env
  3. TypeOrmModule waits for ConfigService
  4. Factory builds DB config
  5. TypeORM creates connection pool
  6. Entities registered
  7. Controllers/providers loaded

DI flow:

  1. Nest scans modules
  2. Sees UserService in providers
  3. Registers it
  4. When creating UserController
  5. Auto injects the instance