gs_core

package
v1.2.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RefreshDefault = refreshState(iota) // 未刷新
	RefreshInit                         // 准备刷新
	Refreshing                          // 正在刷新
	Refreshed                           // 已刷新
)

Variables

View Source
var (
	GsContextType = reflect.TypeOf((*gs.Context)(nil)).Elem()
)
View Source
var UnregisteredBeanType = reflect.TypeOf((*gs.UnregisteredBean)(nil))

Functions

func New

func New() gs.Container

New 创建 IoC 容器。

func NewBean

func NewBean(objOrCtor interface{}, ctorArgs ...gs.Arg) *gs.UnregisteredBean

NewBean 普通函数注册时需要使用 reflect.ValueOf(fn) 形式以避免和构造函数发生冲突。

func TripleSort

func TripleSort(sorting *list.List, fn GetBeforeItems) *list.List

TripleSort 三路排序

Types

type BeanRuntime

type BeanRuntime interface {
	Name() string
	Type() reflect.Type
	Value() reflect.Value
	Interface() interface{}
	Callable() gs.Callable
	Match(typeName string, beanName string) bool
	Status() gs_bean.BeanStatus
	IsPrimary() bool
	String() string
}

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 (*Container) Bind

func (c *Container) Bind(i interface{}, opts ...conf.BindArg) error

func (*Container) Close

func (c *Container) Close()

Close 关闭容器,此方法必须在 Refresh 之后调用。该方法会触发 ctx 的 Done 信 号,然后等待所有 goroutine 结束,最后按照被依赖先销毁的原则执行所有的销毁函数。

func (*Container) Context

func (c *Container) Context() context.Context

Context 返回 IoC 容器的 ctx 对象。

func (*Container) Find

func (c *Container) Find(selector gs.BeanSelector) ([]gs.CondBean, error)

Find 查找符合条件的 bean 对象,注意该函数只能保证返回的 bean 是有效的, 即未被标记为删除的,而不能保证已经完成属性绑定和依赖注入。

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

func (c *Container) Go(fn func(ctx context.Context))

Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。

func (*Container) Group

func (c *Container) Group(fn GroupFunc)

func (*Container) Has

func (c *Container) Has(key string) bool

func (*Container) Invoke

func (c *Container) Invoke(fn interface{}, args ...gs.Arg) ([]interface{}, error)

Invoke 调用函数,函数的参数会自动注入,函数的返回值也会自动注入。

func (*Container) Keys

func (c *Container) Keys() []string

func (*Container) Object

func (c *Container) Object(i interface{}) *gs.RegisteredBean

Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func (*Container) Prop

func (c *Container) Prop(key string, opts ...conf.GetOption) string

func (*Container) Provide

func (c *Container) Provide(ctor interface{}, args ...gs.Arg) *gs.RegisteredBean

Provide 注册构造函数形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func (*Container) Refresh

func (c *Container) Refresh() (err error)

func (*Container) RefreshProperties

func (c *Container) RefreshProperties(p gs.Properties) error

func (*Container) Resolve

func (c *Container) Resolve(s string) (string, error)

func (*Container) SimplifyMemory

func (c *Container) SimplifyMemory()

SimplifyMemory 清理运行时不需要的空间。

func (*Container) SubKeys

func (c *Container) SubKeys(key string) ([]string, error)

func (*Container) Wire

func (c *Container) Wire(objOrCtor interface{}, ctorArgs ...gs.Arg) (interface{}, error)

Wire 如果传入的是 bean 对象,则对 bean 对象进行属性绑定和依赖注入,如果传入的 是构造函数,则立即执行该构造函数,然后对返回的结果进行属性绑定和依赖注入。无论哪 种方式,该函数执行完后都会返回 bean 对象的真实值。

type GetBeforeItems

type GetBeforeItems func(sorting *list.List, current interface{}) *list.List

GetBeforeItems 获取 sorting 中排在 current 前面的元素

type GroupFunc

type GroupFunc = func(p gs.Properties) ([]*gs.UnregisteredBean, error)

Jump to

Keyboard shortcuts

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