Documentation
¶
Overview ¶
实现了一个完善的 IoC 容器。
Index ¶
- Constants
- func IsValidBean(bean interface{}) (reflect.Type, bool)
- func ParseBeanId(beanId string) (typeName string, beanName string, nullable bool)
- func RegisterTypeConverter(fn interface{})
- func TypeName(t reflect.Type) string
- type Annotation
- func (annotation *Annotation) ConditionOn(cond Condition) *Annotation
- func (annotation *Annotation) ConditionOnBean(beanId string) *Annotation
- func (annotation *Annotation) ConditionOnExpression(expression string) *Annotation
- func (annotation *Annotation) ConditionOnMatches(fn ConditionFunc) *Annotation
- func (annotation *Annotation) ConditionOnMissingBean(beanId string) *Annotation
- func (annotation *Annotation) ConditionOnProperty(name string, havingValue string) *Annotation
- func (annotation *Annotation) DependsOn(beanId ...string) *Annotation
- func (annotation *Annotation) Primary(primary bool) *Annotation
- func (annotation *Annotation) Profile(profile string) *Annotation
- type BeanCacheItem
- type BeanCondition
- type BeanDefinition
- type BeanKey
- type BeanStatus
- type Condition
- type ConditionFunc
- type ConditionNode
- type Conditional
- func (c *Conditional) And() *Conditional
- func (c *Conditional) Matches(ctx SpringContext) bool
- func (c *Conditional) OnBean(beanId string) *Conditional
- func (c *Conditional) OnCondition(cond Condition) *Conditional
- func (c *Conditional) OnExpression(expression string) *Conditional
- func (c *Conditional) OnMatches(fn ConditionFunc) *Conditional
- func (c *Conditional) OnMissingBean(beanId string) *Conditional
- func (c *Conditional) OnProperty(name string, havingValue string) *Conditional
- func (c *Conditional) Or() *Conditional
- type ConstructorBean
- type DefaultProperties
- func (p *DefaultProperties) BindProperty(name string, i interface{})
- func (p *DefaultProperties) GetAllProperties() map[string]interface{}
- func (p *DefaultProperties) GetBoolProperty(name string) bool
- func (p *DefaultProperties) GetDefaultProperty(name string, defaultValue interface{}) (interface{}, bool)
- func (p *DefaultProperties) GetFloatProperty(name string) float64
- func (p *DefaultProperties) GetIntProperty(name string) int64
- func (p *DefaultProperties) GetPrefixProperties(prefix string) map[string]interface{}
- func (p *DefaultProperties) GetProperty(name string) interface{}
- func (p *DefaultProperties) GetStringProperty(name string) string
- func (p *DefaultProperties) GetUintProperty(name string) uint64
- func (p *DefaultProperties) LoadProperties(filename string)
- func (p *DefaultProperties) SetProperty(name string, value interface{})
- type DefaultSpringContext
- func (ctx *DefaultSpringContext) AutoWireBeans()
- func (ctx *DefaultSpringContext) CollectBeans(i interface{}) bool
- func (ctx *DefaultSpringContext) FindBeanByName(beanId string) (interface{}, bool)
- func (ctx *DefaultSpringContext) GetAllBeanDefinitions() []*BeanDefinition
- func (ctx *DefaultSpringContext) GetBean(i interface{}) bool
- func (ctx *DefaultSpringContext) GetBeanByName(beanId string, i interface{}) bool
- func (ctx *DefaultSpringContext) GetProfile() string
- func (ctx *DefaultSpringContext) RegisterBean(bean interface{}) *Annotation
- func (ctx *DefaultSpringContext) RegisterBeanDefinition(d *BeanDefinition) *Annotation
- func (ctx *DefaultSpringContext) RegisterBeanFn(fn interface{}, tags ...string) *Annotation
- func (ctx *DefaultSpringContext) RegisterNameBean(name string, bean interface{}) *Annotation
- func (ctx *DefaultSpringContext) RegisterNameBeanFn(name string, fn interface{}, tags ...string) *Annotation
- func (ctx *DefaultSpringContext) SetProfile(profile string)
- func (ctx *DefaultSpringContext) WireBean(bean interface{})
- func (ctx *DefaultSpringContext) WireBeanDefinition(beanDefinition *BeanDefinition)
- type ExpressionCondition
- type FunctionCondition
- type MapPropertyHolder
- type MissingBeanCondition
- type OpMode
- type OriginalBean
- type Properties
- type PropertyCondition
- type PropertyHolder
- type SpringBean
- type SpringContext
Constants ¶
const ( BeanStatus_Default = BeanStatus(0) // 默认状态 BeanStatus_Resolved = BeanStatus(1) // 已决议状态 BeanStatus_Wiring = BeanStatus(2) // 正在绑定状态 BeanStatus_Wired = BeanStatus(3) // 绑定完成状态 )
const ( OpMode_None = OpMode(0) OpMode_Or = OpMode(1) OpMode_And = OpMode(2) )
Variables ¶
This section is empty.
Functions ¶
func ParseBeanId ¶
解析 BeanId 的内容,"TypeName:BeanName?" 或者 "[]?"
func RegisterTypeConverter ¶
func RegisterTypeConverter(fn interface{})
注册类型转换器,用于属性绑定,函数原型 func(string)struct
Types ¶
type Annotation ¶
type Annotation struct {
// contains filtered or unexported fields
}
设定 Bean 的各种元数据
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 (*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 (*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 ToBeanDefinition ¶
func ToBeanDefinition(name string, i interface{}) *BeanDefinition
将 Bean 转换为 BeanDefinition 对象
type Condition ¶
type Condition interface {
Matches(ctx SpringContext) bool
}
定义 Condition 接口,当判断条件返回 true 时 Bean 才会真正注册到 IoC 容器。
type ConditionNode ¶
type ConditionNode struct {
// contains filtered or unexported fields
}
定义 Condition 表达式的节点
func (*ConditionNode) Matches ¶
func (c *ConditionNode) Matches(ctx SpringContext) bool
type Conditional ¶
type Conditional struct {
// contains filtered or unexported fields
}
定义 Condition 表达式
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
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 (*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 (*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 (*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 OriginalBean ¶
type OriginalBean struct {
// contains filtered or unexported fields
}
保存原始对象的 SpringBean
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 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 的注册规则:
- 只能注册单例 Bean。
- AutoWireBeans 开始后不允许注册新的 Bean(性能考虑)。
- 原型 Bean 只能通过 BeanFactory 的形式使用,参见测试用例。