Skip to content

A Go injection library inspired by Java Spring Framework.

License

Notifications You must be signed in to change notification settings

yarencheng/gospring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gospring Build Status codecov Go Report Card

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.

Quick start

  1. 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
    }
  2. 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.",
            ),
    )
  3. Put it into a context

    ctx, e := gs.NewApplicationContext(beans...)
  4. Create a bean from context

    bean, e := ctx.GetBean("a_id")
  5. 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."
        }
    }

About

A Go injection library inspired by Java Spring Framework.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages