bean

package module
v1.0.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2020 License: Apache-2.0 Imports: 3 Imported by: 2

README

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

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

Usage

  • ConstructorArgs 构造器注入
var definitions = []bean.Definition{
    {
        Name: "foo",
        Reflect: bean.NewReflect(NewHttpClient),
        ConstructorArgs: bean.ConstructorArgs{
            time.Duration(time.Second * 3),
        },
    },
}

// 必须返回指针类型
func NewHttpClient(timeout time.Duration) *http.Client {
    return &http.Client{
        Timeout: timeout,
    }
}

context := bean.NewApplicationContext(definitions)
foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
fmt.Println(fmt.Sprintf("%+v", foo))
  • Fields 字段注入
var definitions = []bean.Definition{
    {
        Name: "foo",
        Reflect: bean.NewReflect(http.Client{}),
        Fields: bean.Fields{
            "Timeout": time.Duration(time.Second * 3),
        },
    },
}

context := bean.NewApplicationContext(definitions)
foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
fmt.Println(fmt.Sprintf("%+v", foo))
  • ConstructorArgs + Fields 混合使用
var definitions = []bean.Definition{
    {
        Name: "foo",
        Reflect: bean.NewReflect(NewHttpClient),
        ConstructorArgs: bean.ConstructorArgs{
            time.Duration(time.Second * 3),
        },
        Fields: bean.Fields{
            "Timeout": time.Duration(time.Second * 2),
        },
    },
}

// 必须返回指针类型
func NewHttpClient(timeout time.Duration) *http.Client {
    return &http.Client{
        Timeout: timeout,
    }
}

context := bean.NewApplicationContext(definitions)
foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
fmt.Println(fmt.Sprintf("%+v", foo))
  • NewReference 引用

引用其他依赖注入

type Foo struct {
    Client *http.Client // 引用注入的都是指针类型
}

var definitions = []bean.Definition{
    {
        Name: "foo",
        Reflect: bean.NewReflect(Foo{}),
        },
        Fields: bean.Fields{
            "Client": NewReference("bar"),
        },
    },
    {
        Name: "bar",
        Reflect: bean.NewReflect(http.Client{}),
        Fields: bean.Fields{
            "Timeout": time.Duration(time.Second * 3),
        },
    },
}

context := bean.NewApplicationContext(definitions)
foo := context.Get("foo").(*Foo) // 返回的都是指针类型
cli := foo.Client
fmt.Println(fmt.Sprintf("%+v", cli))
  • Scope: SINGLETON 单例

定义组件为全局单例

var definitions = []bean.Definition{
    {
        Name: "foo",
        Scope: bean.SINGLETON, // 这里定义了单例模式
        Reflect: bean.NewReflect(http.Client{}),
        Fields: bean.Fields{
            "Timeout": time.Duration(time.Second * 3),
        },
    },
}

context := bean.NewApplicationContext(definitions)
foo := context.Get("foo").(*http.Client) // 返回的都是指针类型
fmt.Println(fmt.Sprintf("%+v", foo))
  • InitMethod 初始化方法

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

type Foo struct {
    Bar    string
}

func (c *Foo) Init() {
    c.Bar = "bar ..."
    fmt.Println("init")
}

var definitions = []bean.Definition{
    {
        Name:       "foo",
        InitMethod: "Init", // 这里定义了初始化方法
        Reflect: bean.NewReflect(Foo{}),
        Fields: bean.Fields{
            "Bar":    "bar",
        },
    },
}

context := bean.NewApplicationContext(definitions)
foo := context.Get("foo").(*Foo) // 返回的都是指针类型
fmt.Println(fmt.Sprintf("%+v", foo))
  • Refresh 动态刷新配置

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

type Foo struct {
    Bar    string
}

var definitions = []bean.Definition{
    {
        Name:       "foo",
        Reflect: bean.NewReflect(Foo{}),
        Fields: bean.Fields{
            "Bar":    "bar",
        },
    },
}

context := bean.NewApplicationContext(definitions)

// 第一次获取
foo := context.Get("foo").(*Foo)
fmt.Println(fmt.Sprintf("%+v", foo))

// 修改配置
bd := context.GetBeanDefinition("foo")
bd.Fields["Bar"] = "bar2"
bd.Refresh()

// 第二次获取就是新的配置
foo := context.Get("foo").(*Foo)
fmt.Println(fmt.Sprintf("%+v", foo))

License

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

Documentation

Index

Constants

View Source
const (
	PROTOTYPE = "prototype"
	SINGLETON = "singleton"
)

依赖场景类型

Variables

This section is empty.

Functions

func NewReflect

func NewReflect(i interface{}) func() reflect.Value

NewReflect 创建反射

Types

type ApplicationContext

type ApplicationContext struct {
	Definitions []BeanDefinition
	// contains filtered or unexported fields
}

ApplicationContext 应用上下文

func NewApplicationContext

func NewApplicationContext(definitions []BeanDefinition) *ApplicationContext

NewApplicationContext 创建应用上下文

func (*ApplicationContext) Get

func (t *ApplicationContext) Get(name string) interface{}

Get 快速获取实例

func (*ApplicationContext) GetBean

func (t *ApplicationContext) GetBean(name string, fields Fields, args ConstructorArgs) interface{}

GetBean 获取实例

func (*ApplicationContext) GetBeanDefinition

func (t *ApplicationContext) GetBeanDefinition(name string) *BeanDefinition

GetBeanDefinition 获取依赖定义

func (*ApplicationContext) Has

func (t *ApplicationContext) Has(name string) (ok bool)

Has 判断组件是否存在

func (*ApplicationContext) Init

func (t *ApplicationContext) Init()

Init 初始化

type BeanDefinition

type BeanDefinition struct {
	Name            string
	Reflect         func() reflect.Value
	Scope           string
	InitMethod      string
	ConstructorArgs ConstructorArgs
	Fields          Fields
	// contains filtered or unexported fields
}

BeanDefinition 依赖定义

func (*BeanDefinition) Refresh

func (t *BeanDefinition) Refresh()

Refresh 刷新

type ConstructorArgs

type ConstructorArgs []interface{}

ConstructorArgs 构造器参数

type Fields

type Fields map[string]interface{}

Fields 字段

type Reference

type Reference struct {
	Name string
}

Reference 引用

func NewReference

func NewReference(name string) Reference

NewReference 创建引用

type ReturnError

type ReturnError struct {
	// contains filtered or unexported fields
}

ReturnError 返回错误

func NewReturnError

func NewReturnError(err error) *ReturnError

NewReturnError 创建返回错误

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL