SpringCore

package
v1.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2019 License: Apache-2.0 Imports: 9 Imported by: 10

Documentation

Overview

实现了一个完善的 IoC 容器。

Index

Constants

View Source
const (
	BeanStatus_Default  = BeanStatus(0) // 默认状态
	BeanStatus_Resolved = BeanStatus(1) // 已决议状态
	BeanStatus_Wiring   = BeanStatus(2) // 正在绑定状态
	BeanStatus_Wired    = BeanStatus(3) // 绑定完成状态
)
View Source
const (
	OpMode_None = OpMode(0)
	OpMode_Or   = OpMode(1)
	OpMode_And  = OpMode(2)
)

Variables

This section is empty.

Functions

func IsValidBean

func IsValidBean(bean interface{}) (reflect.Type, bool)

检查是否为合法的 Bean 对象

func ParseBeanId

func ParseBeanId(beanId string) (typeName string, beanName string, nullable bool)

解析 BeanId 的内容,"TypeName:BeanName?" 或者 "[]?"

func RegisterTypeConverter

func RegisterTypeConverter(fn interface{})

注册类型转换器,用于属性绑定,函数原型 func(string)struct

func TypeName

func TypeName(t reflect.Type) string

获取原始类型的全限定名,golang 允许不同的路径下存在相同的包,故此有全限定名的需求。形如 "github.com/go-spring/go-spring/spring-core/SpringCore.DefaultSpringContext"

Types

type Annotation

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

设定 Bean 的各种元数据

func NewAnnotation

func NewAnnotation(bean *BeanDefinition) *Annotation

构造函数

func (*Annotation) ConditionOn

func (annotation *Annotation) ConditionOn(cond Condition) *Annotation

设置一个 Condition

func (*Annotation) ConditionOnBean

func (annotation *Annotation) ConditionOnBean(beanId string) *Annotation

设置一个 BeanCondition

func (*Annotation) ConditionOnExpression

func (annotation *Annotation) ConditionOnExpression(expression string) *Annotation

设置一个 ExpressionCondition

func (*Annotation) ConditionOnMatches

func (annotation *Annotation) ConditionOnMatches(fn ConditionFunc) *Annotation

设置一个 FunctionCondition

func (*Annotation) ConditionOnMissingBean

func (annotation *Annotation) ConditionOnMissingBean(beanId string) *Annotation

设置一个 MissingBeanCondition

func (*Annotation) ConditionOnProperty

func (annotation *Annotation) ConditionOnProperty(name string, havingValue string) *Annotation

设置一个 PropertyCondition

func (*Annotation) DependsOn

func (annotation *Annotation) DependsOn(beanId ...string) *Annotation

设置 bean 的非直接依赖

func (*Annotation) Primary

func (annotation *Annotation) Primary(primary bool) *Annotation

设置 bean 的优先级

func (*Annotation) Profile

func (annotation *Annotation) Profile(profile string) *Annotation

设置 bean 的运行环境

type BeanCacheItem

type BeanCacheItem struct {
	// Different typed beans implemented same interface maybe
	// have same name, so name can't be a map's key. therefor
	// we use a list to store the cached beans.
	Named []*BeanDefinition

	// 收集模式得到的 Bean 列表,一个类型只需收集一次。
	Collect reflect.Value
}

BeanCacheItem defines a BeanCache's item.

func NewBeanCacheItem

func NewBeanCacheItem() *BeanCacheItem

BeanCacheItem's factory method.

func (*BeanCacheItem) Store

func (item *BeanCacheItem) Store(d *BeanDefinition)

将一个 Bean 存储到 CachedBeanMapItem 里

func (*BeanCacheItem) StoreCollect

func (item *BeanCacheItem) StoreCollect(v reflect.Value)

将收集到的 Bean 列表的值存储到 CachedBeanMapItem 里

type BeanCondition

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

基于 Bean 的 Condition 实现

func NewBeanCondition

func NewBeanCondition(beanId string) *BeanCondition

工厂函数

func (*BeanCondition) Matches

func (c *BeanCondition) Matches(ctx SpringContext) bool

type BeanDefinition

type BeanDefinition struct {
	SpringBean

	Name string // 名称
	// contains filtered or unexported fields
}

定义 BeanDefinition 类型

func FnToBeanDefinition

func FnToBeanDefinition(name string, fn interface{}, tags ...string) *BeanDefinition

将构造函数转换为 BeanDefinition 对象

func NewBeanDefinition

func NewBeanDefinition(bean SpringBean, name string) *BeanDefinition

工厂函数

func ToBeanDefinition

func ToBeanDefinition(name string, i interface{}) *BeanDefinition

将 Bean 转换为 BeanDefinition 对象

func (*BeanDefinition) Match

func (d *BeanDefinition) Match(typeName string, beanName string) bool

测试类型全限定名和 Bean 名称是否都能匹配。

type BeanKey

type BeanKey struct {
	Type reflect.Type
	Name string
}

BeanKey defines a bean's unique key, type+name.

type BeanStatus

type BeanStatus int

定义 Bean 的状态值

type Condition

type Condition interface {
	Matches(ctx SpringContext) bool
}

定义 Condition 接口,当判断条件返回 true 时 Bean 才会真正注册到 IoC 容器。

type ConditionFunc

type ConditionFunc func(ctx SpringContext) bool

定义 Condition 接口 Matches 方法的类型

type ConditionNode

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

定义 Condition 表达式的节点

func NewConditionNode

func NewConditionNode() *ConditionNode

工厂函数

func (*ConditionNode) Matches

func (c *ConditionNode) Matches(ctx SpringContext) bool

type Conditional

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

定义 Condition 表达式

func NewConditional

func NewConditional() *Conditional

工厂函数

func (*Conditional) And

func (c *Conditional) And() *Conditional

c=a&&b

func (*Conditional) Matches

func (c *Conditional) Matches(ctx SpringContext) bool

func (*Conditional) OnBean

func (c *Conditional) OnBean(beanId string) *Conditional

func (*Conditional) OnCondition

func (c *Conditional) OnCondition(cond Condition) *Conditional

func (*Conditional) OnExpression

func (c *Conditional) OnExpression(expression string) *Conditional

func (*Conditional) OnMatches

func (c *Conditional) OnMatches(fn ConditionFunc) *Conditional

func (*Conditional) OnMissingBean

func (c *Conditional) OnMissingBean(beanId string) *Conditional

func (*Conditional) OnProperty

func (c *Conditional) OnProperty(name string, havingValue string) *Conditional

func (*Conditional) Or

func (c *Conditional) Or() *Conditional

c=a||b

type ConstructorBean

type ConstructorBean struct {
	OriginalBean
	// contains filtered or unexported fields
}

保存构造函数的 SpringBean

func NewConstructorBean

func NewConstructorBean(fn interface{}, tags ...string) *ConstructorBean

工厂函数,所有 tag 都必须同时有或者同时没有序号。

type DefaultProperties

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

定义 Properties 的默认版本

func NewDefaultProperties

func NewDefaultProperties() *DefaultProperties

工厂函数

func (*DefaultProperties) BindProperty

func (p *DefaultProperties) BindProperty(name string, i interface{})

根据类型获取属性值,属性名称统一转成小写。

func (*DefaultProperties) GetAllProperties

func (p *DefaultProperties) GetAllProperties() map[string]interface{}

获取所有的属性值,属性名称统一转成小写。

func (*DefaultProperties) GetBoolProperty

func (p *DefaultProperties) GetBoolProperty(name string) bool

获取布尔型属性值,属性名称统一转成小写。

func (*DefaultProperties) GetDefaultProperty

func (p *DefaultProperties) GetDefaultProperty(name string, defaultValue interface{}) (interface{}, bool)

获取属性值,如果没有找到则使用指定的默认值

func (*DefaultProperties) GetFloatProperty

func (p *DefaultProperties) GetFloatProperty(name string) float64

获取浮点型属性值,属性名称统一转成小写。

func (*DefaultProperties) GetIntProperty

func (p *DefaultProperties) GetIntProperty(name string) int64

获取有符号整型属性值,属性名称统一转成小写。

func (*DefaultProperties) GetPrefixProperties

func (p *DefaultProperties) GetPrefixProperties(prefix string) map[string]interface{}

获取指定前缀的属性值集合,属性名称统一转成小写。

func (*DefaultProperties) GetProperty

func (p *DefaultProperties) GetProperty(name string) interface{}

获取属性值,属性名称统一转成小写。

func (*DefaultProperties) GetStringProperty

func (p *DefaultProperties) GetStringProperty(name string) string

获取字符串型属性值,属性名称统一转成小写。

func (*DefaultProperties) GetUintProperty

func (p *DefaultProperties) GetUintProperty(name string) uint64

获取无符号整型属性值,属性名称统一转成小写。

func (*DefaultProperties) LoadProperties

func (p *DefaultProperties) LoadProperties(filename string)

加载属性配置文件

func (*DefaultProperties) SetProperty

func (p *DefaultProperties) SetProperty(name string, value interface{})

设置属性值,属性名称统一转成小写。

type DefaultSpringContext

type DefaultSpringContext struct {
	// 属性值列表接口
	*DefaultProperties

	BeanMap   map[BeanKey]*BeanDefinition     // Bean 的集合
	BeanCache map[reflect.Type]*BeanCacheItem // Bean 的缓存
	// contains filtered or unexported fields
}

SpringContext 的默认版本

func NewDefaultSpringContext

func NewDefaultSpringContext() *DefaultSpringContext

工厂函数

func (*DefaultSpringContext) AutoWireBeans

func (ctx *DefaultSpringContext) AutoWireBeans()

自动绑定所有的 Bean

func (*DefaultSpringContext) CollectBeans

func (ctx *DefaultSpringContext) CollectBeans(i interface{}) bool

收集数组或指针定义的所有符合条件的 Bean 对象,收集到返回 true,否则返回 false。

func (*DefaultSpringContext) FindBeanByName

func (ctx *DefaultSpringContext) FindBeanByName(beanId string) (interface{}, bool)

根据名称和类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。

func (*DefaultSpringContext) GetAllBeanDefinitions

func (ctx *DefaultSpringContext) GetAllBeanDefinitions() []*BeanDefinition

获取所有 Bean 的定义,一般仅供调试使用。

func (*DefaultSpringContext) GetBean

func (ctx *DefaultSpringContext) GetBean(i interface{}) bool

根据类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。

func (*DefaultSpringContext) GetBeanByName

func (ctx *DefaultSpringContext) GetBeanByName(beanId string, i interface{}) bool

根据名称和类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。

func (*DefaultSpringContext) GetProfile

func (ctx *DefaultSpringContext) GetProfile() string

获取运行环境

func (*DefaultSpringContext) RegisterBean

func (ctx *DefaultSpringContext) RegisterBean(bean interface{}) *Annotation

注册单例 Bean,无需指定名称,重复注册会 panic。

func (*DefaultSpringContext) RegisterBeanDefinition

func (ctx *DefaultSpringContext) RegisterBeanDefinition(d *BeanDefinition) *Annotation

注册单例 Bean,使用 BeanDefinition 对象,重复注册会 panic。

func (*DefaultSpringContext) RegisterBeanFn

func (ctx *DefaultSpringContext) RegisterBeanFn(fn interface{}, tags ...string) *Annotation

通过构造函数注册单例 Bean,无需指定名称,重复注册会 panic。

func (*DefaultSpringContext) RegisterNameBean

func (ctx *DefaultSpringContext) RegisterNameBean(name string, bean interface{}) *Annotation

注册单例 Bean,需要指定名称,重复注册会 panic。

func (*DefaultSpringContext) RegisterNameBeanFn

func (ctx *DefaultSpringContext) RegisterNameBeanFn(name string, fn interface{}, tags ...string) *Annotation

通过构造函数注册单例 Bean,需要指定名称,重复注册会 panic。

func (*DefaultSpringContext) SetProfile

func (ctx *DefaultSpringContext) SetProfile(profile string)

设置运行环境

func (*DefaultSpringContext) WireBean

func (ctx *DefaultSpringContext) WireBean(bean interface{})

绑定外部指定的 Bean

func (*DefaultSpringContext) WireBeanDefinition

func (ctx *DefaultSpringContext) WireBeanDefinition(beanDefinition *BeanDefinition)

绑定 BeanDefinition 指定的 Bean

type ExpressionCondition

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

基于表达式的 Condition 实现

func NewExpressionCondition

func NewExpressionCondition(expression string) *ExpressionCondition

工厂函数

func (*ExpressionCondition) Matches

func (c *ExpressionCondition) Matches(ctx SpringContext) bool

type FunctionCondition

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

基于 Matches 方法的 Condition 实现

func NewFunctionCondition

func NewFunctionCondition(fn ConditionFunc) *FunctionCondition

工厂函数

func (*FunctionCondition) Matches

func (c *FunctionCondition) Matches(ctx SpringContext) bool

type MapPropertyHolder

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

func NewMapPropertyHolder

func NewMapPropertyHolder(m map[interface{}]interface{}) *MapPropertyHolder

func (*MapPropertyHolder) GetDefaultProperty

func (p *MapPropertyHolder) GetDefaultProperty(name string, defaultValue interface{}) (interface{}, bool)

type MissingBeanCondition

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

基于 Missing Bean 的 Condition 实现

func NewMissingBeanCondition

func NewMissingBeanCondition(beanId string) *MissingBeanCondition

工厂函数

func (*MissingBeanCondition) Matches

func (c *MissingBeanCondition) Matches(ctx SpringContext) bool

type OpMode

type OpMode int

type OriginalBean

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

保存原始对象的 SpringBean

func NewOriginalBean

func NewOriginalBean(bean interface{}) *OriginalBean

工厂函数

func (*OriginalBean) Bean

func (b *OriginalBean) Bean() interface{}

func (*OriginalBean) Type

func (b *OriginalBean) Type() reflect.Type

func (*OriginalBean) TypeName

func (b *OriginalBean) TypeName() string

func (*OriginalBean) Value

func (b *OriginalBean) Value() reflect.Value

type Properties

type Properties interface {
	// 加载属性配置文件
	LoadProperties(filename string)

	// 获取属性值,属性名称统一转成小写。
	GetProperty(name string) interface{}

	// 获取布尔型属性值,属性名称统一转成小写。
	GetBoolProperty(name string) bool

	// 获取有符号整型属性值,属性名称统一转成小写。
	GetIntProperty(name string) int64

	// 获取无符号整型属性值,属性名称统一转成小写。
	GetUintProperty(name string) uint64

	// 获取浮点型属性值,属性名称统一转成小写。
	GetFloatProperty(name string) float64

	// 获取字符串型属性值,属性名称统一转成小写。
	GetStringProperty(name string) string

	// 获取属性值,如果没有找到则使用指定的默认值,属性名称统一转成小写。
	GetDefaultProperty(name string, defaultValue interface{}) (interface{}, bool)

	// 设置属性值,属性名称统一转成小写。
	SetProperty(name string, value interface{})

	// 获取指定前缀的属性值集合,属性名称统一转成小写。
	GetPrefixProperties(prefix string) map[string]interface{}

	// 获取所有的属性值,属性名称统一转成小写。
	GetAllProperties() map[string]interface{}

	// 根据类型获取属性值,属性名称统一转成小写。
	BindProperty(name string, i interface{})
}

定义属性值接口

type PropertyCondition

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

基于属性值匹配的 Condition 实现

func NewPropertyCondition

func NewPropertyCondition(name string, havingValue string) *PropertyCondition

工厂函数

func (*PropertyCondition) Matches

func (c *PropertyCondition) Matches(ctx SpringContext) bool

type PropertyHolder

type PropertyHolder interface {
	GetDefaultProperty(name string, defaultValue interface{}) (interface{}, bool)
}

type SpringBean

type SpringBean interface {
	Bean() interface{}
	Type() reflect.Type
	Value() reflect.Value
	TypeName() string
}

定义 SpringBean 接口

type SpringContext

type SpringContext interface {

	// 属性值列表接口
	Properties

	// 获取运行环境
	GetProfile() string

	// 设置运行环境
	SetProfile(profile string)

	// 注册单例 Bean,不指定名称,重复注册会 panic。
	RegisterBean(bean interface{}) *Annotation

	// 注册单例 Bean,需指定名称,重复注册会 panic。
	RegisterNameBean(name string, bean interface{}) *Annotation

	// 通过构造函数注册单例 Bean,不指定名称,重复注册会 panic。
	RegisterBeanFn(fn interface{}, tags ...string) *Annotation

	// 通过构造函数注册单例 Bean,需指定名称,重复注册会 panic。
	RegisterNameBeanFn(name string, fn interface{}, tags ...string) *Annotation

	// 注册单例 Bean,使用 BeanDefinition 对象,重复注册会 panic。
	RegisterBeanDefinition(beanDefinition *BeanDefinition) *Annotation

	// 执行自动绑定过程
	AutoWireBeans()

	// 绑定外部指定的 Bean
	WireBean(bean interface{})

	// 根据类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。
	// 什么情况下会多于 1 个?假设 StructA 实现了 InterfaceT,而且用户在注
	// 册时使用了 StructA 的指针注册多个 Bean,如果在获取时使用 InterfaceT,
	// 则必然出现多于 1 个的情况。
	GetBean(i interface{}) bool

	// 根据名称和类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。
	// 什么情况下会多于 1 个?假设 StructA 和 StructB 都实现了 InterfaceT,
	// 而且用户在注册时使用了相同的名称分别注册了 StructA 和 StructB 的 Bean,
	// 这时候如果使用 InterfaceT 去获取,就会出现多于 1 个的情况。
	GetBeanByName(beanId string, i interface{}) bool

	// 收集数组或指针定义的所有符合条件的 Bean 对象,收集到返回 true,否则返回 false。
	// 什么情况下可以使用此功能?假设 HandlerA 和 HandlerB 都实现了 HandlerT 接口,
	// 而且用户分别注册了一个 HandlerA 和 HandlerB 对象,如果用户想要同时获取 HandlerA
	// 和 HandlerB 对象,那么他可以通过 []HandlerT 即数组的方式获取到所有 Bean。
	CollectBeans(i interface{}) bool

	// 根据名称和类型获取单例 Bean,若多于 1 个则 panic;找到返回 true 否则返回 false。
	FindBeanByName(beanId string) (interface{}, bool)

	// 获取所有 Bean 的定义,一般仅供调试使用。
	GetAllBeanDefinitions() []*BeanDefinition
}

定义 IoC 容器接口,Bean 的注册规则:

  1. 只能注册单例 Bean。
  2. AutoWireBeans 开始后不允许注册新的 Bean(性能考虑)。
  3. 原型 Bean 只能通过 BeanFactory 的形式使用,参见测试用例。

Jump to

Keyboard shortcuts

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