PostConstruct added

This commit is contained in:
crusader 2018-09-04 15:44:42 +09:00
parent 93072d1a5e
commit d72db42216
4 changed files with 43 additions and 3 deletions

2
Gopkg.lock generated
View File

@ -5,7 +5,7 @@
branch = "master" branch = "master"
name = "git.loafle.net/overflow/annotation-go" name = "git.loafle.net/overflow/annotation-go"
packages = ["."] packages = ["."]
revision = "178b12d303bdd83566a35d71cce68b0c6cc1f4d3" revision = "b94873cf6c40c8a46393f4e9f229eee152e2eb1b"
[[projects]] [[projects]]
branch = "master" branch = "master"

View File

@ -0,0 +1,19 @@
package annotation
// @Inject(name? string)
import (
"reflect"
oa "git.loafle.net/overflow/annotation-go"
)
func init() {
oa.Register(PostConstructAnnotationType)
}
var PostConstructAnnotationType = reflect.TypeOf((*PostConstructAnnotation)(nil))
type PostConstructAnnotation struct {
oa.Annotation `@name:"@PostConstruct"`
}

View File

@ -170,6 +170,7 @@ func (r *InstanceRegistry) GetInstance(t reflect.Type) (instance interface{}, er
td *TypeDefinition td *TypeDefinition
injectableV interface{} injectableV interface{}
ok bool ok bool
name string
) )
rt, _, _ := our.GetTypeInfo(t) rt, _, _ := our.GetTypeInfo(t)
@ -192,7 +193,8 @@ func (r *InstanceRegistry) GetInstance(t reflect.Type) (instance interface{}, er
if a, err := oa.GetTypeAnnotation(td.Type, annotation.InjectableAnnotationType); nil == err && nil != a { if a, err := oa.GetTypeAnnotation(td.Type, annotation.InjectableAnnotationType); nil == err && nil != a {
_a := a.(*annotation.InjectableAnnotation) _a := a.(*annotation.InjectableAnnotation)
if "" != _a.Name { if "" != _a.Name {
r.instanceByName[_a.Name] = instance name = _a.Name
r.instanceByName[name] = instance
} }
} }
@ -200,8 +202,13 @@ func (r *InstanceRegistry) GetInstance(t reflect.Type) (instance interface{}, er
defer func() { defer func() {
if nil != err { if nil != err {
instance = nil instance = nil
if _, ok := r.instanceByType[td.RealType]; ok {
delete(r.instanceByType, td.RealType) delete(r.instanceByType, td.RealType)
} }
if _, ok := r.instanceByName[name]; ok {
delete(r.instanceByName, name)
}
}
}() }()
var ( var (
@ -273,6 +280,13 @@ func (r *InstanceRegistry) GetInstance(t reflect.Type) (instance interface{}, er
} }
} }
if as, err := oa.GetMethodAnnotationsByType(td.Type, annotation.PostConstructAnnotationType); nil == err && nil != as {
for k := range as {
ins := make([]reflect.Value, 0)
reflect.ValueOf(instance).MethodByName(k).Call(ins)
}
}
return return
} }

View File

@ -1,6 +1,7 @@
package di package di
import ( import (
"log"
"reflect" "reflect"
"testing" "testing"
@ -21,9 +22,15 @@ var InjectServiceType = reflect.TypeOf((*InjectService)(nil))
type InjectService struct { type InjectService struct {
Service *InjectableService `annotation:"@Inject('name': 'InjectableService')"` Service *InjectableService `annotation:"@Inject('name': 'InjectableService')"`
_Init oa.MethodAnnotation `annotation:"@PostConstruct()"`
R string R string
} }
func (s *InjectService) Init() {
log.Print("Init")
}
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
type args struct { type args struct {
parent Registry parent Registry