Skip to content

Commit f91ef89

Browse files
lvan100lianghuan
authored andcommitted
code refactor
1 parent 6715ffd commit f91ef89

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

gs/gs.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/go-spring/spring-core/gs/internal/gs"
2626
"github.com/go-spring/spring-core/gs/internal/gs_app"
2727
"github.com/go-spring/spring-core/gs/internal/gs_arg"
28-
"github.com/go-spring/spring-core/gs/internal/gs_bean"
2928
"github.com/go-spring/spring-core/gs/internal/gs_cond"
3029
"github.com/go-spring/spring-core/gs/internal/gs_conf"
3130
"github.com/go-spring/spring-core/gs/internal/gs_core"
@@ -166,8 +165,10 @@ type (
166165
)
167166

168167
type (
169-
BeanInit = gs_bean.BeanInit
170-
BeanDestroy = gs_bean.BeanDestroy
168+
BeanInitFunc = gs.BeanInitFunc
169+
BeanDestroyFunc = gs.BeanDestroyFunc
170+
BeanInitInterface = gs.BeanInitInterface
171+
BeanDestroyInterface = gs.BeanDestroyInterface
171172
)
172173

173174
type (

gs/internal/gs/gs.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ import (
2525
"github.com/go-spring/spring-core/conf"
2626
)
2727

28-
// InitFunc defines the prototype for initialization functions,
28+
// BeanInitFunc defines the prototype for initialization functions,
2929
// e.g., `func(bean)` or `func(bean) error`.
30-
type InitFunc = interface{}
30+
type BeanInitFunc = interface{}
3131

32-
// DestroyFunc defines the prototype for destroy functions,
32+
// BeanDestroyFunc defines the prototype for destroy functions,
3333
// e.g., `func(bean)` or `func(bean) error`.
34-
type DestroyFunc = interface{}
34+
type BeanDestroyFunc = interface{}
35+
36+
// BeanInitInterface defines an interface for bean initialization.
37+
type BeanInitInterface interface {
38+
OnBeanInit(ctx Context) error
39+
}
40+
41+
// BeanDestroyInterface defines an interface for bean destruction.
42+
type BeanDestroyInterface interface {
43+
OnBeanDestroy()
44+
}
3545

3646
// BeanSelectorInterface is an interface for selecting beans.
3747
type BeanSelectorInterface interface {
@@ -149,9 +159,9 @@ type BeanRegistration interface {
149159
// SetName sets the name of the bean.
150160
SetName(name string)
151161
// SetInit sets the initialization function for the bean.
152-
SetInit(fn InitFunc)
162+
SetInit(fn BeanInitFunc)
153163
// SetDestroy sets the destruction function for the bean.
154-
SetDestroy(fn DestroyFunc)
164+
SetDestroy(fn BeanDestroyFunc)
155165
// SetCondition adds a condition for the bean.
156166
SetCondition(conditions ...Condition)
157167
// SetDependsOn sets the beans that this bean depends on.
@@ -192,13 +202,13 @@ func (d *beanBuilder[T]) Name(name string) *T {
192202
}
193203

194204
// Init sets the initialization function for the bean.
195-
func (d *beanBuilder[T]) Init(fn InitFunc) *T {
205+
func (d *beanBuilder[T]) Init(fn BeanInitFunc) *T {
196206
d.b.SetInit(fn)
197207
return *(**T)(unsafe.Pointer(&d))
198208
}
199209

200210
// Destroy sets the destruction function for the bean.
201-
func (d *beanBuilder[T]) Destroy(fn DestroyFunc) *T {
211+
func (d *beanBuilder[T]) Destroy(fn BeanDestroyFunc) *T {
202212
d.b.SetDestroy(fn)
203213
return *(**T)(unsafe.Pointer(&d))
204214
}

gs/internal/gs_bean/bean.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,11 @@ func (status BeanStatus) String() string {
6262
}
6363
}
6464

65-
// BeanInit defines an interface for bean initialization.
66-
type BeanInit interface {
67-
OnBeanInit(ctx gs.Context) error
68-
}
69-
70-
// BeanDestroy defines an interface for bean destruction.
71-
type BeanDestroy interface {
72-
OnBeanDestroy()
73-
}
74-
7565
// BeanMetadata holds the metadata information of a bean.
7666
type BeanMetadata struct {
7767
f gs.Callable // Callable for the bean (ctor or method).
78-
init interface{} // Initialization function for the bean.
79-
destroy interface{} // Destruction function for the bean.
68+
init gs.BeanInitFunc // Initialization function for the bean.
69+
destroy gs.BeanDestroyFunc // Destruction function for the bean.
8070
dependsOn []gs.BeanSelectorInterface // List of dependencies for the bean.
8171
exports []reflect.Type // List of exported types for the bean.
8272
conditions []gs.Condition // List of conditions for the bean.
@@ -93,12 +83,12 @@ type BeanMetadata struct {
9383
}
9484

9585
// Init returns the initialization function of the bean.
96-
func (d *BeanMetadata) Init() interface{} {
86+
func (d *BeanMetadata) Init() gs.BeanInitFunc {
9787
return d.init
9888
}
9989

10090
// Destroy returns the destruction function of the bean.
101-
func (d *BeanMetadata) Destroy() interface{} {
91+
func (d *BeanMetadata) Destroy() gs.BeanDestroyFunc {
10292
return d.destroy
10393
}
10494

@@ -249,7 +239,7 @@ func validLifeCycleFunc(fnType reflect.Type, beanType reflect.Type) bool {
249239
}
250240

251241
// SetInit sets the initialization function for the bean.
252-
func (d *BeanDefinition) SetInit(fn gs.InitFunc) {
242+
func (d *BeanDefinition) SetInit(fn gs.BeanInitFunc) {
253243
if validLifeCycleFunc(reflect.TypeOf(fn), d.Type()) {
254244
d.init = fn
255245
return
@@ -258,7 +248,7 @@ func (d *BeanDefinition) SetInit(fn gs.InitFunc) {
258248
}
259249

260250
// SetDestroy sets the destruction function for the bean.
261-
func (d *BeanDefinition) SetDestroy(fn gs.DestroyFunc) {
251+
func (d *BeanDefinition) SetDestroy(fn gs.BeanDestroyFunc) {
262252
if validLifeCycleFunc(reflect.TypeOf(fn), d.Type()) {
263253
d.destroy = fn
264254
return

gs/internal/gs_core/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (c *Container) Wire(objOrCtor interface{}, ctorArgs ...gs.Arg) (interface{}
313313
}
314314

315315
// 如果 bean 实现了 BeanInit 接口,则执行其 OnInit 方法。
316-
if f, ok := b.Interface().(gs_bean.BeanInit); ok {
316+
if f, ok := b.Interface().(gs.BeanInitInterface); ok {
317317
if err = f.OnBeanInit(c); err != nil {
318318
return nil, err
319319
}

gs/internal/gs_core/wire.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (s *WiringStack) getSortedDestroyers() ([]func(), error) {
139139
destroy := func(v reflect.Value, fn interface{}) func() {
140140
return func() {
141141
if fn == nil {
142-
v.Interface().(gs_bean.BeanDestroy).OnBeanDestroy()
142+
v.Interface().(gs.BeanDestroyInterface).OnBeanDestroy()
143143
} else {
144144
fnValue := reflect.ValueOf(fn)
145145
out := fnValue.Call([]reflect.Value{v})
@@ -525,7 +525,7 @@ func (c *Container) wireBean(b *gs_bean.BeanDefinition, stack *WiringStack) erro
525525
}()
526526

527527
// Record the destroy function for the bean, if it exists.
528-
if _, ok := b.Interface().(gs_bean.BeanDestroy); ok || b.Destroy() != nil {
528+
if _, ok := b.Interface().(gs.BeanDestroyInterface); ok || b.Destroy() != nil {
529529
haveDestroy = true
530530
d := stack.saveDestroyer(b)
531531
if i := stack.destroyers.Back(); i != nil {
@@ -615,7 +615,7 @@ func (c *Container) wireBean(b *gs_bean.BeanDefinition, stack *WiringStack) erro
615615
}
616616

617617
// If the bean implements the BeanInit interface, execute its OnBeanInit method.
618-
if f, ok := b.Interface().(gs_bean.BeanInit); ok {
618+
if f, ok := b.Interface().(gs.BeanInitInterface); ok {
619619
if err = f.OnBeanInit(c); err != nil {
620620
return err
621621
}

0 commit comments

Comments
 (0)