Documentation
¶
Overview ¶
Package core 提供了一个类型安全的依赖注入(DI)容器实现,灵感来自 Spring Framework 的 IoC 容器。
详细文档请参阅 package core 的说明。
Package core 提供了一个类型安全的依赖注入(DI)容器实现,灵感来自 Spring Framework 的 IoC 容器。
设计哲学 ¶
本框架遵循 Go 语言哲学,优先使用泛型和函数式 API,最小化反射使用。 与 Spring 的注解驱动不同,我们采用显式注册 + 泛型包装的方式, 在编译期确保类型安全,避免运行时类型断言错误。
核心特性 ¶
- 编译期类型安全:通过泛型函数 Register[T]/Get[T] 在编译期检查 Bean 类型
- 零反射注册:用户 API 层完全避免反射,仅在容器内部使用 reflect.Type 存储类型信息
- 函数式依赖:通过工厂函数 func(Container) T 显式声明依赖关系
- 生命周期管理:支持 Init/Destroy 回调和阶段监听器
- 作用域支持:Singleton(单例)和 Prototype(原型)两种作用域
- 并发安全:使用 sync.Map 优化读多写少的注册场景
- 父子容器:支持容器层级关系,子容器可访问父容器中的 Bean
架构概览 ¶
core/ # 核心包
├── doc.go # 核心接口定义(BeanDef, Container, BeanOption)
├── container.go # Scope 类型定义
├── container_impl.go # 默认容器实现
├── generic_api.go # 泛型 API 包装函数
├── errors.go # 错误定义
│
├── scope/ # 作用域管理
│ ├── doc.go # Scope, ScopeRegistry 接口
│ └── scope_impl.go # Singleton/Prototype 实现(sync.Map)
│
├── lifecycle/ # 生命周期管理
│ ├── doc.go # LifecycleManager 接口
│ └── lifecycle_impl.go # 生命周期管理器实现
│
├── binding/ # 数据绑定
│ ├── doc.go # Binder, ValueResolver, TypeConverter 接口 + Inject[T]
│ ├── binding_impl.go # 字段注入和配置绑定实现
│ └── inject_impl.go # 泛型注入实现
│
└── registry/ # Bean 注册表(内部包)
├── doc.go # BeanRegistry, BeanIDGenerator 接口
└── registry_impl.go # 注册表实现(sync.Map)
快速开始 ¶
// 1. 创建容器
container := core.NewContainer()
// 2. 注册 Bean
core.Register(container, "db", func(c core.Container) *Database {
return &Database{DSN: "localhost:3306"}
})
// 3. 注册带依赖的 Bean
core.Register(container, "userService", func(c core.Container) *UserService {
db := core.MustGet[*Database](c, "db")
return &UserService{DB: db}
})
// 4. 初始化容器(创建所有非延迟初始化的 Singleton Bean)
container.Initialize()
// 5. 获取 Bean
svc := core.MustGet[*UserService](container, "userService")
作用域配置 ¶
// 单例作用域(默认)
core.Register(container, "cache", func(c core.Container) *Cache {
return NewCache()
})
// 原型作用域(每次获取创建新实例)
core.Register(container, "request", func(c core.Container) *Request {
return &Request{}
}, core.WithScope[*Request](core.Prototype))
// 延迟初始化
core.Register(container, "expensive", func(c core.Container) *Expensive {
return &Expensive{}
}, core.WithLazy[*Expensive](true))
生命周期回调 ¶
core.Register(container, "service", func(c core.Container) *Service {
return &Service{}
}, core.WithInit(func(s *Service) error {
return s.Start()
}), core.WithDestroy(func(s *Service) error {
return s.Stop()
}))
数据绑定 ¶
// 字段注入
type MyBean struct {
DB *Database `inject:"db"`
}
binder := binding.NewBinder()
bean := &MyBean{}
binder.BindFields(bean, container)
// 配置值注入
type Config struct {
Timeout int `value:"app.timeout"`
}
resolver := binding.ValueResolverFunc(func(key string) (string, bool) {
return "30", true
})
binder.BindValue(&Config{}, resolver)
设计说明 ¶
由于 Go 语言限制(接口方法不能有类型参数),Container 接口使用 reflect.Type 存储类型信息, 但通过泛型包装函数(Register[T], Get[T] 等)提供编译期类型安全的 API。
用户应始终使用泛型 API,避免直接调用 Container 接口的 reflect.Type 方法。
Package core 错误定义。
错误分类:
- 容器状态错误:已初始化、已销毁
- Bean 查找错误:未找到、已存在
- 依赖注入错误:依赖未找到、注入失败
- 生命周期错误:初始化失败、销毁失败
Package core 提供泛型 API 包装函数,用于编译期类型安全的 Bean 注册和获取。
用户应优先使用本文件的泛型 API,而非直接调用 Container 接口的反射方法。
Index ¶
- Variables
- func GetByName[T any](container Container, name string) (T, error)
- func Has[T any](container Container, name string) bool
- func MustGet[T any](container Container, name string) T
- func Register[T any](container Container, opts ...BeanOption) error
- type BeanCreator
- type BeanGet
- type BeanIDGenerator
- type BeanOption
- func WithDestroy[T any](destroy func(bean any) error) BeanOption
- func WithFactory[T any](factory func(c ...any) (any, error)) BeanOption
- func WithInit[T any](init func(bean any) error) BeanOption
- func WithLazy[T any](lazy bool) BeanOption
- func WithName[T any](name string) BeanOption
- func WithPrimary[T any](primary bool) BeanOption
- func WithScope[T any](scope registry.Scope) BeanOption
- func WithType[T any](typ reflect.Type) BeanOption
- type BeanRegister
- type Container
- type ContainerExt
Constants ¶
This section is empty.
Variables ¶
var ( // ErrContainerAlreadyInitialized 容器已初始化,不能再注册新 Bean。 ErrContainerAlreadyInitialized = errors.New("container already initialized") // ErrContainerDestroyed 容器已销毁,不能再使用。 ErrContainerDestroyed = errors.New("container has been destroyed") )
容器状态错误。
var ( // ErrBeanNotFound Bean 未找到。 ErrBeanNotFound = errors.New("bean not found") // ErrBeanAlreadyExists Bean 已存在。 ErrBeanAlreadyExists = errors.New("bean already exists") // ErrInvalidBeanName Bean 名称无效。 ErrInvalidBeanName = errors.New("invalid bean name") )
Bean 查找错误。
var ( // ErrCircularDependency 循环依赖检测。 ErrCircularDependency = errors.New("circular dependency detected") // ErrDependencyNotFound 依赖的 Bean 未找到。 ErrDependencyNotFound = errors.New("dependency bean not found") // ErrInjectFailed 依赖注入失败。 ErrInjectFailed = errors.New("failed to inject dependencies") // ErrNilFactory 工厂函数不能为 nil。 ErrNilFactory = errors.New("factory function cannot be nil") )
依赖注入错误。
var ( // ErrInitFailed Bean 初始化失败。 ErrInitFailed = errors.New("bean initialization failed") // ErrDestroyFailed Bean 销毁失败。 ErrDestroyFailed = errors.New("bean destruction failed") )
生命周期错误。
Functions ¶
func GetByName ¶
GetByName 泛型获取函数,提供编译期类型安全的 Bean 获取。
参数:
- container: IoC 容器
- name: Bean 名称(可选)
返回:
- T: Bean 实例
- error: 错误信息
func Register ¶
func Register[T any](container Container, opts ...BeanOption) error
Register 使用泛型注册 Bean 定义,提供编译期类型安全。
参数:
- container: IoC 容器
- opts: Bean 选项(工厂函数、作用域、生命周期回调等)
示例:
core.Register(container, func(c core.Container) *UserService {
db := core.MustGet[*Database](c, "db")
return &UserService{DB: db}
})
Types ¶
type BeanCreator ¶
type BeanCreator interface {
// CreateBean 创建指定 ID 的 Bean 实例。
//
// 参数:
// - beanID: Bean ID
//
// 返回:
// - any: Bean 实例
// - error: 错误信息
CreateBean(beanID string) (any, error)
}
BeanCreator Bean 创建器接口。
用于在运行时动态创建 Bean 实例,通常用于刷新作用域、原型作用域等场景,必须在容器中已经存在对应的 Bean 定义。
type BeanGet ¶
type BeanGet interface {
// Get 获取指定类型的 Bean 实例列表。
//
// 参数:
// - typ: Bean 类型,用于类型检查
//
// 返回:
// - []any: Bean 实例列表
// - error: 错误信息
Get(typ reflect.Type) ([]any, error)
// GetByTypeAndName 获取指定名称的 Bean 实例。
//
// 参数:
// - name: Bean 名称,可以为空字符串
// - typ: Bean 类型,用于类型检查
//
// 返回:
// - any: Bean 实例
// - error: 错误信息
GetByTypeAndName(name string, typ reflect.Type) (any, error)
// GetAll 获取所有 Bean 实例列表。
//
// 参数:
// - typ: Bean 类型,用于类型检查
//
// 返回:
// - []any: Bean 实例列表
GetAll() []any
// Has 检查容器中是否存在指定类型和名称组合的 Bean。
//
// 参数:
// - name: Bean 名称,可以为空字符串
// - typ: Bean 类型,用于类型检查
//
// 返回:
// - bool: 是否存在
Has(name string, typ reflect.Type) bool
// HasType 检查容器中是否存在指定类型 Bean。
//
// 参数:
// - typ: Bean 类型,用于类型检查
//
// 返回:
// - bool: 是否存在
HasType(typ reflect.Type) bool
// Types 返回容器中所有已注册的 Bean 类型列表。
Types() []reflect.Type
// ListBeans 列出所有已注册的Bean信息
//
// 返回:
// - map[string]*registry.BeanDef: Bean 定义映射
ListBeans() map[string]*registry.BeanDef
}
type BeanIDGenerator ¶
type BeanIDGenerator interface {
// Generate 生成 Bean ID。
//
// 参数:
// - typ: Bean 类型,用于类型检查
// - customName: 自定义名称(可选)
//
// 返回:
// - string: Bean ID
Generate(typ reflect.Type, customName ...string) string
// Parse 解析 Bean ID。
//
// 参数:
// - beanID: Bean ID
//
// 返回:
// - pkgPath: 包路径
// - typeName: 类型名称
// - customName: 自定义名称
Parse(beanID string) (pkgPath, typeName, customName string)
}
BeanIDGenerator Bean ID 生成器。
负责根据类型和名称生成标准格式的 Bean ID。
type BeanOption ¶
BeanOption Bean 注册选项函数类型。
用于函数式配置 Bean 的生命周期等属性。
可用的选项函数:
- WithInit: 设置初始化回调
- WithDestroy: 设置销毁回调
- WithLazy: 设置延迟初始化
- WithScope: 设置作用域
func WithDestroy ¶
func WithDestroy[T any](destroy func(bean any) error) BeanOption
WithDestroy 设置销毁回调的选项函数。
func WithFactory ¶
func WithFactory[T any](factory func(c ...any) (any, error)) BeanOption
WithFactory 设置创建 Bean 实例的工厂函数的选项函数。
type BeanRegister ¶
type BeanRegister interface {
// RegisterBean 注册一个 Bean。
//
// 参数:
// - def: Bean 定义,包含类型信息和工厂函数
//
// 返回:
// - error: 错误信息
RegisterBean(def registry.BeanDef) error
// RegisterInstance 注册一个已存在的 Bean 实例。
//
// 参数:
// - instance: Bean 实例
// - typ: Bean 类型,用于类型检查
//
//
// 返回:
// - error: 错误信息
RegisterInstance(instance any, typ reflect.Type) error
}
BeanRegister 容器注册接口。
Bean ID 格式:包路径.类型名#自定义名称
- 包路径:Bean 类型所属的包路径
- 类型名:Bean 类型的名称
- 自定义名称:Bean 实例的自定义名称,为空时自动生成
type Container ¶
type Container interface {
BeanGet
BeanRegister
BeanIDGenerator
BeanCreator
// Initialize 初始化容器,创建所有 Singleton Bean 并调用 Init 回调。
Initialize() error
// Destroy 销毁容器,调用所有 Singleton Bean 的 Destroy 回调并清理资源。
Destroy() error
}
Container IoC 容器接口。
设计说明:
- 接口方法不使用泛型(Go 语法限制)
- 通过泛型包装函数实现编译期类型安全
- 内部使用 reflect.Type 存储类型信息
type ContainerExt ¶
type ContainerExt interface {
// SetParent 设置父容器,子容器可以获取父容器中的 Bean。
SetParent(parent Container)
// GetParent 获取父容器,如果没有父容器则返回 nil。
GetParent() Container
// Types 返回容器中所有已注册的 Bean 类型列表。
Types() []reflect.Type
// BeanCount 返回容器中已注册的 Bean 数量。
BeanCount() int
// BeanCountType 返回容器中指定类型 Bean 数量。
// 参数:
// - typ: Bean 类型,用于类型检查
// 返回:
// - int: Bean 数量
BeanCountType(typ reflect.Type) int
// Validate 验证所有已注册Bean的依赖是否可解析,检测循环依赖。
// 应在 Initialize() 之前调用,提前发现配置错误。
//
// 返回:
// - error: 验证失败时返回错误,验证通过返回 nil
Validate() error
}
ContainerExt 容器扩展接口,提供高级功能如子容器、条件注册等。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package binding 提供了数据绑定和依赖注入的底层支持。
|
Package binding 提供了数据绑定和依赖注入的底层支持。 |
|
Package lifecycle 定义了 Bean 的生命周期管理接口和实现。
|
Package lifecycle 定义了 Bean 的生命周期管理接口和实现。 |
|
Package registry 提供了 Bean 注册表的内部实现。
|
Package registry 提供了 Bean 注册表的内部实现。 |
|
Package scope 定义了 Bean 的作用域管理接口和实现。
|
Package scope 定义了 Bean 的作用域管理接口和实现。 |