项目作者: mix-go

项目描述 :
DI, IoC container, reference spring bean
高级语言: Go
项目地址: git://github.com/mix-go/bean.git
创建时间: 2019-12-31T01:41:56Z
项目社区:https://github.com/mix-go/bean

开源协议:Apache License 2.0

下载


OpenMix 出品:https://openmix.org

Mix Bean

DI、IoC 容器,参考 spring bean 设计

DI, IoC container, reference spring bean

该库还有 php 版本:https://github.com/mix-php/bean

Overview

一个创建对象以及处理对象依赖关系的库,该库可以实现统一管理依赖,全局对象管理,动态配置刷新等。

Installation

  • 安装
  1. go get -u github.com/mix-go/bean

Usage

  • ConstructorArgs 构造器注入
  1. var definitions = []bean.Definition{
  2. {
  3. Name: "foo",
  4. Reflect: bean.NewReflect(NewHttpClient),
  5. ConstructorArgs: bean.ConstructorArgs{
  6. time.Duration(time.Second * 3),
  7. },
  8. },
  9. }
  10. // 必须返回指针类型
  11. func NewHttpClient(timeout time.Duration) *http.Client {
  12. return &http.Client{
  13. Timeout: timeout,
  14. }
  15. }
  16. context := bean.NewApplicationContext(definitions)
  17. foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
  18. fmt.Println(fmt.Sprintf("%+v", foo))
  • Fields 字段注入
  1. var definitions = []bean.Definition{
  2. {
  3. Name: "foo",
  4. Reflect: bean.NewReflect(http.Client{}),
  5. Fields: bean.Fields{
  6. "Timeout": time.Duration(time.Second * 3),
  7. },
  8. },
  9. }
  10. context := bean.NewApplicationContext(definitions)
  11. foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
  12. fmt.Println(fmt.Sprintf("%+v", foo))
  • ConstructorArgs + Fields 混合使用
  1. var definitions = []bean.Definition{
  2. {
  3. Name: "foo",
  4. Reflect: bean.NewReflect(NewHttpClient),
  5. ConstructorArgs: bean.ConstructorArgs{
  6. time.Duration(time.Second * 3),
  7. },
  8. Fields: bean.Fields{
  9. "Timeout": time.Duration(time.Second * 2),
  10. },
  11. },
  12. }
  13. // 必须返回指针类型
  14. func NewHttpClient(timeout time.Duration) *http.Client {
  15. return &http.Client{
  16. Timeout: timeout,
  17. }
  18. }
  19. context := bean.NewApplicationContext(definitions)
  20. foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
  21. fmt.Println(fmt.Sprintf("%+v", foo))
  • NewReference 引用

引用其他依赖注入

  1. type Foo struct {
  2. Client *http.Client // 引用注入的都是指针类型
  3. }
  4. var definitions = []bean.Definition{
  5. {
  6. Name: "foo",
  7. Reflect: bean.NewReflect(Foo{}),
  8. },
  9. Fields: bean.Fields{
  10. "Client": NewReference("bar"),
  11. },
  12. },
  13. {
  14. Name: "bar",
  15. Reflect: bean.NewReflect(http.Client{}),
  16. Fields: bean.Fields{
  17. "Timeout": time.Duration(time.Second * 3),
  18. },
  19. },
  20. }
  21. context := bean.NewApplicationContext(definitions)
  22. foo := context.Get("foo").(*Foo) // 返回的都是指针类型
  23. cli := foo.Client
  24. fmt.Println(fmt.Sprintf("%+v", cli))
  • Scope: SINGLETON 单例

定义组件为全局单例

  1. var definitions = []bean.Definition{
  2. {
  3. Name: "foo",
  4. Scope: bean.SINGLETON, // 这里定义了单例模式
  5. Reflect: bean.NewReflect(http.Client{}),
  6. Fields: bean.Fields{
  7. "Timeout": time.Duration(time.Second * 3),
  8. },
  9. },
  10. }
  11. context := bean.NewApplicationContext(definitions)
  12. foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
  13. fmt.Println(fmt.Sprintf("%+v", foo))
  • InitMethod 初始化方法

对象创建完成并且 ConstructorArgs + Fields 两种注入全部完成后执行该方法,用来初始化处理。

  1. type Foo struct {
  2. Bar string
  3. }
  4. func (c *Foo) Init() {
  5. c.Bar = "bar ..."
  6. fmt.Println("init")
  7. }
  8. var definitions = []bean.Definition{
  9. {
  10. Name: "foo",
  11. InitMethod: "Init", // 这里定义了初始化方法
  12. Reflect: bean.NewReflect(Foo{}),
  13. Fields: bean.Fields{
  14. "Bar": "bar",
  15. },
  16. },
  17. }
  18. context := bean.NewApplicationContext(definitions)
  19. foo := context.Get("foo").(*Foo) // 返回的都是指针类型
  20. fmt.Println(fmt.Sprintf("%+v", foo))
  • Refresh 动态刷新配置

这个通常用于通过微服务配置中心实现动态刷新微服务配置的功能。

  1. type Foo struct {
  2. Bar string
  3. }
  4. var definitions = []bean.Definition{
  5. {
  6. Name: "foo",
  7. Reflect: bean.NewReflect(Foo{}),
  8. Fields: bean.Fields{
  9. "Bar": "bar",
  10. },
  11. },
  12. }
  13. context := bean.NewApplicationContext(definitions)
  14. // 第一次获取
  15. foo := context.Get("foo").(*Foo)
  16. fmt.Println(fmt.Sprintf("%+v", foo))
  17. // 修改配置
  18. bd := context.GetBeanDefinition("foo")
  19. bd.Fields["Bar"] = "bar2"
  20. bd.Refresh()
  21. // 第二次获取就是新的配置
  22. foo := context.Get("foo").(*Foo)
  23. fmt.Println(fmt.Sprintf("%+v", foo))

License

Apache License Version 2.0, http://www.apache.org/licenses/