SpringCore

package
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2019 License: Apache-2.0 Imports: 5 Imported by: 10

Documentation

Overview

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

Index

Constants

View Source
const (
	Uninitialized = iota // 还未初始化
	Initializing         // 正在初始化
	Initialized          // 完成初始化
)

SpringBean 初始化状态值

Variables

View Source
var (
	EMPTY_VALUE = reflect.Value{}
)

Functions

func ParseBeanId

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

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

func TypeName

func TypeName(t reflect.Type) string

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

Types

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 BeanDefinition

type BeanDefinition struct {
	Bean  SpringBean    // Bean 对象
	Name  string        // Bean 名称,可能为空
	Init  int           // Bean 初始化状态
	Type  reflect.Type  // Bean 反射得到的类型
	Value reflect.Value // Bean 反射得到的值
}

定义 BeanDefinition 类型

func ToBeanDefinition

func ToBeanDefinition(name string, bean SpringBean) *BeanDefinition

将 SpringBean 转换为 BeanDefinition 对象

func (*BeanDefinition) Match

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

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

type BeanInitialization

type BeanInitialization interface {
	InitBean(ctx SpringContext)
}

SpringBean 初始化接口

type BeanKey

type BeanKey struct {
	Type reflect.Type
	Name string
}

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

type DefaultSpringContext

type DefaultSpringContext struct {
	Frozen        bool                            // 冻结 Bean 注册
	Properties    map[string]interface{}          // 所有属性值的集合
	BeanMap       map[BeanKey]*BeanDefinition     // 所有 Bean 的集合
	BeanCache     map[reflect.Type]*BeanCacheItem // Bean 的分组缓存
	TypeConverter map[reflect.Type]interface{}    // 类型转换器的集合
}

SpringContext 的默认版本

func NewDefaultSpringContext

func NewDefaultSpringContext() *DefaultSpringContext

工厂函数

func (*DefaultSpringContext) AutoWireBeans

func (ctx *DefaultSpringContext) AutoWireBeans()

自动绑定所有的 SpringBean

func (*DefaultSpringContext) CollectBeans

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

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

func (*DefaultSpringContext) FindBean

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

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

func (*DefaultSpringContext) FindBeanByName

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

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

func (*DefaultSpringContext) GetAllBeansDefinition

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

获取所有的 bean 对象

func (*DefaultSpringContext) GetAllProperties

func (ctx *DefaultSpringContext) GetAllProperties() map[string]interface{}

获取所有的属性值

func (*DefaultSpringContext) GetBean

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

根据类型获取单例 Bean,多于 1 个会 panic,找不到也会 panic。

func (*DefaultSpringContext) GetBeanByName

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

根据名称和类型获取单例 Bean,多于 1 个会 panic,找不到也会 panic。

func (*DefaultSpringContext) GetBeanCacheItem

func (ctx *DefaultSpringContext) GetBeanCacheItem(t reflect.Type) (*BeanCacheItem, bool)

func (*DefaultSpringContext) GetBoolProperty

func (ctx *DefaultSpringContext) GetBoolProperty(name string) bool

获取布尔型属性值,属性名称不支持大小写。

func (*DefaultSpringContext) GetDefaultProperty

func (ctx *DefaultSpringContext) GetDefaultProperty(name string, defaultValue interface{}) (interface{}, bool)

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

func (*DefaultSpringContext) GetFloatProperty

func (ctx *DefaultSpringContext) GetFloatProperty(name string) float64

获取浮点型属性值,属性名称不支持大小写。

func (*DefaultSpringContext) GetIntProperty

func (ctx *DefaultSpringContext) GetIntProperty(name string) int64

获取有符号整型属性值,属性名称不支持大小写。

func (*DefaultSpringContext) GetPrefixProperties

func (ctx *DefaultSpringContext) GetPrefixProperties(prefix string) map[string]interface{}

获取指定前缀的属性值集合

func (*DefaultSpringContext) GetProperty

func (ctx *DefaultSpringContext) GetProperty(name string) interface{}

获取属性值,属性名称不支持大小写。

func (*DefaultSpringContext) GetStringProperty

func (ctx *DefaultSpringContext) GetStringProperty(name string) string

获取字符串型属性值,属性名称不支持大小写。

func (*DefaultSpringContext) GetUintProperty

func (ctx *DefaultSpringContext) GetUintProperty(name string) uint64

获取无符号整型属性值,属性名称不支持大小写。

func (*DefaultSpringContext) LoadProperties

func (ctx *DefaultSpringContext) LoadProperties(filename string)

加载属性配置文件

func (*DefaultSpringContext) MustCollectBeans

func (ctx *DefaultSpringContext) MustCollectBeans(i interface{})

收集数组或指针定义的所有符合条件的 Bean 对象,收集不到会 panic。

func (*DefaultSpringContext) RegisterBean

func (ctx *DefaultSpringContext) RegisterBean(bean SpringBean)

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

func (*DefaultSpringContext) RegisterBeanDefinition

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

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

func (*DefaultSpringContext) RegisterNameBean

func (ctx *DefaultSpringContext) RegisterNameBean(name string, bean SpringBean)

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

func (*DefaultSpringContext) RegisterTypeConverter

func (ctx *DefaultSpringContext) RegisterTypeConverter(fn interface{})

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

func (*DefaultSpringContext) SetProperty

func (ctx *DefaultSpringContext) SetProperty(name string, value interface{})

设置属性值,属性名称不支持大小写。

func (*DefaultSpringContext) WireBean

func (ctx *DefaultSpringContext) WireBean(bean SpringBean) error

绑定外部指定的 SpringBean

func (*DefaultSpringContext) WireBeanDefinition

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

绑定 BeanDefinition 指定的 SpringBean

type SpringBean

type SpringBean interface{}

定义 SpringBean 类型

type SpringContext

type SpringContext interface {
	// 注册单例 Bean,不指定名称,重复注册会 panic。
	RegisterBean(bean SpringBean)

	// 注册单例 Bean,需指定名称,重复注册会 panic。
	RegisterNameBean(name string, bean SpringBean)

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

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

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

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

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

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

	// 收集数组或指针定义的所有符合条件的 Bean 对象,收集不到会 panic。
	MustCollectBeans(i interface{})

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

	// 加载属性配置文件
	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{}

	// 自动绑定所有的 SpringBean
	AutoWireBeans()

	// 绑定外部指定的 SpringBean
	WireBean(bean SpringBean) error

	// 注册类型转换器,用于属性绑定,函数原型 func(string)struct
	RegisterTypeConverter(fn interface{})
}

定义 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