di

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 12 Imported by: 6

README

di

di是一个简易版本的Go依赖注入实现

文档地址

特性

  • 支持手动注册bean实例
  • 支持注册bean类型原型,由DI容器自动实例化并托管bean实例
  • 支持根据名称、类型获取DI容器托管的bean实例
  • 支持根据类型手动生成新的bean实例并返回
  • 支持配置项注入并转换成对应的基本类型
  • 支持匿名字段的bean注入

特别鸣谢

JetBrains

感谢 JetBrains 提供的开源开发许可证。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBean       = errors.New("error bean")
	ErrDefinition = errors.New("error definition")
	ErrLoaded     = errors.New("di loaded")
)

Functions

func AutoMigrateEnv added in v0.0.5

func AutoMigrateEnv()

func GetBean

func GetBean(beanName string) (bean interface{}, ok bool)

func GetBeanName

func GetBeanName(o interface{}) (name string)

func GetByType added in v0.1.3

func GetByType(beanType interface{}) (bean interface{}, ok bool)

func GetProperty added in v0.1.5

func GetProperty(key string) interface{}

func IsPtr

func IsPtr(o interface{}) bool

func Load

func Load()

func LoadAndServ

func LoadAndServ(ctx context.Context)

func LoadEnvironment added in v0.0.5

func LoadEnvironment(replacer *strings.Replacer, trimPrefix bool, prefix ...string) map[string]interface{}

func LoadProperties added in v0.1.5

func LoadProperties(prefix string, propertyType interface{}) interface{}

func New

func New() *di

func NewBean added in v0.1.0

func NewBean(beanType interface{}) (bean interface{})

func NewBeanByName added in v0.1.0

func NewBeanByName(beanName string) (bean interface{})

func Serve

func Serve(ctx context.Context)

Types

type AfterPropertiesSet

type AfterPropertiesSet interface {
	AfterPropertiesSet()
}

AfterPropertiesSet Bean实例注入完成

type AfterPropertiesSetWithContainer added in v0.0.8

type AfterPropertiesSetWithContainer interface {
	AfterPropertiesSet(DI)
}

AfterPropertiesSetWithContainer Bean实例注入完成

type BeanConstruct

type BeanConstruct interface {
	BeanConstruct()
}

BeanConstruct Bean实例创建时

type BeanConstructWithContainer added in v0.0.8

type BeanConstructWithContainer interface {
	BeanConstruct(DI)
}

BeanConstructWithContainer Bean实例创建时

type BeanName added in v0.0.5

type BeanName interface {
	BeanName() string
}

BeanName 返回beanName

type BeanWithName added in v0.1.6

type BeanWithName struct {
	Name string
	Bean interface{}
}

func GetByTypeAll added in v0.1.6

func GetByTypeAll(beanType interface{}) (beans []BeanWithName)

type DI

type DI interface {
	DebugMode(bool) DI

	Log(log Log) DI

	RegisterBean(bean interface{}) DI

	RegisterNamedBean(name string, bean interface{}) DI

	Provide(prototype interface{}) DI

	ProvideNamedBean(beanName string, prototype interface{}) DI

	GetBean(beanName string) (bean interface{}, ok bool)

	GetByType(beanType interface{}) (bean interface{}, ok bool)

	GetByTypeAll(beanType interface{}) (beans []BeanWithName)

	NewBean(beanType interface{}) (bean interface{})

	NewBeanByName(beanName string) (bean interface{})

	UseValueStore(v ValueStore) DI

	Property() ValueStore

	SetDefaultProperty(key string, value interface{}) DI

	SetDefaultPropertyMap(properties map[string]interface{}) DI

	SetProperty(key string, value interface{}) DI

	SetPropertyMap(properties map[string]interface{}) DI

	GetProperty(key string) interface{}

	LoadProperties(prefix string, propertyType interface{}) interface{}

	Load()

	Serve(ctx context.Context)

	Context() context.Context
}

func Provide

func Provide(prototype interface{}) DI

func ProvideNamedBean added in v0.0.5

func ProvideNamedBean(beanName string, prototype interface{}) DI

func RegisterBean

func RegisterBean(bean interface{}) DI

func RegisterNamedBean

func RegisterNamedBean(name string, bean interface{}) DI

func SetDefaultProperty

func SetDefaultProperty(key string, value interface{}) DI

func SetDefaultPropertyMap added in v0.0.5

func SetDefaultPropertyMap(properties map[string]interface{}) DI

func SetProperty

func SetProperty(key string, value interface{}) DI

func SetPropertyMap

func SetPropertyMap(properties map[string]interface{}) DI

func UseValueStore

func UseValueStore(v ValueStore) DI

type Disposable

type Disposable interface {
	Destroy()
}

Disposable 在Bean注销时调用

type DisposableWithContainer added in v0.0.8

type DisposableWithContainer interface {
	Destroy(DI)
}

DisposableWithContainer 在Bean注销时调用

type Initialized

type Initialized interface {
	Initialized()
}

Initialized 在Bean依赖注入完成后执行,可以理解为DI加载完成的通知事件。

type InitializedWithContainer added in v0.0.8

type InitializedWithContainer interface {
	Initialized(DI)
}

InitializedWithContainer 在Bean依赖注入完成后执行,可以理解为DI加载完成的通知事件。

type InjectInfo added in v0.1.6

type InjectInfo struct {
	Bean        interface{}
	BeanName    string
	Type        reflect.Type
	IsPtr       bool // 是否为结构指针
	IsInterface bool // 是否为接口
	Anonymous   bool // 是否为匿名字段
	Omitempty   bool // 不存在依赖时则忽略注入
}

type Injector added in v0.1.6

type Injector interface {
	BeanInject(di DI, info *InjectInfo)
}

Injector bean实例注入器

type Log added in v0.1.0

type Log interface {
	DebugMode(bool)
	Debug(string)
	Info(string)
	Warn(string)
	Fatal(string)
}

type PreInitialize

type PreInitialize interface {
	PreInitialize()
}

PreInitialize Bean实例依赖注入前

type PreInitializeWithContainer added in v0.0.8

type PreInitializeWithContainer interface {
	PreInitialize(DI)
}

PreInitializeWithContainer Bean实例依赖注入前

type ValueStore

type ValueStore interface {
	SetDefault(key string, value interface{})

	Set(key string, value interface{})

	Get(key string) (val interface{})

	GetAll() map[string]interface{}
}

func Property

func Property() ValueStore

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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