Golang語(yǔ)言使用像JAVA?Spring注解一樣的DI和AOP依賴注入實(shí)例
引言
Java Spring 在易用性和交互體驗(yàn)上足夠優(yōu)秀,同時(shí)語(yǔ)言本身也非常適合基于運(yùn)行時(shí)的注入機(jī)制。
即使社區(qū)已經(jīng)有很多基于運(yùn)行時(shí)的依賴注入, Go 實(shí)際上更多官方推崇的玩法是基于代碼生成和靜態(tài)分析,比如 wire 就是 google 提供的一個(gè)依賴注入實(shí)現(xiàn)。
wire 提供依賴注入
但是 wire 在易用性我認(rèn)為還存在一個(gè)使用體驗(yàn)上的問(wèn)題, 就是需要額外維護(hù) wire.Set 相關(guān)的聲明,比如:
要利用下列素材組裝出以下 Target 這樣一個(gè)結(jié)構(gòu)體,
type StructA struct{}
type StructB struct {
InterfaceC
}
type StructC struct {
StructA
}
func (StructC) Foo() {}
type InterfaceC interface {
Foo()
}
type Target struct {
StructA
StructB
InterfaceC
}額外聲明
你必須提供一份額外的聲明:
var (
_Set = wire.NewSet(
wire.Struct(new(StructA), "*"),
wire.Struct(new(StructB), "*"),
wire.Bind(new(InterfaceC), new(*StructC)),
wire.Struct(new(StructC), "*"),
wire.Struct(new(Target), "*"),
)
)這個(gè)需要開(kāi)發(fā)者自行額外維護(hù)的聲明,我認(rèn)為也是導(dǎo)致 wire 無(wú)法在企業(yè)大規(guī)模普及落地的一個(gè)重要原因。
其核心的交互體驗(yàn)受損在于,用戶的對(duì)象聲明和關(guān)系聲明會(huì)出現(xiàn)空間上的割裂,即使是對(duì)同樣對(duì)象的邏輯,也需要在不同的代碼文件中進(jìn)行維護(hù)。
即使額外使用各種中間 wire.NewSet 去組合,也沒(méi)辦法徹底優(yōu)化這個(gè)體驗(yàn)。
可以參考 JAVA Spring 的交互設(shè)計(jì) 用戶只需要在對(duì)象添加注解,就能完成聲明依賴注入關(guān)系的工作。
在筆者以往的工作中,都在團(tuán)隊(duì)內(nèi)維護(hù)和推廣了可以類似 Spring 使用注解自動(dòng)生成依賴注入聲明的工具,這個(gè)工具讓 wire 變得十分地易用。
因此,團(tuán)隊(duì)成功將依賴注入的模式落地到幾乎所有的 Golang 項(xiàng)目中,讓團(tuán)隊(duì)的代碼質(zhì)量和架構(gòu)設(shè)計(jì)能力都得到了極大地提升。
開(kāi)源版本 Gozz
在多年的沉淀和整合了其他功能后,這個(gè)工具的開(kāi)源版本就是 Gozz
Gozz 提供的 wire 插件 將會(huì)很有效的提升用戶使用 wire 的體驗(yàn)和上手難度 :
基本原理是: 通過(guò)對(duì)注解額外語(yǔ)法分析,以及注解對(duì)象上下文,可以直接推斷注入對(duì)象的注入方式以及注入?yún)?shù),然后直接依賴注入框架為生成注入聲明。
例如我們剛才提到的上述例子,使用 Gozz 后,可以直接把人工維護(hù)的各種 wire.Set 刪掉。
反而,只需要在代碼上加上注解:
// +zz:wire
type StructA struct{}
// +zz:wire
type StructB struct {
InterfaceC
}
// +zz:wire:bind=InterfaceC
type StructC struct {
StructA
}
func (StructC) Foo() {}
type InterfaceC interface {
Foo()
}
// +zz:wire:inject=./
type Target struct {
StructA
StructB
InterfaceC
}上面還出現(xiàn)的兩個(gè)選項(xiàng)意思就是:
bind 表示 進(jìn)行 interface的綁定
inject 表示為此對(duì)象生成目標(biāo)函數(shù) Injector 以及生成的文件地址
執(zhí)行 gozz run -p "wire" ${filename} 后
你會(huì)發(fā)現(xiàn)使用 wire 要額外加的所有東西都被生成好了,而且也自動(dòng)幫你執(zhí)行好了 wire
全過(guò)程,只需要幾條注解 加上 一條命令 你就得到了下面的完整依賴注入函數(shù):
func Initialize_Target() (*Target, func(), error) {
structA := StructA{}
structC := &StructC{
StructA: structA,
}
structB := StructB{
InterfaceC: structC,
}
target := &Target{
StructA: structA,
StructB: structB,
InterfaceC: structC,
}
return target, func() {
}, nil
}除了自動(dòng)化的依賴注入之外,Gozz 還可以在依賴注入中進(jìn)行AOP,自動(dòng)地生成 interface 的動(dòng)態(tài)代理
比如下面這個(gè)例子, Interface 綁定了兩個(gè)類型,其中一個(gè)有 aop 選項(xiàng)
最后的 Target 則需要 三種 Interface 來(lái)構(gòu)造,雖然他們其實(shí)都是同個(gè)類型的別名
type Implement struct{}
// +zz:wire:bind=InterfaceX
// +zz:wire:bind=InterfaceX2:aop
type Interface interface {
Foo(ctx context.Context, param int) (result int, err error)
Bar(ctx context.Context, param int) (result int, err error)
}
type InterfaceX Interface
type InterfaceX2 Interface
// +zz:wire:inject=/
type Target struct {
Interface
InterfaceX
InterfaceX2
}
func (Implement) Foo(ctx context.Context, param int) (result int, err error) {
return
}
func (Implement) Bar(ctx context.Context, param int) (result int, err error) {
return
}通過(guò)執(zhí)行 gozz run -p "wire" ./${filename}
會(huì)生成 以下的注入,你會(huì)發(fā)現(xiàn) InterfaceX2 的注入會(huì)被替換成wire02_impl_aop_InterfaceX2
一個(gè)自動(dòng)生成的結(jié)構(gòu)體
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//+build !wireinject
package wire02
// Injectors from wire_zinject.go:
// github.com/go-zing/gozz-doc-examples/wire02.Target
func Initialize_Target() (*Target, func(), error) {
implement := &Implement{}
wire02_impl_aop_InterfaceX2 := &_impl_aop_InterfaceX2{
_aop_InterfaceX2: implement,
}
target := &Target{
Interface: implement,
InterfaceX: implement,
InterfaceX2: wire02_impl_aop_InterfaceX2,
}
return target, func() {
}, nil
}在生成的另一個(gè)文件 wire_zzaop.go 可以看到它的定義:
type _aop_interceptor interface {
Intercept(v interface{}, name string, params, results []interface{}) (func(), bool)
}
// InterfaceX2
type (
_aop_InterfaceX2 InterfaceX2
_impl_aop_InterfaceX2 struct{ _aop_InterfaceX2 }
)
func (i _impl_aop_InterfaceX2) Foo(p0 context.Context, p1 int) (r0 int, r1 error) {
if t, x := i._aop_InterfaceX2.(_aop_interceptor); x {
if up, ok := t.Intercept(i._aop_InterfaceX2, "Foo",
[]interface{}{&p0, &p1},
[]interface{}{&r0, &r1},
); up != nil {
defer up()
} else if !ok {
return
}
}
return i._aop_InterfaceX2.Foo(p0, p1)
}
func (i _impl_aop_InterfaceX2) Bar(p0 context.Context, p1 int) (r0 int, r1 error) {
if t, x := i._aop_InterfaceX2.(_aop_interceptor); x {
if up, ok := t.Intercept(i._aop_InterfaceX2, "Bar",
[]interface{}{&p0, &p1},
[]interface{}{&r0, &r1},
); up != nil {
defer up()
} else if !ok {
return
}
}
return i._aop_InterfaceX2.Bar(p0, p1)
}簡(jiǎn)而言之 ,它通過(guò)實(shí)現(xiàn)了所有的原 Interface 方法對(duì)原綁定的調(diào)用進(jìn)行了一層代理封裝,并且可以通過(guò)代理封裝提供所有參數(shù)和返回值的指針,以及調(diào)用的原始對(duì)象和方法名。
只要通過(guò)一些指針斷言和接口操作,實(shí)際上我們就可以:
- 在函數(shù)調(diào)用進(jìn)行自定義前置和后置邏輯
- 獲取實(shí)際調(diào)用方及調(diào)用方法名
- 對(duì)函數(shù)參數(shù)及返回值進(jìn)行替換
- 不經(jīng)過(guò)實(shí)際調(diào)用方,直接終止調(diào)用
通過(guò)這些功能我們可以實(shí)現(xiàn):
- 檢查返回值錯(cuò)誤,自動(dòng)打印錯(cuò)誤堆棧及調(diào)用信息,自動(dòng)注入日志、鏈路追蹤、埋點(diǎn)上報(bào)等。
- 檢查授權(quán)狀態(tài)及訪問(wèn)權(quán)限。
- 對(duì)調(diào)用參數(shù)和返回值進(jìn)行自動(dòng)緩存。
- 檢查或替換 context.Context,添加超時(shí)或檢查中斷。
這個(gè)功能也是社區(qū)目前大部分依賴注入框架都沒(méi)辦法做到的,而使用 Gozz 只需要添加一個(gè)選項(xiàng) aop
實(shí)際上 gozz 在運(yùn)行時(shí)工具庫(kù) gozz-kit 中還提供了工具,可以幫大家生成這種關(guān)系依賴圖:
比如上面例子的運(yùn)行時(shí)依賴實(shí)際上就是:

gozz-wire 的強(qiáng)大兼容性和推斷能力
最后一個(gè)例子會(huì)展示 gozz-wire 的強(qiáng)大兼容性和推斷能力:
- 注入值對(duì)象
- 使用值對(duì)象綁定接口
- 引用類型作為結(jié)構(gòu)體
- 使用指定函數(shù)提供注入類型
- 使用結(jié)構(gòu)體字段值進(jìn)行注入
- 使用
set對(duì)注入進(jìn)行分組 - 使用額外的原生
wire.NewSet
//go:generate gozz run -p "wire" ./
// provide value and interface value
// +zz:wire:bind=io.Writer:aop
// +zz:wire
var Buffer = &bytes.Buffer{}
// provide referenced type
// +zz:wire
type NullString nullString
type nullString sql.NullString
// use provider function to provide referenced type alias
// +zz:wire
type String = string
func ProvideString() String {
return ""
}
// provide value from implicit type
// +zz:wire
var Bool = false
// +zz:wire:inject=/
type Target struct {
Buffer *bytes.Buffer
Writer io.Writer
NullString NullString
Int int
}
// origin wire set
// +zz:wire
var Set = wire.NewSet(wire.Value(Int))
var Int = 0
// mock set injector
// +zz:wire:inject=/:set=mock
type mockString sql.NullString
// mock set string
// provide type from function
// +zz:wire:set=mock
func MockString() String {
return "mock"
}
// mock set struct type provide fields
// +zz:wire:set=mock:field=*
type MockConfig struct{ Bool bool }
// mock set value
// +zz:wire:set=mock
var mock = &MockConfig{Bool: true}實(shí)際上如此復(fù)雜的注入場(chǎng)景,都可以被完美處理:
// github.com/go-zing/gozz-doc-examples/wire03.Target
func Initialize_Target() (*Target, func(), error) {
buffer := _wireBufferValue
wire03_aop_io_Writer := _wireBytesBufferValue
wire03_impl_aop_io_Writer := &_impl_aop_io_Writer{
_aop_io_Writer: wire03_aop_io_Writer,
}
string2 := ProvideString()
bool2 := _wireBoolValue
wire03NullString := NullString{
String: string2,
Valid: bool2,
}
int2 := _wireIntValue
target := &Target{
Buffer: buffer,
Writer: wire03_impl_aop_io_Writer,
NullString: wire03NullString,
Int: int2,
}
return target, func() {
}, nil
}
var (
_wireBufferValue = Buffer
_wireBytesBufferValue = Buffer
_wireBoolValue = Bool
_wireIntValue = Int
)
// github.com/go-zing/gozz-doc-examples/wire03.mockString
func Initialize_mock_mockString() (mockString, func(), error) {
string2 := MockString()
mockConfig := _wireMockConfigValue
bool2 := mockConfig.Bool
wire03MockString := mockString{
String: string2,
Valid: bool2,
}
return wire03MockString, func() {
}, nil
}
var (
_wireMockConfigValue = mock
)當(dāng)然 這些強(qiáng)大能力一定程度還是歸功于 wire 本身的優(yōu)秀, Gozz 只是站在了巨人的肩膀上。
以上其實(shí)都是 Gozz 提供的示例,在文檔頁(yè)面中都可以找到
而 wire 其實(shí)也是 Gozz 提供的強(qiáng)大插件之一,如果使用 Gozz 的其他插件,會(huì)得到更加優(yōu)秀的開(kāi)發(fā)體驗(yàn)和引導(dǎo)你進(jìn)行更合理的架構(gòu)設(shè)計(jì)。
以上就是Golang語(yǔ)言使用像JAVA Spring注解一樣的DI和AOP依賴注入實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于Go 依賴注入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Golang變量初始化/類型推斷/短聲明的問(wèn)題
這篇文章主要介紹了關(guān)于Golang變量初始化/類型推斷/短聲明的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
解決golang在import自己的包報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決golang在import自己的包報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
Go語(yǔ)言中同一個(gè)package中函數(shù)互相調(diào)用為undefined的解決
這篇文章主要介紹了Go語(yǔ)言中同一個(gè)package中函數(shù)互相調(diào)用為undefined的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
golang封裝一個(gè)執(zhí)行命令行的函數(shù)(return?stderr/stdout/exitcode)示例代碼
在?Go?語(yǔ)言中,您可以使用?os/exec?包來(lái)執(zhí)行外部命令,不通過(guò)調(diào)用?shell,并且能夠獲得進(jìn)程的退出碼、標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出,下面給大家分享golang封裝一個(gè)執(zhí)行命令行的函數(shù)(return?stderr/stdout/exitcode)的方法,感興趣的朋友跟隨小編一起看看吧2024-06-06
使用VSCODE配置GO語(yǔ)言開(kāi)發(fā)環(huán)境的完整步驟
Go語(yǔ)言是采用UTF8編碼的,理論上使用任何文本編輯器都能做Go語(yǔ)言開(kāi)發(fā),大家可以根據(jù)自己的喜好自行選擇,下面這篇文章主要給大家介紹了關(guān)于使用VSCODE配置GO語(yǔ)言開(kāi)發(fā)環(huán)境的完整步驟,需要的朋友可以參考下2022-11-11

