-
Notifications
You must be signed in to change notification settings - Fork 3
/
autowire4go.go
85 lines (69 loc) · 1.72 KB
/
autowire4go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package boot4go
import (
. "github.com/gohutool/boot4go/reflect"
. "reflect"
)
/**
* golang-sample源代码,版权归锦翰科技(深圳)有限公司所有。
* <p>
* 文件名称 : autowire4go.go
* 文件路径 :
* 作者 : DavidLiu
× Email: david.liu@ginghan.com
*
* 创建日期 : 2022/4/13 16:29
* 修改历史 : 1. [2022/4/13 16:29] 创建文件 by LongYong
*/
type SupportType Kind
const (
CLASS = Struct
METHOD = Func
FIELD = String
)
type AutoWiredMeta struct {
Object Value
Value Value
Tag StructTagPlus
Field StructField
Interface any
}
type AutowiredBeanHandler interface {
ID() string
BeforeAutowired(meta AutoWiredMeta) any
PostAutowired(source any, meta AutoWiredMeta)
SupportType() []Kind
}
const AUTOCONFIG_AUTOWIRED_TAG = "auto"
type AutoConfigurationAutowiredBeanHandler struct {
}
func (s *AutoConfigurationAutowiredBeanHandler) ID() string {
return AUTOCONFIG_AUTOWIRED_TAG
}
func (s *AutoConfigurationAutowiredBeanHandler) BeforeAutowired(meta AutoWiredMeta) any {
bn, _ := meta.Tag.Get(AUTOWIRED_FLAG + AUTOCONFIG_AUTOWIRED_TAG)
k := meta.Field.Type.Kind()
if len(bn) == 0 {
if k == Ptr {
bn = meta.Value.Elem().Type().String()
} else {
bn = meta.Value.Type().String()
}
}
var v any
if b, _ := Context.GetPooledBean(bn); b != nil {
v = b
} else {
if k == Ptr {
v, _ = Context.GetBean(meta.Value.Elem().Type())
} else {
v, _ = Context.GetBean(meta.Value.Type())
}
}
//fmt.Printf("%v %v %v\n", meta.Type, meta.Interface, meta.Tag)
return v
}
func (s *AutoConfigurationAutowiredBeanHandler) PostAutowired(source any, meta AutoWiredMeta) {
}
func (s *AutoConfigurationAutowiredBeanHandler) SupportType() []Kind {
return []Kind{CLASS, METHOD, FIELD}
}