gioc

package module
v0.0.0-...-ce7160f Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: MIT Imports: 11 Imported by: 20

README

goioc

go ioc framework

感谢您的支持

star it && QQ群:869428810

使用步骤
  1. go get github.com/gosrv/goioc
  2. 加载配置文件
	loader := gioc.NewConfigLoader()
	err := loader.Load("example/conf/config.json")
	util.VerifyNoError(err)
  1. 创建IBeanContainerBuilder
    builder := gioc.NewBeanContainerBuilder()
  1. 加入1个ITagParser和若干个ITagProcessor
	builder.AddBean(loader)
	builder.AddBean(gioc.NewBeanTagProcessor(builder.GetBeanContainer()))
	builder.AddBean(gioc.NewConfigValueTagProcessor(loader))
	builder.AddBean(gioc.NewTagParser())
	builder.AddBean(gioc.NewBeanBeanConditionInjector())
  1. 加入专属bean
    builder.AddBean(otherBeans...)
  1. 构建容器
    builder.Build()
bean

放入容器中的对象就是一个bean,容器可以针对bean的成员变量做一些注入操作,比如其它bean实例的注入,或者配置数据的注入

IBeanContainerBuilder

用来构建bean容器,并完成注入,bean的注入操作由ITagProcessor完成
构建过程

  1. 收集满足条件的bean(通过IBeanCondition判断条件十分成立)
  2. 获取所有的ITagProcessor,并按优先级排序
  3. 按ITagProcessor的优先级先后处理所有满足条件的bean,这也是注入处理
IBeanCondition

通过内嵌接口IBeanCondition来实现条件判断,只有生效的bean才会被容器操作

  1. NewConditionAnd() 只有所有的条件都满足才能通过
  2. NewConditionOr() 有一个条件满足就能通过
  3. NewConditionOnValue() 当配置文件中存在配置项时才能通过
  4. NewConditionOnBeanType() 当存在某个类型的bean时才能通过
  5. NewConditionOnBeanName() 当存在某个名字的bean时才能通过
系统级tag

标注在bean上,可以执行一些特定意义的注入,目前支持两种系统级tag: bean和cfg

tag bean
  1. "bean" 通过类型注入,会注入其它同类型的bean,如果不是slice,要求容器内有且仅有一个次类型的bean,如果是slice,则可以有任意个
  2. "bean.name" 通过bean名字注入,必须存在并且只能有一个
  3. "bean.required" 如果没找到会不会报错,默认是true
    type BeanC struct {
        beanA IBeanA	`bean:"" bean.name:"beana" bean.required:"true"`
    }
tag cfg
  1. "cfg" 注入配置文件中的配置项
  2. "cfg.d" 注入特定域的配置项,与IConfigBase配合使用,调整配置注入的根节点
  3. "cfg.default" 如果配置文件中不存,则使用的默认配置
    type BeanConfig struct {
        Name  string
        Level int
        Age   int
    }
    
    type Bean struct {
        // 注入配置文件的配置项"cfg.a"
        ConfigA *BeanConfig `cfg:"cfg.a"`
        // 注入配置文件的配置项"cfg.b"
        ConfigB *BeanConfig `cfg:"cfg.b"`
    }
ITagProcessor

可以通过实现接口ITagProcessor来自定义tag注入器,你还可以为它指定一个优先级,如果它没有任何依赖,则可以设置为系统级tag

ITagParser

解析tag标签,容器中只能存在一个,而且必须有一个,用来解析tag

exaple

请查看example1.go/example2.go/example3.go/example4.go

gcluster

https://github.com/gosrv/gcluster
ioc容器的应用:游戏服务器引擎框架(用来开发游戏引擎的引擎)

Documentation

Index

Constants

View Source
const (
	BeanTagProcessor = "bean"
	BeanTag          = "bean"
	// 必须存在,如果装配时不存在会报错,默认是true
	BeanRequiredTag = "bean.required"
)

* bean相关tag处理

View Source
const (
	ConfigTagProcessor = "config"
	// 从配置文件中读取的配置项
	ValueConfigTag       = "cfg"
	ValueConfigDomainTag = "cfg.d"
	// 默认配置项
	ValueDefaultTag = "cfg.default"
)

* cfg相关tag处理

View Source
const (
	PrioritySystem = 0
	PriorityHigh   = 10000
	PriorityMiddle = 100000
	PriorityLow    = 1000000
)

Variables

View Source
var BeanConditionArrayType = reflect.TypeOf((*[]IBeanCondition)(nil)).Elem()
View Source
var BeanConditionInjectorHelper = struct {
	GetBeanConditionInjector func(beanContainer IBeanContainer) []IBeanConditionInjector
	BeanConditionInjector    func(bcondition IBeanCondition, injecots []IBeanConditionInjector, beanContainer IBeanContainer)
}{
	GetBeanConditionInjector: getBeanConditionInjector,
	BeanConditionInjector:    beanConditionInjector,
}
View Source
var BeanConditionType = reflect.TypeOf((*IBeanCondition)(nil)).Elem()
View Source
var BeanProcessHelper = &beanProcessHelper{}
View Source
var IBeanAfterTagProcessType = reflect.TypeOf((*IBeanAfterTagProcess)(nil)).Elem()
View Source
var IBeanBeforeTagProcessType = reflect.TypeOf((*IBeanBeforeTagProcess)(nil)).Elem()
View Source
var IBeanFinishAssemblyType = reflect.TypeOf((*IBeanFinishAssembly)(nil)).Elem()
View Source
var IBeanNameType = reflect.TypeOf((*IBeanName)(nil)).Elem()
View Source
var IBeanStartAssemblyType = reflect.TypeOf((*IBeanStartAssembly)(nil)).Elem()
View Source
var IConfigBaseType = reflect.TypeOf((*IConfigBase)(nil)).Elem()
View Source
var IPriorityType = reflect.TypeOf((*IPriority)(nil)).Elem()
View Source
var ITagParserType = reflect.TypeOf((*ITagParser)(nil)).Elem()
View Source
var ITagProcessorPriorityType = reflect.TypeOf((*ITagProcessorPriority)(nil)).Elem()
View Source
var ITagProcessorType = reflect.TypeOf((*ITagProcessor)(nil)).Elem()
View Source
var TagParserHelper = &tagParserHelper{}
View Source
var TagProcessorHelper = &tagProcessorHelper{}

Functions

func BoolInject

func BoolInject(value reflect.Value, val string) error

func FloatInject

func FloatInject(value reflect.Value, val string) error

func IntInject

func IntInject(value reflect.Value, val string) error

func NewBeanContainer

func NewBeanContainer() *defaultBeanContainer

func NewConditionOnBeanName

func NewConditionOnBeanName(beanName string, exit bool) *conditionOnBeanName

func NewConditionOnBeanType

func NewConditionOnBeanType(beanType reflect.Type, exit bool) *conditionOnBeanType

func NewConfigBase

func NewConfigBase(base string) *configBase

func NewConfigLoader

func NewConfigLoader() *configAutoLoader

func PtrInject

func PtrInject(value reflect.Value, val string) error

func SliceInject

func SliceInject(value reflect.Value, val string) error

func StringInject

func StringInject(value reflect.Value, val string) error

func StructInject

func StructInject(value reflect.Value, val string) error

func UintInject

func UintInject(value reflect.Value, val string) error

Types

type BeanBeanConditionInjector

type BeanBeanConditionInjector struct {
}

func NewBeanBeanConditionInjector

func NewBeanBeanConditionInjector() *BeanBeanConditionInjector

func (*BeanBeanConditionInjector) BeanConditionInjector

func (this *BeanBeanConditionInjector) BeanConditionInjector(bcondition IBeanCondition, beanContainer IBeanContainer)

type BeanInitDriver

type BeanInitDriver struct {
	BeansInit  []IBeanInit  `bean:""`
	BeansStart []IBeanStart `bean:""`
	// contains filtered or unexported fields
}

func NewBeanInitDriver

func NewBeanInitDriver() *BeanInitDriver

func (*BeanInitDriver) CallInit

func (this *BeanInitDriver) CallInit()

func (*BeanInitDriver) CallStart

func (this *BeanInitDriver) CallStart()

func (*BeanInitDriver) CallStop

func (this *BeanInitDriver) CallStop()

func (*BeanInitDriver) CallUnInit

func (this *BeanInitDriver) CallUnInit()

type EmptyBeanContainer

type EmptyBeanContainer struct {
}

type FuncStringValueInjector

type FuncStringValueInjector func(value reflect.Value, val string) error

func (FuncStringValueInjector) StringValueInjector

func (this FuncStringValueInjector) StringValueInjector(value reflect.Value, val string) error

type FuncTagParser

type FuncTagParser func(tag reflect.StructTag) map[string]string

func (FuncTagParser) Parse

func (this FuncTagParser) Parse(tag reflect.StructTag) map[string]string

type IBeanAfterTagProcess

type IBeanAfterTagProcess interface {
	// tag后缀处理
	BeanAfterTagProcess(tagProcessor ITagProcessor, beanContainer IBeanContainer)
}

type IBeanBeforeTagProcess

type IBeanBeforeTagProcess interface {
	// tag前置处理
	BeanBeforeTagProcess(tagProcessor ITagProcessor, beanContainer IBeanContainer)
}

type IBeanCondition

type IBeanCondition interface {
	IsConditionPass() bool
}

* bean的装配条件,只有IsConditionPass才会进入装配,并放入到BeanContainer

func NewConditionAnd

func NewConditionAnd(conditions ...IBeanCondition) IBeanCondition

func NewConditionNot

func NewConditionNot(condition IBeanCondition) IBeanCondition

func NewConditionOnValue

func NewConditionOnValue(value string, exist bool) IBeanCondition

func NewConditionOr

func NewConditionOr(conditions ...IBeanCondition) IBeanCondition

type IBeanConditionInjector

type IBeanConditionInjector interface {
	BeanConditionInjector(bcondition IBeanCondition, beanContainer IBeanContainer)
}

type IBeanContainer

type IBeanContainer interface {
	// 用这个加入的bean会直接加入容器,忽略装配,装配在builder中完成
	AddBean(beans ...interface{})
	AddNamedBean(name string, bean interface{})
	// 通过类型获取bean
	GetBeanByType(pt reflect.Type) []interface{}
	// 通过名字获取bean
	GetBeanByName(name string) interface{}
	// 获取所有的bean
	GetAllBeans() []interface{}
}

bean容器

type IBeanContainerBuilder

type IBeanContainerBuilder interface {
	AddBean(bean ...interface{})
	AddNamedBean(name string, bean interface{})
	GetBeanContainer() IBeanContainer
	Build()
}

func NewBeanContainerBuilder

func NewBeanContainerBuilder() IBeanContainerBuilder

type IBeanFinishAssembly

type IBeanFinishAssembly interface {
	// tag前置处理
	BeanFinishAssembly(beanContainer IBeanContainer)
}

type IBeanInit

type IBeanInit interface {
	BeanInit()
	BeanUninit()
}

type IBeanName

type IBeanName interface {
	BeanName() string
}

func NewBeanName

func NewBeanName(name string) IBeanName

type IBeanStart

type IBeanStart interface {
	BeanStart()
	BeanStop()
}

type IBeanStartAssembly

type IBeanStartAssembly interface {
	// tag前置处理
	BeanStartAssembly(beanContainer IBeanContainer)
}

type IConfigBase

type IConfigBase interface {
	ConfigBase() string
}

type IConfigLoader

type IConfigLoader interface {
	Config() reader.Values
	Load(cfgFileName string) error
	AutoLoad(func())
}

type IPriority

type IPriority interface {
	GetPriority() int
}

type IStringValueInjector

type IStringValueInjector interface {
	StringValueInjector(value reflect.Value, val string) error
}

type ITagParser

type ITagParser interface {
	Parse(tag reflect.StructTag) map[string]string
}

* tag解析,使用go默认的tag解析

func NewTagParser

func NewTagParser() ITagParser

type ITagProcessor

type ITagProcessor interface {
	TagProcessorName() string
	// tag开始处理前会调用一次
	PrepareProcess()
	TagProcess(bean interface{}, fType reflect.StructField, fValue reflect.Value, tags map[string]string)
}

func NewBeanTagProcessor

func NewBeanTagProcessor(beanContainer IBeanContainer) ITagProcessor

func NewConfigValueTagProcessor

func NewConfigValueTagProcessor(conf IConfigLoader) ITagProcessor

type ITagProcessorPriority

type ITagProcessorPriority interface {
	GetTagProcessorPriority() int
}

type PropertyInjectGroup

type PropertyInjectGroup struct {
	// contains filtered or unexported fields
}

func (*PropertyInjectGroup) Inject

func (this *PropertyInjectGroup) Inject(value reflect.Value, val string) error

func (*PropertyInjectGroup) StringValueInjector

func (this *PropertyInjectGroup) StringValueInjector(value reflect.Value, val string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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