1 Star 0 Fork 0

Arnolixi / web-drive

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
beanFactory.go 1.77 KB
Copy Edit Raw Blame History
Arnolixi authored 2021-11-17 16:13 . gin 脚手架
package arc
import (
"reflect"
)
type BeanFactoryImpl struct {
beanMapper BeanMapper
}
func NewBeanFactoryImpl() *BeanFactoryImpl {
return &BeanFactoryImpl{
beanMapper: make(BeanMapper),
}
}
var BeanFactory *BeanFactoryImpl
func init() {
BeanFactory = NewBeanFactoryImpl()
}
func (this *BeanFactoryImpl) Set(vList ...interface{}) {
if vList == nil || len(vList) == 0 {
return
}
for _, v := range vList {
this.beanMapper.add(v)
}
}
func (this *BeanFactoryImpl) Get(v interface{}) interface{} {
if v == nil {
return nil
}
getVal := this.beanMapper.get(v)
if getVal.IsValid() {
return getVal.Interface()
}
return nil
}
func (this *BeanFactoryImpl) GetBM() BeanMapper {
return this.beanMapper
}
func (this *BeanFactoryImpl) Apply(bean interface{}) {
if bean == nil {
return
}
v := reflect.ValueOf(bean)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return
}
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
if v.Field(i).CanSet() && field.Tag.Get("inject") != "" {
if field.Tag.Get("inject") == "-" {
// 注入指定 struct
if getV := this.Get(field.Type); getV != nil {
v.Field(i).Set(reflect.ValueOf(getV))
this.Apply(getV)
}
} else {
// TODO 支持表达式注入
continue
}
}
}
}
func (this *BeanFactoryImpl) InjectConfig(cfgs ...interface{}) {
for _, cfg := range cfgs {
t := reflect.TypeOf(cfgs)
if t.Kind() != reflect.Ptr {
panic("Required Ptr Object!")
}
if t.Elem().Kind() != reflect.Struct {
continue
}
this.Set(cfg)
this.Apply(cfg)
v := reflect.ValueOf(cfg)
for i := 0; i < t.NumMethod(); i++ {
m := v.Method(i)
callRet := m.Call(nil)
if callRet != nil && len(callRet) == 1 {
this.Set(callRet[0].Interface())
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/arnolixi/web-drive.git
git@gitee.com:arnolixi/web-drive.git
arnolixi
web-drive
web-drive
v1.0.6

Search

344bd9b3 5694891 D2dac590 5694891