simpleioc

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 GetBean

func GetBean[T any]() (*T, error)

GetBean 从默认容器按类型获取 bean。

func GetBeanFrom

func GetBeanFrom[T any](container *Container) (*T, error)

GetBeanFrom 从指定容器按类型获取 bean。

func GetCache

func GetCache() cache.Rediser

GetCache 获取redis实例 Deprecated: 使用 Get[*cache.RedisCache]()。

func GetCronJobInstance

func GetCronJobInstance() *cron.Cron

GetCronJobInstance 获取定时任务实例 Deprecated: 使用 Get[*cron.Cron]()。

func GetDb

func GetDb() *gorm.DB

GetDb 获取数据库实例 Deprecated: 使用 Get[*gorm.DB]()。

func GetMongoDb

func GetMongoDb() *mongodb.Client

GetMongoDb 获取MongoDbClient Deprecated: 使用 Get[*mongodb.Client]()。

func GetNamedBean

func GetNamedBean[T any](name string) (*T, error)

GetNamedBean 从默认容器按名称获取 bean。

func GetNamedBeanFrom

func GetNamedBeanFrom[T any](container *Container, name string) (*T, error)

GetNamedBeanFrom 从指定容器按名称获取 bean。

func HasBean

func HasBean[T any]() bool

HasBean 判断默认容器是否已注册该类型(不触发构造)。

func HasBeanIn

func HasBeanIn[T any](container *Container) bool

HasBeanIn 判断指定容器是否已注册该类型(不触发构造)。

func MustGetBean

func MustGetBean[T any]() *T

MustGetBean 从默认容器按类型获取 bean,失败时 panic(仅限启动期使用)。

func MustGetBeanFrom

func MustGetBeanFrom[T any](container *Container) *T

MustGetBeanFrom 从指定容器按类型获取 bean,失败时 panic(仅限启动期使用)。

func Register

func Register[T any](provider func() *T) error

Register 在默认容器注册类型单例 provider。 provider 在首次 GetBean 或 Start 时执行;同类型重复注册返回 ErrDuplicate。

func RegisterInstance

func RegisterInstance[T any](instance *T) error

RegisterInstance 在默认容器直接注册已构造实例(等价旧 Set 语义)。

func RegisterInstanceTo

func RegisterInstanceTo[T any](container *Container, instance *T) error

RegisterInstanceTo 在指定容器直接注册已构造实例。

func RegisterNamed

func RegisterNamed[T any](name string, provider func() *T) error

RegisterNamed 在默认容器注册具名单例 provider。

func RegisterNamedTo

func RegisterNamedTo[T any](container *Container, name string, provider func() *T) error

RegisterNamedTo 在指定容器注册具名单例 provider。

func RegisterPrototype

func RegisterPrototype[T any](provider func() *T) error

RegisterPrototype 在默认容器注册原型(多例)provider,每次获取返回新实例。

func RegisterPrototypeTo

func RegisterPrototypeTo[T any](container *Container, provider func() *T) error

RegisterPrototypeTo 在指定容器注册原型(多例)provider。

func RegisterTo

func RegisterTo[T any](container *Container, provider func() *T) error

RegisterTo 在指定容器注册类型单例 provider。

func Reset

func Reset()

Reset 重置默认容器(测试隔离专用)。

func Set

func Set(beans ...any)

Set 将 struct 类型对象放入容器中(仅接受 *struct)。 Deprecated: 新代码使用 Register / RegisterInstance。 保留旧语义:重复注册忽略;非结构体指针 panic。

func Shutdown

func Shutdown(ctx context.Context) error

Shutdown 关闭默认容器:逆序执行 OnDestroy,之后容器不可再注册/获取。

func Start

func Start(ctx context.Context) error

Start 启动默认容器:按注册顺序构造单例并执行 OnInit。

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。

func (*Container) Shutdown

func (c *Container) Shutdown(ctx context.Context) error

Shutdown 按注册逆序对已构造单例调用 OnDestroy,之后容器进入关闭态。 Shutdown 幂等;多个关闭错误使用 errors.Join 聚合。

func (*Container) Start

func (c *Container) Start(ctx context.Context) error

Start 按注册顺序构造全部单例,并对实现 Initializer 的实例调用 OnInit。 Start 幂等:重复调用不会重复初始化已初始化的实例。

type Disposer

type Disposer interface {
	OnDestroy(ctx context.Context) error
}

Disposer 由需要在关闭时释放资源的单例实现。 容器 Shutdown 时按注册逆序调用 OnDestroy;返回错误会被聚合返回。 注意:原型(多例)bean 不参与生命周期管理。

type GlobalContext

type GlobalContext struct {
	// 上下文实例
	Ctx context.Context
}

GlobalContext 自定义封装全局上下文

func GetContext

func GetContext() *GlobalContext

GetContext 获取全局上下文 Deprecated: 使用 Get[*GlobalContext]()。

type Initializer

type Initializer interface {
	OnInit(ctx context.Context) error
}

Initializer 由需要在启动时初始化的单例实现。 容器 Start 时按注册顺序调用 OnInit;返回错误会终止启动流程。 注意:原型(多例)bean 不参与生命周期管理。

type Scope

type Scope uint8

Scope 定义 bean 的实例作用域。

const (
	// ScopeSingleton 单例:整个容器共享一个实例(默认)。
	// 单例使用懒加载,首次 Get 或 Start 时构造,之后复用。
	ScopeSingleton Scope = iota
	// ScopePrototype 原型(多例):每次 Get 都创建新实例,容器不管理其生命周期。
	ScopePrototype
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL