Documentation
¶
Index ¶
- Constants
- Variables
- func New() gs.Container
- func NewBean(objOrCtor interface{}, ctorArgs ...gs.Arg) *gs.UnregisteredBean
- func TripleSort(sorting *list.List, fn GetBeforeItems) *list.List
- type BeanRuntime
- type Container
- func (c *Container) Accept(b *gs.UnregisteredBean) *gs.RegisteredBean
- func (c *Container) Bind(i interface{}, opts ...conf.BindArg) error
- func (c *Container) Close()
- func (c *Container) Context() context.Context
- func (c *Container) Find(selector gs.BeanSelector) ([]gs.CondBean, error)
- func (c *Container) Get(i interface{}, selectors ...gs.BeanSelector) error
- func (c *Container) Go(fn func(ctx context.Context))
- func (c *Container) Group(fn GroupFunc)
- func (c *Container) Has(key string) bool
- func (c *Container) Invoke(fn interface{}, args ...gs.Arg) ([]interface{}, error)
- func (c *Container) Keys() []string
- func (c *Container) Object(i interface{}) *gs.RegisteredBean
- func (c *Container) Prop(key string, opts ...conf.GetOption) string
- func (c *Container) Provide(ctor interface{}, args ...gs.Arg) *gs.RegisteredBean
- func (c *Container) Refresh() (err error)
- func (c *Container) RefreshProperties(p gs.Properties) error
- func (c *Container) Resolve(s string) (string, error)
- func (c *Container) SimplifyMemory()
- func (c *Container) SubKeys(key string) ([]string, error)
- func (c *Container) Wire(objOrCtor interface{}, ctorArgs ...gs.Arg) (interface{}, error)
- type GetBeforeItems
- type GroupFunc
Constants ¶
const ( RefreshDefault = refreshState(iota) // 未刷新 RefreshInit // 准备刷新 Refreshing // 正在刷新 Refreshed // 已刷新 )
Variables ¶
var UnregisteredBeanType = reflect.TypeOf((*gs.UnregisteredBean)(nil))
Functions ¶
Types ¶
type BeanRuntime ¶
type Container ¶
type Container struct {
ContextAware bool
AllowCircularReferences bool `value:"${spring.allow-circular-references:=false}"`
ForceAutowireIsNullable bool `value:"${spring.force-autowire-is-nullable:=false}"`
// 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) Accept ¶
func (c *Container) Accept(b *gs.UnregisteredBean) *gs.RegisteredBean
func (*Container) Close ¶
func (c *Container) Close()
Close 关闭容器,此方法必须在 Refresh 之后调用。该方法会触发 ctx 的 Done 信 号,然后等待所有 goroutine 结束,最后按照被依赖先销毁的原则执行所有的销毁函数。
func (*Container) Get ¶
func (c *Container) Get(i interface{}, selectors ...gs.BeanSelector) error
Get 根据类型和选择器获取符合条件的 bean 对象。当 i 是一个基础类型的 bean 接收 者时,表示符合条件的 bean 对象只能有一个,没有找到或者多于一个时会返回 error。 当 i 是一个 map 类型的 bean 接收者时,表示获取任意数量的 bean 对象,map 的 key 是 bean 的名称,map 的 value 是 bean 的地址。当 i 是一个 array 或者 slice 时,也表示获取任意数量的 bean 对象,但是它会对获取到的 bean 对象进行排序, 如果没有传入选择器或者传入的选择器是 * ,则根据 bean 的 order 值进行排序,这种 工作模式称为自动模式,否则根据传入的选择器列表进行排序,这种工作模式成为指派模式。 该方法和 Find 方法的区别是该方法保证返回的所有 bean 对象都已经完成属性绑定和依 赖注入,而 Find 方法只能保证返回的 bean 对象是有效的,即未被标记为删除的。
func (*Container) Go ¶
Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。
func (*Container) Object ¶
func (c *Container) Object(i interface{}) *gs.RegisteredBean
Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。
func (*Container) Provide ¶
func (c *Container) Provide(ctor interface{}, args ...gs.Arg) *gs.RegisteredBean
Provide 注册构造函数形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。
func (*Container) RefreshProperties ¶
func (c *Container) RefreshProperties(p gs.Properties) error
type GetBeforeItems ¶
GetBeforeItems 获取 sorting 中排在 current 前面的元素
type GroupFunc ¶
type GroupFunc = func(p gs.Properties) ([]*gs.UnregisteredBean, error)