1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
singleton_bean_registry.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-09-02 00:40 . refactor
// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/**
*@describe:
*@author wfl19/Kristas
*@date 2021/08/31
*/
package bean_repository
import (
"fmt"
"gitee.com/kristas/booting-go/framework/common/util/ccmap"
"gitee.com/kristas/booting-go/framework/common/util/list"
"gitee.com/kristas/booting-go/framework/common/util/reflectx"
. "gitee.com/kristas/booting-go/framework/core/bean"
"reflect"
)
var (
singletonObjects *ccmap.ConcurrentMap
registeredSingletons list.Set
)
func init() {
singletonObjects = ccmap.NewConcurrentMap()
registeredSingletons = list.NewConcurrentSets()
}
func GetSingleton(name string) interface{} {
if ExistsSingletonBean(name) {
return singletonObjects.Get(name)
}
return nil
}
func ExistsSingletonBean(name string) bool {
return registeredSingletons.Exists(name)
}
func RegisterSingletonBean(bean interface{}) {
register(GetName(bean), bean)
}
func RegisterSingletonBeanWithName(name string, bean interface{}) {
register(name, bean)
}
func register(name string, obj interface{}) {
if ExistsSingletonBean(name) {
panic(fmt.Sprintf("duplicate bean: %s", name))
}
singletonObjects.Set(name, obj)
registeredSingletons.Put(name)
}
func GetSingletonByInterface(typ reflect.Type) []reflect.Value {
var valueSet = make([]reflect.Value, 0)
singletonObjects.ForEach(func(k string, v interface{}) {
if reflectx.IsImplement(v, reflect.New(typ).Interface()) {
valueSet = append(valueSet, reflect.ValueOf(v))
}
})
return valueSet
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.3.7

搜索帮助