FastAPI 注入(fastapi_inject)¶
symphra_container.integrations.fastapi_inject(service_type)
¶
创建 FastAPI 依赖注入函数.
用于在 FastAPI 路由中注入服务。返回的函数可以作为 Depends() 的参数。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
service_type
|
type[T]
|
要注入的服务类型 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
Callable |
Callable[[], T]
|
可用于 Depends() 的依赖函数 |
引发:
| 类型 | 描述 |
|---|---|
RuntimeError
|
如果容器未初始化 |
ServiceNotFoundError
|
如果服务未注册 |
示例
@app.get("/users") async def get_users( ... user_service: UserService = Depends(inject(UserService)) ... ): ... return await user_service.get_all()