Skip to content

Commit 2e614f0

Browse files
committed
Fix method bean parent resolve incorrect
1 parent 32763d1 commit 2e614f0

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

gs/arg/arg.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,23 @@ func (r *Callable) In(i int) (reflect.Type, bool) {
362362
return r.fnType.In(i), true
363363
}
364364

365+
func (r *Callable) Parent() (selector interface{}, ok bool) {
366+
// check receiver
367+
if selector, ok = r.In(0); !ok || !utils.IsBeanType(selector.(reflect.Type)) {
368+
return nil, false
369+
}
370+
371+
// bean selector
372+
if arg, exists := r.Arg(0); exists {
373+
switch arg.(type) {
374+
case utils.BeanDefinition:
375+
return arg, true
376+
}
377+
}
378+
379+
return
380+
}
381+
365382
// Call invokes the function with its binding arguments processed in the IoC
366383
// container. If the function returns an error, then the Call returns it.
367384
func (r *Callable) Call(ctx Context) ([]reflect.Value, error) {

gs/gs.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,7 @@ func (c *container) resolveBean(b *BeanDefinition) error {
487487

488488
// method bean 先确定 parent bean 是否存在
489489
if b.method {
490-
selector, ok := b.f.Arg(0)
491-
if !ok || selector == "" {
492-
selector, ok = b.f.In(0)
493-
}
490+
selector, ok := b.f.Parent()
494491
if ok {
495492
parents, err := c.findBean(selector)
496493
if err != nil {

gs/gs_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,6 +3048,7 @@ func TestLogLogger(t *testing.T) {
30483048

30493049
type testFoo struct {
30503050
prefix string
3051+
name string
30513052
}
30523053

30533054
type testBar struct {
@@ -3070,12 +3071,12 @@ func (tac *testAutoConfiguration) NewEmpty() *BeanDefinition {
30703071
return NewBean(new(struct{})).Name("empty")
30713072
}
30723073

3073-
func (tac *testAutoConfiguration) newFoo() *testFoo {
3074-
return &testFoo{prefix: tac.Prefix}
3074+
func (tac *testAutoConfiguration) newFoo(name string) *testFoo {
3075+
return &testFoo{prefix: tac.Prefix, name: name}
30753076
}
30763077

30773078
func (tac *testAutoConfiguration) NewFoo() *BeanDefinition {
3078-
return NewBean(tac.newFoo)
3079+
return NewBean(tac.newFoo, "${name}")
30793080
}
30803081

30813082
func (tac *testAutoConfiguration) NewBar(foo *testFoo) (*testBar, error) {
@@ -3097,6 +3098,7 @@ func TestConfiguration(t *testing.T) {
30973098
p := conf.New()
30983099
p.Set("prefix", "hello")
30993100
p.Set("open", "true")
3101+
p.Set("name", "go-spring")
31003102

31013103
err := c.Properties().Refresh(p)
31023104
assert.Nil(t, err)
@@ -3109,6 +3111,7 @@ func TestConfiguration(t *testing.T) {
31093111

31103112
subject := bd.Interface().(*testConfiguration)
31113113
assert.Equal(t, subject.Subject.Bar.foo.prefix, "hello")
3114+
assert.Equal(t, subject.Subject.Bar.foo.name, "go-spring")
31123115
})
31133116

31143117
t.Run("test Configuration with conditional", func(t *testing.T) {
@@ -3118,6 +3121,7 @@ func TestConfiguration(t *testing.T) {
31183121
p.Set("prefix", "hello")
31193122
p.Set("open", "true")
31203123
p.Set("enable", "false")
3124+
p.Set("name", "go-spring")
31213125

31223126
err := c.Properties().Refresh(p)
31233127
assert.Nil(t, err)

0 commit comments

Comments
 (0)