Documentation
¶
Overview ¶
Package context 提供应用上下文管理,用于 enhance 框架。
核心功能 ¶
- 容器管理: 提供 IoC 容器访问,管理 Bean 的注册和获取
- 环境配置: 提供环境配置访问,支持多级配置源
- 事件总线: 提供事件发布和订阅,支持优先级和异步
- 生命周期: 管理应用启动和关闭,控制状态流转
- 刷新作用域: 支持配置热更新时的 Bean 刷新
使用方式 ¶
获取应用上下文:
ctx := boot.NewApplication() container := ctx.Container() env := ctx.Environment()
从容器获取 Bean:
service, err := container.Get("myService")
发布事件:
ctx.PublishEvent(event.NewEvent("myEvent"))
设计模式 ¶
- Facade: ApplicationContext 作为框架核心组件的统一入口
- Adapter: asyncEventPublisherAdapter 适配 event.AsyncPublisher 为 AsyncEventPublisher
- Singleton: globalClassLoader 全局共享 ClassLoader 实例
Index ¶
- func WithBean(t reflect.Type, opts ...core.BeanOption) func(*ApplicationContextBuilder)
- func WithContainer(container core.Container) func(*ApplicationContextBuilder)
- func WithEnvironment(env *environment.Environment) func(*ApplicationContextBuilder)
- func WithProfile(profile string) func(*ApplicationContextBuilder)
- func WithProfiles(profiles ...string) func(*ApplicationContextBuilder)
- type ApplicationContext
- type ApplicationContextBuilder
- func (b *ApplicationContextBuilder) Bean(t reflect.Type, opts ...core.BeanOption) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) Build() (ApplicationContext, error)
- func (b *ApplicationContextBuilder) Container(container core.Container) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) Environment(env *environment.Environment) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) EventBus(eventBus *event.EventBusWithOrdering) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) Lifecycle(lifecycle *lifecycle.LifecycleManager) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) MustBuild() ApplicationContext
- func (b *ApplicationContextBuilder) OnApplicationReady(listener event.EventListener) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) OnApplicationStarted(listener event.EventListener) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) OnApplicationStopped(listener event.EventListener) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) Profile(profile string) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) Profiles(profiles ...string) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) RefreshScopeManager(mgr *refresh.RefreshScopeManager) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) WithEventListener(eventType string, listener event.EventListener) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) WithPhaseListener(listener lifecycle.PhaseListener) *ApplicationContextBuilder
- func (b *ApplicationContextBuilder) WithRefreshOption(opts ...refresh.RefreshOption) *ApplicationContextBuilder
- type ApplicationContextHelper
- func (h *ApplicationContextHelper) GetActiveProfiles() []string
- func (h *ApplicationContextHelper) GetBeanByType(t reflect.Type) (any, error)
- func (h *ApplicationContextHelper) GetBeanByTypeOrDefault(t reflect.Type, defaultVal any) any
- func (h *ApplicationContextHelper) GetBoolProperty(key string, defaultVal bool) bool
- func (h *ApplicationContextHelper) GetIntProperty(key string, defaultVal int) int
- func (h *ApplicationContextHelper) GetPhase() lifecycle.ApplicationPhase
- func (h *ApplicationContextHelper) GetProperty(key string, defaultVal string) string
- func (h *ApplicationContextHelper) HasBeanByType(t reflect.Type) bool
- func (h *ApplicationContextHelper) Invoke(fn any) error
- func (h *ApplicationContextHelper) IsDev() bool
- func (h *ApplicationContextHelper) IsProd() bool
- func (h *ApplicationContextHelper) IsRunning() bool
- func (h *ApplicationContextHelper) PublishEvent(eventType string)
- func (h *ApplicationContextHelper) PublishReady()
- func (h *ApplicationContextHelper) PublishStarted()
- func (h *ApplicationContextHelper) PublishStopped()
- type ApplicationRunner
- type AsyncEventPublisher
- type DefaultApplicationContext
- func (c *DefaultApplicationContext) AsyncEventPublisher() AsyncEventPublisher
- func (c *DefaultApplicationContext) Container() core.Container
- func (c *DefaultApplicationContext) Environment() *environment.Environment
- func (c *DefaultApplicationContext) EventBus() EventBusAccess
- func (c *DefaultApplicationContext) EventPublisher() EventPublisher
- func (c *DefaultApplicationContext) GetByType(t reflect.Type) (any, error)
- func (c *DefaultApplicationContext) GetProperty(key string) (any, bool)
- func (c *DefaultApplicationContext) HasProperty(key string) bool
- func (c *DefaultApplicationContext) Invoke(fn any) error
- func (c *DefaultApplicationContext) IsRunning() bool
- func (c *DefaultApplicationContext) Lifecycle() *lifecycle.LifecycleManager
- func (c *DefaultApplicationContext) RefreshScopeManager() *refresh.RefreshScopeManager
- func (c *DefaultApplicationContext) Register(t reflect.Type, opts ...core.BeanOption) error
- func (c *DefaultApplicationContext) Start() error
- func (c *DefaultApplicationContext) Stop() error
- type EventBusAccess
- type EventPublisher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithBean ¶
func WithBean(t reflect.Type, opts ...core.BeanOption) func(*ApplicationContextBuilder)
WithBean 注册Bean的选项函数
func WithContainer ¶
func WithContainer(container core.Container) func(*ApplicationContextBuilder)
WithContainer 设置容器的选项函数
func WithEnvironment ¶
func WithEnvironment(env *environment.Environment) func(*ApplicationContextBuilder)
WithEnvironment 设置环境的选项函数
func WithProfile ¶
func WithProfile(profile string) func(*ApplicationContextBuilder)
WithProfile 添加Profile的选项函数
func WithProfiles ¶
func WithProfiles(profiles ...string) func(*ApplicationContextBuilder)
WithProfiles 批量添加Profiles的选项函数
Types ¶
type ApplicationContext ¶
type ApplicationContext interface {
// Container 返回 IoC 容器实例。
Container() core.Container
// Environment 返回环境配置实例。
Environment() *environment.Environment
// Lifecycle 返回生命周期管理器。
Lifecycle() *lifecycle.LifecycleManager
// EventBus 返回事件总线访问接口(支持 EventBus 和 EventBusWithOrdering)。
EventBus() EventBusAccess
// EventPublisher 返回事件发布器接口(解耦事件发布)。
EventPublisher() EventPublisher
// AsyncEventPublisher 返回异步事件发布器接口。
AsyncEventPublisher() AsyncEventPublisher
// RefreshScopeManager 返回刷新作用域管理器。
RefreshScopeManager() *refresh.RefreshScopeManager
// Start 启动应用:发布启动事件并切换至运行阶段。
Start() error
// Stop 停止应用:切换至停止阶段并发布停止事件。
Stop() error
}
ApplicationContext 应用上下文接口。
作为框架核心组件的统一入口(Facade 模式),聚合对各个子系统的访问。 不重复定义已有方法,通过 Container() 获取 core.Container 来操作 Bean, 通过 Lifecycle() 获取 lifecycle.LifecycleManager 来控制生命周期。
该接口遵循小接口原则,包含以下方法:
- Container: 获取依赖注入容器
- Environment: 获取环境配置
- Lifecycle: 获取生命周期管理器
- EventBus: 获取事件总线访问
- EventPublisher: 获取事件发布器
- AsyncEventPublisher: 获取异步事件发布器
- RefreshScopeManager: 获取刷新作用域管理器
- Start: 启动应用,发布生命周期事件
- Stop: 停止应用,发布生命周期事件
func CreateApplicationContext ¶
func CreateApplicationContext(opts ...func(*ApplicationContextBuilder)) (ApplicationContext, error)
CreateApplicationContext 创建并配置应用上下文的便捷函数
type ApplicationContextBuilder ¶
type ApplicationContextBuilder struct {
// contains filtered or unexported fields
}
ApplicationContextBuilder 应用上下文构建器,支持链式配置
func NewApplicationContextBuilder ¶
func NewApplicationContextBuilder() *ApplicationContextBuilder
NewApplicationContextBuilder 创建应用上下文构建器
func (*ApplicationContextBuilder) Bean ¶
func (b *ApplicationContextBuilder) Bean(t reflect.Type, opts ...core.BeanOption) *ApplicationContextBuilder
Bean 注册Bean定义
func (*ApplicationContextBuilder) Build ¶
func (b *ApplicationContextBuilder) Build() (ApplicationContext, error)
Build 构建应用上下文
func (*ApplicationContextBuilder) Container ¶
func (b *ApplicationContextBuilder) Container(container core.Container) *ApplicationContextBuilder
Container 设置IoC容器
func (*ApplicationContextBuilder) Environment ¶
func (b *ApplicationContextBuilder) Environment(env *environment.Environment) *ApplicationContextBuilder
Environment 设置环境配置
func (*ApplicationContextBuilder) EventBus ¶
func (b *ApplicationContextBuilder) EventBus(eventBus *event.EventBusWithOrdering) *ApplicationContextBuilder
EventBus 设置事件总线
func (*ApplicationContextBuilder) Lifecycle ¶
func (b *ApplicationContextBuilder) Lifecycle(lifecycle *lifecycle.LifecycleManager) *ApplicationContextBuilder
Lifecycle 设置生命周期管理器
func (*ApplicationContextBuilder) MustBuild ¶
func (b *ApplicationContextBuilder) MustBuild() ApplicationContext
MustBuild 构建应用上下文,失败则panic
func (*ApplicationContextBuilder) OnApplicationReady ¶
func (b *ApplicationContextBuilder) OnApplicationReady(listener event.EventListener) *ApplicationContextBuilder
OnApplicationReady 注册应用就绪事件监听器
func (*ApplicationContextBuilder) OnApplicationStarted ¶
func (b *ApplicationContextBuilder) OnApplicationStarted(listener event.EventListener) *ApplicationContextBuilder
OnApplicationStarted 注册应用启动事件监听器
func (*ApplicationContextBuilder) OnApplicationStopped ¶
func (b *ApplicationContextBuilder) OnApplicationStopped(listener event.EventListener) *ApplicationContextBuilder
OnApplicationStopped 注册应用停止事件监听器
func (*ApplicationContextBuilder) Profile ¶
func (b *ApplicationContextBuilder) Profile(profile string) *ApplicationContextBuilder
Profile 添加激活的 Profile
func (*ApplicationContextBuilder) Profiles ¶
func (b *ApplicationContextBuilder) Profiles(profiles ...string) *ApplicationContextBuilder
Profiles 批量添加激活的 Profiles
func (*ApplicationContextBuilder) RefreshScopeManager ¶
func (b *ApplicationContextBuilder) RefreshScopeManager(mgr *refresh.RefreshScopeManager) *ApplicationContextBuilder
RefreshScopeManager 设置刷新作用域管理器
func (*ApplicationContextBuilder) WithEventListener ¶
func (b *ApplicationContextBuilder) WithEventListener(eventType string, listener event.EventListener) *ApplicationContextBuilder
WithEventListener 添加事件监听器
func (*ApplicationContextBuilder) WithPhaseListener ¶
func (b *ApplicationContextBuilder) WithPhaseListener(listener lifecycle.PhaseListener) *ApplicationContextBuilder
WithPhaseListener 添加阶段监听器
func (*ApplicationContextBuilder) WithRefreshOption ¶
func (b *ApplicationContextBuilder) WithRefreshOption(opts ...refresh.RefreshOption) *ApplicationContextBuilder
WithRefreshOption 添加刷新配置选项
type ApplicationContextHelper ¶
type ApplicationContextHelper struct {
// contains filtered or unexported fields
}
ApplicationContextHelper 应用上下文辅助工具
func NewApplicationContextHelper ¶
func NewApplicationContextHelper(ctx ApplicationContext) *ApplicationContextHelper
NewApplicationContextHelper 创建应用上下文辅助工具
func (*ApplicationContextHelper) GetActiveProfiles ¶
func (h *ApplicationContextHelper) GetActiveProfiles() []string
GetActiveProfiles 获取激活的Profiles
func (*ApplicationContextHelper) GetBeanByType ¶
func (h *ApplicationContextHelper) GetBeanByType(t reflect.Type) (any, error)
GetBeanByType 按类型获取Bean
func (*ApplicationContextHelper) GetBeanByTypeOrDefault ¶
func (h *ApplicationContextHelper) GetBeanByTypeOrDefault(t reflect.Type, defaultVal any) any
GetBeanByTypeOrDefault 按类型获取Bean,如果不存在返回默认值
func (*ApplicationContextHelper) GetBoolProperty ¶
func (h *ApplicationContextHelper) GetBoolProperty(key string, defaultVal bool) bool
GetBoolProperty 获取布尔类型属性
func (*ApplicationContextHelper) GetIntProperty ¶
func (h *ApplicationContextHelper) GetIntProperty(key string, defaultVal int) int
GetIntProperty 获取整数类型属性
func (*ApplicationContextHelper) GetPhase ¶
func (h *ApplicationContextHelper) GetPhase() lifecycle.ApplicationPhase
GetPhase 获取当前生命周期阶段
func (*ApplicationContextHelper) GetProperty ¶
func (h *ApplicationContextHelper) GetProperty(key string, defaultVal string) string
GetProperty 获取字符串类型属性
func (*ApplicationContextHelper) HasBeanByType ¶
func (h *ApplicationContextHelper) HasBeanByType(t reflect.Type) bool
HasBeanByType 检查指定类型的Bean是否存在
func (*ApplicationContextHelper) Invoke ¶
func (h *ApplicationContextHelper) Invoke(fn any) error
Invoke 调用函数并自动注入依赖
func (*ApplicationContextHelper) IsDev ¶
func (h *ApplicationContextHelper) IsDev() bool
IsDev 检查是否为开发环境
func (*ApplicationContextHelper) IsProd ¶
func (h *ApplicationContextHelper) IsProd() bool
IsProd 检查是否为生产环境
func (*ApplicationContextHelper) IsRunning ¶
func (h *ApplicationContextHelper) IsRunning() bool
IsRunning 检查应用是否运行中
func (*ApplicationContextHelper) PublishEvent ¶
func (h *ApplicationContextHelper) PublishEvent(eventType string)
PublishEvent 发布事件
func (*ApplicationContextHelper) PublishReady ¶
func (h *ApplicationContextHelper) PublishReady()
PublishReady 发布应用就绪事件
func (*ApplicationContextHelper) PublishStarted ¶
func (h *ApplicationContextHelper) PublishStarted()
PublishStarted 发布应用启动事件
func (*ApplicationContextHelper) PublishStopped ¶
func (h *ApplicationContextHelper) PublishStopped()
PublishStopped 发布应用停止事件
type ApplicationRunner ¶
type ApplicationRunner struct {
// contains filtered or unexported fields
}
ApplicationRunner 应用运行器,简化应用的启动和停止
func NewApplicationRunner ¶
func NewApplicationRunner(ctx ApplicationContext) *ApplicationRunner
NewApplicationRunner 创建应用运行器
func (*ApplicationRunner) Context ¶
func (r *ApplicationRunner) Context() ApplicationContext
Context 获取应用上下文
type AsyncEventPublisher ¶
type AsyncEventPublisher interface {
// PublishAsync 异步发布事件,使用背景上下文。
PublishAsync(event event.ApplicationEvent)
// PublishAsyncWithCtx 异步发布事件,使用指定上下文(支持超时控制)。
PublishAsyncWithCtx(ctx context.Context, event event.ApplicationEvent)
}
AsyncEventPublisher 异步事件发布器接口。
支持异步事件发布,不阻塞调用者。
type DefaultApplicationContext ¶
type DefaultApplicationContext struct {
// contains filtered or unexported fields
}
DefaultApplicationContext 默认应用上下文实现。
组合了 IoC 容器、环境配置、生命周期管理和事件总线, 提供 enhance 框架的核心运行时能力。
func NewApplicationContext ¶
func NewApplicationContext(container core.Container, env *environment.Environment, opts ...refresh.RefreshOption) *DefaultApplicationContext
NewApplicationContext 创建默认应用上下文实例。
func (*DefaultApplicationContext) AsyncEventPublisher ¶
func (c *DefaultApplicationContext) AsyncEventPublisher() AsyncEventPublisher
func (*DefaultApplicationContext) Container ¶
func (c *DefaultApplicationContext) Container() core.Container
func (*DefaultApplicationContext) Environment ¶
func (c *DefaultApplicationContext) Environment() *environment.Environment
func (*DefaultApplicationContext) EventBus ¶
func (c *DefaultApplicationContext) EventBus() EventBusAccess
func (*DefaultApplicationContext) EventPublisher ¶
func (c *DefaultApplicationContext) EventPublisher() EventPublisher
func (*DefaultApplicationContext) GetByType ¶
func (c *DefaultApplicationContext) GetByType(t reflect.Type) (any, error)
func (*DefaultApplicationContext) GetProperty ¶
func (c *DefaultApplicationContext) GetProperty(key string) (any, bool)
func (*DefaultApplicationContext) HasProperty ¶
func (c *DefaultApplicationContext) HasProperty(key string) bool
func (*DefaultApplicationContext) Invoke ¶
func (c *DefaultApplicationContext) Invoke(fn any) error
func (*DefaultApplicationContext) IsRunning ¶
func (c *DefaultApplicationContext) IsRunning() bool
IsRunning 检查应用是否运行中
func (*DefaultApplicationContext) Lifecycle ¶
func (c *DefaultApplicationContext) Lifecycle() *lifecycle.LifecycleManager
func (*DefaultApplicationContext) RefreshScopeManager ¶
func (c *DefaultApplicationContext) RefreshScopeManager() *refresh.RefreshScopeManager
func (*DefaultApplicationContext) Register ¶
func (c *DefaultApplicationContext) Register(t reflect.Type, opts ...core.BeanOption) error
func (*DefaultApplicationContext) Start ¶
func (c *DefaultApplicationContext) Start() error
Start 启动应用:PhaseInit → PhaseRunning
func (*DefaultApplicationContext) Stop ¶
func (c *DefaultApplicationContext) Stop() error
Stop 停止应用:PhaseRunning → PhaseStopped
type EventBusAccess ¶
type EventBusAccess interface {
// Publish 发布事件。
Publish(event event.ApplicationEvent)
// Subscribe 订阅指定类型的事件。
Subscribe(eventType string, listener event.EventListener)
// Unsubscribe 取消订阅指定类型的事件。
Unsubscribe(eventType string, target event.EventListener)
}
EventBusAccess 事件总线访问接口。
定义事件总线的公共操作,支持 EventBus 和 EventBusWithOrdering。
type EventPublisher ¶
type EventPublisher interface {
// Publish 发布事件。
Publish(event event.ApplicationEvent)
}
EventPublisher 事件发布器接口。
用于解耦事件发布逻辑,便于测试和替换实现。