golang wire 依赖注入

1 安装wire

1.1 通过命令直接安装

go install github.com/google/wire/cmd/wire@latest

1.2 通过go get方式安装

go get github.com/google/wire/cmd/wire

进入目录编译

cd C:\Users\leell\go\pkg\mod\github.com\google\wire@v0.6.0\cmd\wire
go build

然后将wire.exe移动到 C:\Users\leell\go\bin目录下

具体目录通过go env进行查看

C:\Users\leell\go>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=C:\Users\leell\go\bin

2 业务源码

//Handler 源码
type Handler struct {
	conf   *viper.Viper
	logger *zap.Logger
}

func NewHandler(conf *viper.Viper, logger *zap.Logger) *Handler {
	return &Handler{conf: conf, logger: logger}
}


//RoleHandler 源码
type RoleHandler struct {
	*Handler
	roleService *service.RoleService
}

func NewRoleController(controller *Handler, roleService *service.RoleService) *RoleHandler {
	return &RoleHandler{
		Handler:     controller,
		roleService: roleService,
	}
}

//RoleService 源码
type RoleService struct {
	*Service
	roleDao *dao.RoleDao
}

func NewRoleService(service *Service, roleDao *dao.RoleDao) *RoleService {
	return &RoleService{
		Service: service,
		roleDao: roleDao,
	}
}

//RoleDao源码
type RoleDao struct {
	*Dao
}

func NewRoleDao(dao *Dao) *RoleDao {
	return &RoleDao{Dao: dao}
}

//Dao 源码
type Dao struct {
	conf   *viper.Viper
	logger *zap.Logger
}

func NewDao(conf *viper.Viper, logger *zap.Logger) *Dao {
	return &Dao{conf: conf, logger: logger}
}

3 provider 提供者

provider/provider.go

package provider

import (
	"github.com/google/wire"

	"wire-first/dao"
	"wire-first/handler"
	"wire-first/server"
	"wire-first/service"
)

var ServerSet = wire.NewSet(server.NewServerHttp)

var HandlerSet = wire.NewSet(handler.NewHandler, handler.NewUserController, handler.NewRoleController)

var ServiceSet = wire.NewSet(service.NewService, service.NewUserService, service.NewRoleService)

var DaoSet = wire.NewSet(dao.NewDao, dao.NewUserDao, dao.NewRoleDao)

4 wire提供生成构建代码

wire/wire.go

//go:build wireinject
// +build wireinject

package wire

import (
	"net/http"

	"github.com/google/wire"
	"github.com/spf13/viper"
	"go.uber.org/zap"

	"wire-first/provider"
)

// wire.go 初始化模块
func NewApp(*viper.Viper, *zap.Logger) (*http.Server, error) {
	panic(wire.Build(
		provider.ServerSet,
		provider.HandlerSet,
		provider.ServiceSet,
		provider.DaoSet,
	))
}

5 通过wire生成gen代码

PS G:\github\wire-first> wire  gen wire/wire.go
wire: command-line-arguments: wrote G:\github\wire-first\wire\wire_gen.go

6 结果查看

wire/wire_gen.go

// Code generated by Wire. DO NOT EDIT.

//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject

package wire

import (
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"net/http"
	"wire-first/dao"
	"wire-first/handler"
	"wire-first/server"
	"wire-first/service"
)

// Injectors from wire.go:

// wire.go 初始化模块
func NewApp(viperViper *viper.Viper, logger *zap.Logger) (*http.Server, error) {
	handlerHandler := handler.NewHandler(viperViper, logger)
	serviceService := service.NewService(viperViper, logger)
	daoDao := dao.NewDao(viperViper, logger)
	userDao := dao.NewUserDao(daoDao)
	userService := service.NewUserService(serviceService, userDao)
	userHandler := handler.NewUserController(handlerHandler, userService)
	roleDao := dao.NewRoleDao(daoDao)
	roleService := service.NewRoleService(serviceService, roleDao)
	roleHandler := handler.NewRoleController(handlerHandler, roleService)
	httpServer := server.NewServerHttp(viperViper, logger, userHandler, roleHandler)
	return httpServer, nil
}

7 main.go代码

package main

import (
	"github.com/alecthomas/kingpin/v2"
	"go.uber.org/zap"

	"wire-first/config"
	"wire-first/wire"
)

var (
	cfgPath = kingpin.Flag("config", "the path of the config file").Default("app.yml").String()
)

func main() {
	kingpin.Parse()
	conf := config.NewConfig(*cfgPath)
	// 创建logger
	logger, err := zap.NewDevelopment()
	if err != nil {
		panic(err)
	}
	defer logger.Sync()
	app, err := wire.NewApp(conf, logger)
	if err != nil {
		logger.Error("Initialization failed",
			zap.Error(err),
		)
	}

	logger.Info("Server's running", zap.String("address", app.Addr))
	if err := app.ListenAndServe(); err != nil {
		logger.Error("Server Error",
			zap.String("key", "value"),
			zap.Error(err),
		)
	}
}

源码:wire-first: 依赖注入库wire的示例工程

相关推荐
武昌库里写JAVA6 分钟前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
Q_192849990613 分钟前
基于Spring Boot的九州美食城商户一体化系统
java·spring boot·后端
ZSYP-S42 分钟前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring
yuanbenshidiaos1 小时前
c++------------------函数
开发语言·c++
程序员_三木1 小时前
Three.js入门-Raycaster鼠标拾取详解与应用
开发语言·javascript·计算机外设·webgl·three.js
是小崔啊1 小时前
开源轮子 - EasyExcel01(核心api)
java·开发语言·开源·excel·阿里巴巴
tianmu_sama1 小时前
[Effective C++]条款38-39 复合和private继承
开发语言·c++
黄公子学安全1 小时前
Java的基础概念(一)
java·开发语言·python
liwulin05061 小时前
【JAVA】Tesseract-OCR截图屏幕指定区域识别0.4.2
java·开发语言·ocr
jackiendsc1 小时前
Java的垃圾回收机制介绍、工作原理、算法及分析调优
java·开发语言·算法