Documentation
¶
Overview ¶
Package gs 实现了 go-spring 的核心骨架,包含 IoC 容器、基于 IoC 容器的 App 以及全局 App 对象封装三个部分,可以应用于多种使用场景。
Index ¶
- Constants
- Variables
- func Banner(banner string)
- func Consume(fn interface{}, topics ...string)
- func DeleteBinding(path string, fn interface{}) *web.Mapper
- func DeleteMapping(path string, fn web.HandlerFunc) *web.Mapper
- func GetBinding(path string, fn interface{}) *web.Mapper
- func GetMapping(path string, fn web.HandlerFunc) *web.Mapper
- func Go(fn func(ctx context.Context))
- func HandleDelete(path string, h web.Handler) *web.Mapper
- func HandleGet(path string, h web.Handler) *web.Mapper
- func HandlePost(path string, h web.Handler) *web.Mapper
- func HandlePut(path string, h web.Handler) *web.Mapper
- func HandleRequest(method uint32, path string, h web.Handler) *web.Mapper
- func OnProperty(key string, fn interface{})
- func PostBinding(path string, fn interface{}) *web.Mapper
- func PostMapping(path string, fn web.HandlerFunc) *web.Mapper
- func PrintBanner(banner string)
- func Property(key string, value interface{})
- func PutBinding(path string, fn interface{}) *web.Mapper
- func PutMapping(path string, fn web.HandlerFunc) *web.Mapper
- func RequestBinding(method uint32, path string, fn interface{}) *web.Mapper
- func RequestMapping(method uint32, path string, fn web.HandlerFunc) *web.Mapper
- func Run() error
- func Setenv(key string, value string)
- func ShutDown(err error)
- type App
- func (app *App) Banner(banner string)
- func (app *App) Consume(fn interface{}, topics ...string)
- func (app *App) DeleteBinding(path string, fn interface{}) *web.Mapper
- func (app *App) DeleteMapping(path string, fn web.HandlerFunc) *web.Mapper
- func (app *App) GetBinding(path string, fn interface{}) *web.Mapper
- func (app *App) GetMapping(path string, fn web.HandlerFunc) *web.Mapper
- func (app *App) Go(fn func(ctx context.Context))
- func (app *App) GrpcClient(fn interface{}, endpoint string) *BeanDefinition
- func (app *App) GrpcServer(serviceName string, fn interface{}, service interface{}) *BeanDefinition
- func (app *App) HandleDelete(path string, h web.Handler) *web.Mapper
- func (app *App) HandleGet(path string, h web.Handler) *web.Mapper
- func (app *App) HandlePost(path string, h web.Handler) *web.Mapper
- func (app *App) HandlePut(path string, h web.Handler) *web.Mapper
- func (app *App) HandleRequest(method uint32, path string, h web.Handler) *web.Mapper
- func (app *App) Object(i interface{}) *BeanDefinition
- func (app *App) OnProperty(key string, fn interface{})
- func (app *App) PostBinding(path string, fn interface{}) *web.Mapper
- func (app *App) PostMapping(path string, fn web.HandlerFunc) *web.Mapper
- func (app *App) Property(key string, value interface{})
- func (app *App) Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition
- func (app *App) PutBinding(path string, fn interface{}) *web.Mapper
- func (app *App) PutMapping(path string, fn web.HandlerFunc) *web.Mapper
- func (app *App) RequestBinding(method uint32, path string, fn interface{}) *web.Mapper
- func (app *App) RequestMapping(method uint32, path string, fn web.HandlerFunc) *web.Mapper
- func (app *App) Run() error
- func (app *App) ShutDown(err error)
- type AppContext
- type BeanDefinition
- func GrpcClient(fn interface{}, endpoint string) *BeanDefinition
- func GrpcServer(serviceName string, fn interface{}, service interface{}) *BeanDefinition
- func NewBean(objOrCtor interface{}, ctorArgs ...arg.Arg) *BeanDefinition
- func Object(i interface{}) *BeanDefinition
- func Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition
- func (d *BeanDefinition) BeanName() string
- func (d *BeanDefinition) DependsOn(selectors ...bean.Selector) *BeanDefinition
- func (d *BeanDefinition) Destroy(fn interface{}) *BeanDefinition
- func (d *BeanDefinition) Export(exports ...interface{}) *BeanDefinition
- func (d *BeanDefinition) FileLine() string
- func (d *BeanDefinition) ID() string
- func (d *BeanDefinition) Init(fn interface{}) *BeanDefinition
- func (d *BeanDefinition) Interface() interface{}
- func (d *BeanDefinition) Match(typeName string, beanName string) bool
- func (d *BeanDefinition) Name(name string) *BeanDefinition
- func (d *BeanDefinition) On(cond cond.Condition) *BeanDefinition
- func (d *BeanDefinition) Order(order int) *BeanDefinition
- func (d *BeanDefinition) Primary() *BeanDefinition
- func (d *BeanDefinition) String() string
- func (d *BeanDefinition) Type() reflect.Type
- func (d *BeanDefinition) TypeName() string
- func (d *BeanDefinition) Value() reflect.Value
- func (d *BeanDefinition) Wired() bool
- type Consumers
- type Container
- func (c *Container) Close()
- func (c *Container) Go(fn func(ctx context.Context))
- func (c *Container) Load(file string) error
- func (c *Container) Object(i interface{}) *BeanDefinition
- func (c *Container) Property(key string, value interface{})
- func (c *Container) Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition
- func (c *Container) Refresh() error
- type Environment
- type Pandora
Constants ¶
const ( HighestOrder = math.MinInt32 LowestOrder = math.MaxInt32 )
const ( Default = beanStatus(0) // 默认状态 Resolving = beanStatus(1) // 正在决议 Resolved = beanStatus(2) // 已决议 Wiring = beanStatus(3) // 正在注入 Wired = beanStatus(4) // 注入完成 Deleted = beanStatus(5) // 已删除 )
const ( Unrefreshed = refreshState(0) // 未刷新 Refreshing = refreshState(1) // 正在刷新 Refreshed = refreshState(2) // 已刷新 )
const DefaultBanner = `` /* 641-byte string literal not displayed */
const EnvPrefix = "GS_"
EnvPrefix 属性覆盖的环境变量需要携带该前缀。
Variables ¶
var AppEvent = (*appEvent)(nil)
AppEvent 导出 appEvent 类型
var AppRunner = (*appRunner)(nil)
AppRunner 导出 appRunner 类型
var WebFilter = (*web.Filter)(nil)
WebFilter 导出 web.Filter 类型
var WebRouter = (*web.Router)(nil)
WebRouter 导出 web.Router 类型
Functions ¶
func DeleteBinding ¶
DeleteBinding 注册 DELETE 方法处理函数。
func DeleteMapping ¶
func DeleteMapping(path string, fn web.HandlerFunc) *web.Mapper
DeleteMapping 注册 DELETE 方法处理函数。
func GetBinding ¶
GetBinding 注册 GET 方法处理函数。
func GetMapping ¶
func GetMapping(path string, fn web.HandlerFunc) *web.Mapper
GetMapping 注册 GET 方法处理函数。
func Go ¶
Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。
func HandleDelete ¶
HandleDelete 注册 DELETE 方法处理函数。
func HandlePost ¶
HandlePost 注册 POST 方法处理函数。
func HandleRequest ¶
HandleRequest 注册任意 HTTP 方法处理函数。
func PostBinding ¶
PostBinding 注册 POST 方法处理函数。
func PostMapping ¶
func PostMapping(path string, fn web.HandlerFunc) *web.Mapper
PostMapping 注册 POST 方法处理函数。
func Property ¶
func Property(key string, value interface{})
Property 设置 key 对应的属性值,如果 key 对应的属性值已经存在则 Set 方法会 覆盖旧值。Set 方法除了支持 string 类型的属性值,还支持 int、uint、bool 等 其他基础数据类型的属性值。特殊情况下,Set 方法也支持 slice 、map 与基础数据 类型组合构成的属性值,其处理方式是将组合结构层层展开,可以将组合结构看成一棵树, 那么叶子结点的路径就是属性的 key,叶子结点的值就是属性的值。
func PutBinding ¶
PutBinding 注册 PUT 方法处理函数。
func PutMapping ¶
func PutMapping(path string, fn web.HandlerFunc) *web.Mapper
PutMapping 注册 PUT 方法处理函数。
func RequestBinding ¶
RequestBinding 注册任意 HTTP 方法处理函数。
func RequestMapping ¶
RequestMapping 注册任意 HTTP 方法处理函数。
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App 应用
func (*App) DeleteBinding ¶
DeleteBinding 注册 DELETE 方法处理函数
func (*App) DeleteMapping ¶
DeleteMapping 注册 DELETE 方法处理函数
func (*App) GetBinding ¶
GetBinding 注册 GET 方法处理函数
func (*App) GetMapping ¶
GetMapping 注册 GET 方法处理函数
func (*App) Go ¶
Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。
func (*App) GrpcClient ¶
func (app *App) GrpcClient(fn interface{}, endpoint string) *BeanDefinition
GrpcClient 注册 gRPC 服务客户端,fn 是 gRPC 自动生成的客户端构造函数。
func (*App) GrpcServer ¶
func (app *App) GrpcServer(serviceName string, fn interface{}, service interface{}) *BeanDefinition
GrpcServer 注册 gRPC 服务提供者,fn 是 gRPC 自动生成的服务注册函数, serviceName 是服务名称,必须对应 *_grpc.pg.go 文件里面 grpc.ServerDesc 的 ServiceName 字段,server 是服务提供者对象。
func (*App) HandleDelete ¶
HandleDelete 注册 DELETE 方法处理函数
func (*App) HandlePost ¶
HandlePost 注册 POST 方法处理函数
func (*App) HandleRequest ¶
HandleRequest 注册任意 HTTP 方法处理函数
func (*App) Object ¶
func (app *App) Object(i interface{}) *BeanDefinition
Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。
func (*App) OnProperty ¶
OnProperty 当 key 对应的属性值准备好后发送一个通知。
func (*App) PostBinding ¶
PostBinding 注册 POST 方法处理函数
func (*App) PostMapping ¶
PostMapping 注册 POST 方法处理函数
func (*App) Property ¶
Property 设置 key 对应的属性值,如果 key 对应的属性值已经存在则 Set 方法会 覆盖旧值。Set 方法除了支持 string 类型的属性值,还支持 int、uint、bool 等 其他基础数据类型的属性值。特殊情况下,Set 方法也支持 slice 、map 与基础数据 类型组合构成的属性值,其处理方式是将组合结构层层展开,可以将组合结构看成一棵树, 那么叶子结点的路径就是属性的 key,叶子结点的值就是属性的值。
func (*App) Provide ¶
func (app *App) Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition
Provide 注册构造函数形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。
func (*App) PutBinding ¶
PutBinding 注册 PUT 方法处理函数
func (*App) PutMapping ¶
PutMapping 注册 PUT 方法处理函数
func (*App) RequestBinding ¶
RequestBinding 注册任意 HTTP 方法处理函数
func (*App) RequestMapping ¶
RequestMapping 注册任意 HTTP 方法处理函数
type AppContext ¶
type BeanDefinition ¶
type BeanDefinition struct {
// contains filtered or unexported fields
}
BeanDefinition bean 元数据。
func GrpcClient ¶
func GrpcClient(fn interface{}, endpoint string) *BeanDefinition
GrpcClient 注册 gRPC 服务客户端,fn 是 gRPC 自动生成的客户端构造函数。
func GrpcServer ¶
func GrpcServer(serviceName string, fn interface{}, service interface{}) *BeanDefinition
GrpcServer 注册 gRPC 服务提供者,fn 是 gRPC 自动生成的服务注册函数, serviceName 是服务名称,必须对应 *_grpc.pg.go 文件里面 grpc.ServerDesc 的 ServiceName 字段,server 是服务提供者对象。
func NewBean ¶
func NewBean(objOrCtor interface{}, ctorArgs ...arg.Arg) *BeanDefinition
NewBean 普通函数注册时需要使用 reflect.ValueOf(fn) 形式以避免和构造函数发生冲突。
func Object ¶
func Object(i interface{}) *BeanDefinition
Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。
func Provide ¶
func Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition
Provide 注册构造函数形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。
func (*BeanDefinition) DependsOn ¶
func (d *BeanDefinition) DependsOn(selectors ...bean.Selector) *BeanDefinition
DependsOn 设置 bean 的间接依赖项。
func (*BeanDefinition) Destroy ¶
func (d *BeanDefinition) Destroy(fn interface{}) *BeanDefinition
Destroy 设置 bean 的销毁函数。
func (*BeanDefinition) Export ¶
func (d *BeanDefinition) Export(exports ...interface{}) *BeanDefinition
Export 设置 bean 的导出接口。
func (*BeanDefinition) Init ¶
func (d *BeanDefinition) Init(fn interface{}) *BeanDefinition
Init 设置 bean 的初始化函数。
func (*BeanDefinition) Interface ¶
func (d *BeanDefinition) Interface() interface{}
Interface 返回 bean 的真实值。
func (*BeanDefinition) Match ¶
func (d *BeanDefinition) Match(typeName string, beanName string) bool
Match 测试 bean 的类型全限定名和 bean 的名称是否都匹配。
func (*BeanDefinition) Name ¶
func (d *BeanDefinition) Name(name string) *BeanDefinition
Name 设置 bean 的名称。
func (*BeanDefinition) On ¶
func (d *BeanDefinition) On(cond cond.Condition) *BeanDefinition
On 设置 bean 的 Condition。
func (*BeanDefinition) Order ¶
func (d *BeanDefinition) Order(order int) *BeanDefinition
Order 设置 bean 的排序序号,值越小顺序越靠前(优先级越高)。
func (*BeanDefinition) Primary ¶
func (d *BeanDefinition) Primary() *BeanDefinition
Primary 设置 bean 为主版本。
func (*BeanDefinition) String ¶
func (d *BeanDefinition) String() string
func (*BeanDefinition) TypeName ¶
func (d *BeanDefinition) TypeName() string
TypeName 返回 bean 的原始类型的全限定名。
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container 是 go-spring 框架的基石,实现了 Martin Fowler 在 << Inversion of Control Containers and the Dependency Injection pattern >> 一文中 提及的依赖注入的概念。但原文的依赖注入仅仅是指对象之间的依赖关系处理,而有些 IoC 容器在实现时比如 Spring 还引入了对属性 property 的处理。通常大家会用依赖注入统 述上面两种概念,但实际上使用属性绑定来描述对 property 的处理会更加合适,因此 go-spring 严格区分了这两种概念,在描述对 bean 的处理时要么单独使用依赖注入或属 性绑定,要么同时使用依赖注入和属性绑定。
func (*Container) Close ¶
func (c *Container) Close()
Close 关闭容器,此方法必须在 Refresh 之后调用。该方法会触发 ctx 的 Done 信 号,然后等待所有 goroutine 结束,最后按照被依赖先销毁的原则执行所有的销毁函数。
func (*Container) Go ¶
Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。
func (*Container) Object ¶
func (c *Container) Object(i interface{}) *BeanDefinition
Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。
func (*Container) Property ¶
Property 设置 key 对应的属性值,如果 key 对应的属性值已经存在则 Set 方法会 覆盖旧值。Set 方法除了支持 string 类型的属性值,还支持 int、uint、bool 等 其他基础数据类型的属性值。特殊情况下,Set 方法也支持 slice 、map 与基础数据 类型组合构成的属性值,其处理方式是将组合结构层层展开,可以将组合结构看成一棵树, 那么叶子结点的路径就是属性的 key,叶子结点的值就是属性的值。
type Environment ¶
Environment 提供获取环境变量和命令行参数的方法,命令行参数优先级更高。
type Pandora ¶
type Pandora interface {
Go(fn func(ctx context.Context))
Prop(key string, opts ...conf.GetOption) interface{}
Bind(i interface{}, opts ...conf.BindOption) error
Get(i interface{}, selectors ...bean.Selector) error
Wire(objOrCtor interface{}, ctorArgs ...arg.Arg) (interface{}, error)
Invoke(fn interface{}, args ...arg.Arg) ([]interface{}, error)
}
Pandora 提供了一些在 IoC 容器启动后基于反射获取和使用 property 与 bean 的接 口。因为很多人会担心在运行时大量使用反射会降低程序性能,所以命名为 Pandora,取 其诱人但危险的含义。事实上,这些在 IoC 容器启动后使用属性绑定和依赖注入的方案, 都可以转换为启动阶段的方案以提高程序的性能。 另一方面,为了统一 Container 和 App 两种启动方式下这些方法的使用方式,需要提取 出一个可共用的接口来,也就是说,无论程序是 Container 方式启动还是 App 方式启动, 都可以在需要使用这些方法的地方注入一个 Pandora 对象而不是 Container 对象或者 App 对象,从而实现使用方式的统一。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package arg 用于实现函数参数绑定。
|
Package arg 用于实现函数参数绑定。 |
|
Package bean 定义了 bean 选择器和 Definition 接口。
|
Package bean 定义了 bean 选择器和 Definition 接口。 |
|
Package cond 提供了判断 bean 注册是否有效的条件。
|
Package cond 提供了判断 bean 注册是否有效的条件。 |
|
Package environ 提供了 go-spring 所有内置属性的定义,以及一种允许使用环境变量 进行属性覆盖的机制。
|
Package environ 提供了 go-spring 所有内置属性的定义,以及一种允许使用环境变量 进行属性覆盖的机制。 |