Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions gs/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (app *App) run(resourceLocator ResourceLocator) error {
return err
}

if err := app.container.refresh(false); err != nil {
if err := app.container.refresh(true); err != nil {
return err
}

Expand All @@ -93,7 +93,6 @@ func (app *App) run(resourceLocator ResourceLocator) error {

app.onAppStart(app.container)

app.container.clear()
logger.Info("application started successfully")

// Responding to the Ctrl+C and kill commands in the console.
Expand Down
5 changes: 0 additions & 5 deletions gs/gs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ type Context interface {
Go(fn func(ctx context.Context))
}

// ContextAware injects the Context into a struct as the field GSContext.
type ContextAware struct {
GSContext Context `autowire:""`
}

type tempContainer struct {
props *conf.Properties
beans []*BeanDefinition
Expand Down
13 changes: 13 additions & 0 deletions gs/gs_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (c *container) Get(i interface{}, selectors ...BeanSelector) error {
return errors.New("i can't be nil")
}

if nil == c.tempContainer {
return errors.New("Ioc container is auto cleared, if you want use Get/Wire please use autowire tag to inject `gs.Context` ")
}

v := reflect.ValueOf(i)
if v.Kind() != reflect.Ptr {
return errors.New("i must be pointer")
Expand All @@ -99,6 +103,15 @@ func (c *container) Get(i interface{}, selectors ...BeanSelector) error {
// If the input is a constructor, it immediately executes that constructor and then performs property binding and dependency injection on the returned result.
// In both cases, the function returns the actual value of the bean object after its execution is complete.
func (c *container) Wire(objOrCtor interface{}, ctorArgs ...arg.Arg) (interface{}, error) {

if objOrCtor == nil {
return nil, errors.New("objOrCtor can't be nil")
}

if nil == c.tempContainer {
return nil, errors.New("Ioc container is auto cleared, if you want use Get/Wire please use autowire tag to inject `gs.Context` ")
}

b := NewBean(objOrCtor, ctorArgs...)
stack := newWiringStack(c.logger)
if err := c.wireBean(b, stack); nil != err || len(stack.beans) > 0 {
Expand Down
50 changes: 50 additions & 0 deletions gs/gs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,11 @@ func TestDestroyDependence(t *testing.T) {
assert.Nil(t, err)
}

// ContextAware injects the Context into a struct as the field GSContext.
type ContextAware struct {
GSContext Context `autowire:""`
}

type TContextAware struct {
ContextAware
}
Expand Down Expand Up @@ -3033,3 +3038,48 @@ func TestConfiguration(t *testing.T) {
})

}

func TestContextGetWire(t *testing.T) {

type GetObject struct {
Name string `value:"${app.name}"`
}

type ContextAware struct {
Ctx Context `autowire:""`
}

var appCtx Context

c := New()
c.Properties().Set("app.name", "testapp")
c.Object(new(ContextAware))
getbd := c.Provide(func(ctx Context) *GetObject {
appCtx = ctx
return &GetObject{}
})

err := c.Refresh()
assert.Nil(t, err)

t.Run("Context#Get", func(t *testing.T) {
var getObj *GetObject
err := appCtx.Get(&getObj)
assert.Nil(t, err)
assert.Equal(t, getObj, getbd.Interface())
assert.Equal(t, getObj.Name, "testapp")
})

t.Run("Context#Wire", func(t *testing.T) {

type WireObject struct {
Get *GetObject `autowire:""`
}

wireObj, err := appCtx.Wire(new(WireObject))
assert.Nil(t, err)
assert.Equal(t, wireObj.(*WireObject).Get, getbd.Interface())
assert.Equal(t, wireObj.(*WireObject).Get.Name, "testapp")
})

}