Dependecy Injection
Nestjs VS Spring
| Spring |
Nest |
| @Component |
@Injectable |
| @Autowired |
Constructor Injection |
| ApplicationContext |
Nest Container |
| Singleton |
Default scope |
App startup flow:
- Nest creates DI container
- ConfigModule loads env
- TypeOrmModule waits for ConfigService
- Factory builds DB config
- TypeORM creates connection pool
- Entities registered
- Controllers/providers loaded
DI flow:
- Nest scans modules
- Sees
UserService in providers
- Registers it
- When creating
UserController
- TypeScript reflection metadata
reflect-metadata library
- Auto injects the instance