Documentation
¶
Index ¶
- Variables
- func Get[T any](bean T) T
- func GetBean[T any]() (*T, error)
- func GetBeanFrom[T any](container *Container) (*T, error)
- func GetCache() cache.Rediser
- func GetCronJobInstance() *cron.Cron
- func GetDb() *gorm.DB
- func GetMongoDb() *mongodb.Client
- func GetNamedBean[T any](name string) (*T, error)
- func GetNamedBeanFrom[T any](container *Container, name string) (*T, error)
- func HasBean[T any]() bool
- func HasBeanIn[T any](container *Container) bool
- func MustGetBean[T any]() *T
- func MustGetBeanFrom[T any](container *Container) *T
- func Register[T any](provider func() *T) error
- func RegisterInstance[T any](instance *T) error
- func RegisterInstanceTo[T any](container *Container, instance *T) error
- func RegisterNamed[T any](name string, provider func() *T) error
- func RegisterNamedTo[T any](container *Container, name string, provider func() *T) error
- func RegisterPrototype[T any](provider func() *T) error
- func RegisterPrototypeTo[T any](container *Container, provider func() *T) error
- func RegisterTo[T any](container *Container, provider func() *T) error
- func Reset()
- func Set(beans ...any)
- func Shutdown(ctx context.Context) error
- func Start(ctx context.Context) error
- type Container
- type Disposer
- type GlobalContext
- type Initializer
- type Scope
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound 表示请求的类型或名称未注册。 ErrNotFound = errors.New("simpleioc: bean not found") // ErrDuplicate 表示同类型或同名称的 bean 已存在。 ErrDuplicate = errors.New("simpleioc: bean already registered") // ErrInvalidProvider 表示 provider 为 nil 或返回 nil 实例。 ErrInvalidProvider = errors.New("simpleioc: provider is nil or returned nil") // ErrInvalidType 表示注册/获取的泛型类型不是结构体指针。 ErrInvalidType = errors.New("simpleioc: type must be a pointer to struct") // ErrCircularDependency 表示构造过程中递归获取同一个 bean。 ErrCircularDependency = errors.New("simpleioc: circular dependency detected") // ErrContainerClosed 表示容器已关闭,禁止继续注册或获取。 ErrContainerClosed = errors.New("simpleioc: container is closed") )
容器错误定义。调用方应使用 errors.Is 判断具体失败原因。
Functions ¶
func Get ¶
func Get[T any](bean T) T
Get 从容器中获取与参数类型一致的 *struct 对象。 Deprecated: 新代码使用泛型 Get[T]() 并处理错误。 保留旧语义:未注册或类型不合法时返回原参数值。
func GetBeanFrom ¶
GetBeanFrom 从指定容器按类型获取 bean。
func GetCronJobInstance ¶
GetCronJobInstance 获取定时任务实例 Deprecated: 使用 Get[*cron.Cron]()。
func GetMongoDb ¶
func GetMongoDb() *mongodb.Client
GetMongoDb 获取MongoDbClient Deprecated: 使用 Get[*mongodb.Client]()。
func GetNamedBeanFrom ¶
GetNamedBeanFrom 从指定容器按名称获取 bean。
func MustGetBeanFrom ¶
MustGetBeanFrom 从指定容器按类型获取 bean,失败时 panic(仅限启动期使用)。
func Register ¶
Register 在默认容器注册类型单例 provider。 provider 在首次 GetBean 或 Start 时执行;同类型重复注册返回 ErrDuplicate。
func RegisterInstance ¶
RegisterInstance 在默认容器直接注册已构造实例(等价旧 Set 语义)。
func RegisterInstanceTo ¶
RegisterInstanceTo 在指定容器直接注册已构造实例。
func RegisterNamed ¶
RegisterNamed 在默认容器注册具名单例 provider。
func RegisterNamedTo ¶
RegisterNamedTo 在指定容器注册具名单例 provider。
func RegisterPrototype ¶
RegisterPrototype 在默认容器注册原型(多例)provider,每次获取返回新实例。
func RegisterPrototypeTo ¶
RegisterPrototypeTo 在指定容器注册原型(多例)provider。
func RegisterTo ¶
RegisterTo 在指定容器注册类型单例 provider。
func Set ¶
func Set(beans ...any)
Set 将 struct 类型对象放入容器中(仅接受 *struct)。 Deprecated: 新代码使用 Register / RegisterInstance。 保留旧语义:重复注册忽略;非结构体指针 panic。
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container 是线程安全的依赖容器。 支持类型注册、具名注册、单例/原型作用域、懒加载构造、循环依赖检测与生命周期钩子。 泛型 API 由包级函数提供(Go 不支持泛型方法):Register/Get/GetFrom 等。
func NewContainer ¶
func NewContainer() *Container
NewContainer 创建独立容器,适用于测试隔离与多应用场景。 进程级默认容器使用包级函数访问。
func (*Container) Reset ¶
func (c *Container) Reset()
Reset 清空容器全部注册与实例,恢复到新建状态(测试隔离专用)。 Reset 不会调用任何 OnDestroy。
type Disposer ¶
Disposer 由需要在关闭时释放资源的单例实现。 容器 Shutdown 时按注册逆序调用 OnDestroy;返回错误会被聚合返回。 注意:原型(多例)bean 不参与生命周期管理。
type GlobalContext ¶
GlobalContext 自定义封装全局上下文
func GetContext ¶
func GetContext() *GlobalContext
GetContext 获取全局上下文 Deprecated: 使用 Get[*GlobalContext]()。
type Initializer ¶
Initializer 由需要在启动时初始化的单例实现。 容器 Start 时按注册顺序调用 OnInit;返回错误会终止启动流程。 注意:原型(多例)bean 不参与生命周期管理。