A Go injection library inspired by Java Spring Framework.
Java Spring Framework is a famous IoC framework. This library try to imitate spring's function.
-
Define your go structs
type Astruct struct { // native type IntValue int StringValue string // struct type Asingleton *Bstruct Aprototype *Cstruct } type Bstruct struct { Name string } type Cstruct struct { Name string }
-
Define the beans
beans := gs.Beans( gs.Bean(Astruct{}). ID("a_id"). Property( "IntValue", 123, ). Property( "StringValue", "a string", ). Property( "Asingleton", gs.Ref("b_id"), ). Property( "Aprototype", gs.Ref("c_id"), ), gs.Bean(Bstruct{}). ID("b_id"). Property( "Name", "It's B.", ), gs.Bean(Cstruct{}). ID("c_id"). Property( "Name", "It's C.", ), )
-
Put it into a context
ctx, e := gs.NewApplicationContext(beans...)
-
Create a bean from context
bean, e := ctx.GetBean("a_id")
-
Use the bean
json, _ := json.MarshalIndent(bean, "", " ") fmt.Println(string(json))
{ "IntValue": 123, "StringValue": "a string", "Asingleton": { "Name": "It's B." }, "Aprototype": { "Name": "It's C." } }