diff --git a/registry/definition.go b/registry/definition.go index 6f468b4..cf0076d 100644 --- a/registry/definition.go +++ b/registry/definition.go @@ -33,16 +33,15 @@ func (td *TypeDefinition) GetAnnotationByType(at reflect.Type, includeEmbedding } for _, v := range td.TypeAnnotations { + if at == reflect.TypeOf(v) { + return v + } + if includeEmbedding { if checkAnnotation(reflect.TypeOf(v), at) { return v } - } else { - if at == reflect.TypeOf(v) { - return v - } } - } return nil @@ -87,6 +86,26 @@ func (fd *FieldDefinition) GetAnnotation(name string) cda.Annotation { return fd.Annotations[name] } +func (fd *FieldDefinition) GetAnnotationByType(at reflect.Type, includeEmbedding bool) cda.Annotation { + if nil == fd.Annotations { + return nil + } + + for _, v := range fd.Annotations { + if at == reflect.TypeOf(v) { + return v + } + + if includeEmbedding { + if checkAnnotation(reflect.TypeOf(v), at) { + return v + } + } + } + + return nil +} + func FullName(pkgName, typeName string) string { return fmt.Sprintf("%s/%s", pkgName, typeName) } diff --git a/registry/registry.go b/registry/registry.go index 23ded48..4328806 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -149,12 +149,11 @@ func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} { logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] can not set", fd.FieldName)) } - annotation = fd.GetAnnotation(cdia.InjectTag) - if nil != annotation { + if annotation = fd.GetAnnotationByType(reflect.TypeOf((*cdia.Inject)(nil)), false); nil != annotation { fV = cr.GetInstance(fd.Type) } - annotation = fd.GetAnnotation(cdia.ResourceTag) - if nil != annotation { + + if annotation = fd.GetAnnotationByType(reflect.TypeOf((*cdia.Resource)(nil)), false); nil != annotation { n := annotation.(*cdia.Resource).Name if "" == n { n = fd.FieldName