缘起

最近阅读 [Spring Boot技术内幕: 架构设计与实现原理] (朱智胜 , 2020.6)
本系列笔记拟采用golang练习之
Talk is cheap, show me the code.

Spring

  1. Spring的主要特性:
  2. 1. 控制反转(Inversion of Control, IoC
  3. 2. 面向容器
  4. 3. 面向切面(AspectOriented Programming, AOP
  5. 源码gitee地址:
  6. https://gitee.com/ioly/learning.gooop
  7. 原文链接:
  8. https://my.oschina.net/ioly

目标

  • 参考spring常用注解,使用golang编写“基于注解的静态代码增强器/生成器”
    • 配置: ComponentScan,Configuration, Bean
    • Bean声明:Component, Service, Controller
    • Bean注入:Autowried
    • AOP注解:Before, After, Around, PointCut

子目标(Day 4)

  • 前两天都是在整外围接口,没说清楚到底要搞啥
  • 今天以@RestController为例,把项目目标理清楚:
    • 以gin框架为基础,描述一个订单CRUD服务
    • 以@RestController为例,手写增强前后的代码,描绘如何增强
    • 增强前,应当尽可能简洁,降低框架侵入性
    • 增强后,与框架很好的结合,把脏活累活默默干好

设计

  • OrderController: 订单服务控制器
  • OrderController_Enhanced:增强后的订单服务控制器
    • 添加SetOrderService方法以便依赖注入
    • 添加xxx_Enhanced方法,以集成到gin框架
    • 添加RegisterRestController方法,以注册到Bean容器
    • 添加init()方法,以便Bean容器引用
  • IOrderService:订单持久化服务接口
  • MockOrderService:订单持久化服务的实现,码略
  • dto/, entity/: 订单服务的实体类和数值类,码略
  • IBeanRegistry:bean注册表接口
  • IRestController:RESTFul控制器接口
  • IControllerRegistry:RESTFul控制器注册表及其默认实现

OrderController.go

订单服务控制器

  1. package controller
  2. import (
  3. "learning/gooop/spring/demo/order/dto"
  4. "learning/gooop/spring/demo/order/entity"
  5. "learning/gooop/spring/demo/order/service"
  6. )
  7. // OrderController handles rest requests for CRUD orders
  8. // @RestController
  9. // @RequestMapping path=/order
  10. type OrderController struct {
  11. orderService service.IOrderService
  12. }
  13. // Save create or update an order
  14. // @PostMapping
  15. func (me *OrderController) Save(head *entity.OrderHeadEntity, items []*entity.OrderItemEntity) error {
  16. return me.orderService.Save(head, items)
  17. }
  18. // View gets order and order items
  19. // @GetMapping
  20. func (me *OrderController) View(orderID int) (error, *dto.OrderDTO) {
  21. return me.orderService.Get(orderID)
  22. }
  23. // Query query order headers by custom conditions
  24. // @GetMapping
  25. func (me *OrderController) Query(customerID int, statusFlag int, dateFrom string, dateTo string, pageNO int, pageSize int) (error, []*dto.OrderHeadDTO) {
  26. return me.orderService.Query(customerID, statusFlag, dateFrom, dateTo, pageNO, pageSize)
  27. }

OrderController_Enhanced.go

增强后的订单服务控制器

  • 添加SetOrderService方法以便依赖注入
  • 添加xxx_Enhanced方法,以集成到gin框架
  • 添加RegisterRestController方法,以注册到Bean容器
  • 添加init()方法,以便Bean容器引用 ```go package controller

import ( “github.com/gin-gonic/gin” “learning/gooop/spring/demo/framework/bean/controller” “learning/gooop/spring/demo/order/dto” “learning/gooop/spring/demo/order/entity” “learning/gooop/spring/demo/order/service” “net/http” )

// OrderController_Enhanced handles rest requests for CRUD orders // @RestController // @RequestMapping path=/order type OrderController_Enhanced struct { // @Autowired orderService service.IOrderService }

// SetOrderService is auto generated setter method for injecting service.IOrderService into me.orderService func (me *OrderController_Enhanced) SetOrderService(it interface{}) { me.orderService = it.(service.IOrderService) }

// Save create or update an order // @PostMapping func (me OrderController_Enhanced) Save(head entity.OrderHeadEntity, items []*entity.OrderItemEntity) error { return me.orderService.Save(head, items) }

// OrderController_Save_ParamsDTO is auto generated struct for wrapping parameters of OrderController.Save type OrderController_Save_ParamsDTO struct { Order entity.OrderHeadEntity Items []entity.OrderItemEntity }

// View_Enhanced is the enhanced version of Save func (me OrderController_Enhanced) Save_Enhanced(c gin.Context) { r := new(OrderController_Save_ParamsDTO) e := c.BindJSON(r) if e != nil { c.JSON(http.StatusBadRequest, gin.H{“ok”: false, “error”: e.Error()}) return }

  1. e = me.Save(r.Order, r.Items)
  2. if e != nil {
  3. c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()})
  4. return
  5. }
  6. c.JSON(http.StatusOK, gin.H{"ok": true})

}

// View gets order and order items // @GetMapping func (me OrderController_Enhanced) View(orderID int) (error, dto.OrderDTO) { return me.orderService.Get(orderID) }

// View_Enhanced is the enhanced version of View func (me OrderController_Enhanced) View_Enhanced(c gin.Context) { id := c.GetInt(“id”) e, d := me.View(id) if e != nil { c.JSON(http.StatusInternalServerError, gin.H{“ok”: false, “error”: e.Error()}) }

  1. c.JSON(http.StatusOK, d)

}

// Query query order headers by custom conditions // @GetMapping func (me OrderController_Enhanced) Query(customerID int, statusFlag int, dateFrom string, dateTo string, pageNO int, pageSize int) (error, []dto.OrderHeadDTO) { panic(“implements me”) }

// OrderController_Query_ParamsDTO is auto generated struct for wrapping parameters of PagedQuery type OrderController_Query_ParamsDTO struct { CustomerID int StatusFlag int DateFrom string DateTO string PageNO int PageSize int }

// Query_Enhanced is the enhanced version of PagedQuery func (me OrderController_Enhanced) Query_Enhanced(c gin.Context) { r := new(OrderController_Query_ParamsDTO) e := c.Bind(r) if e != nil { c.JSON(http.StatusBadRequest, gin.H{“ok”: false, “error”: e.Error()}) return }

  1. e, d := me.Query(r.CustomerID, r.StatusFlag, r.DateFrom, r.DateTO, r.PageNO, r.PageSize)
  2. if e != nil {
  3. c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()})
  4. return
  5. }
  6. c.JSON(http.StatusOK, gin.H{"ok": true, "data": d})

}

// RegisterRestController is auto generated to implements controller.IRestController interface func (me OrderController_Enhanced) RegisterRestController(r gin.Engine) { r.POST(“/order/save”, me.Save_Enhanced) r.GET(“/order/view”, me.View_Enhanced) r.GET(“/order/query”, me.Query_Enhanced) }

// init is auto generated to register OrderController_Enhanced into controller.ControllerRegistry func init() { it := new(OrderController_Enhanced) controller.ControllerRegistry.Register(it) }

  1. <a name="XFDpc"></a>
  2. # IOrderService.go
  3. 订单持久化服务接口
  4. ```go
  5. package service
  6. import (
  7. "learning/gooop/spring/demo/order/dto"
  8. "learning/gooop/spring/demo/order/entity"
  9. )
  10. type IOrderService interface {
  11. Save(head *entity.OrderHeadEntity, items []*entity.OrderItemEntity) error
  12. Get(orderID int) (error, *dto.OrderDTO)
  13. Query(customerID int, statusFlag int, dateFrom string, dateTo string, pageNO int, pageSize int) (error, []*dto.OrderHeadDTO)
  14. }

IBeanRegistry.go

bean注册表接口

  1. package bean
  2. type IBeanRegistry interface {
  3. All() []interface{}
  4. }

IRestController.go

RESTFul控制器接口

  1. package controller
  2. import "github.com/gin-gonic/gin"
  3. type IRestController interface {
  4. RegisterRestController(r *gin.Engine)
  5. }

IControllerRegistry.go

RESTFul控制器注册表及其默认实现

  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "learning/gooop/spring/demo/framework/bean"
  5. "sync"
  6. )
  7. type IControllerRegistry interface {
  8. bean.IBeanRegistry
  9. Register(it IRestController)
  10. Apply(r *gin.Engine)
  11. }
  12. type tDefaultControllerRegistry struct {
  13. rwmutex *sync.RWMutex
  14. items []IRestController
  15. }
  16. func (me *tDefaultControllerRegistry) All() []interface{} {
  17. me.rwmutex.RLock()
  18. defer me.rwmutex.RUnlock()
  19. all := make([]interface{}, len(me.items))
  20. for i, it := range me.items {
  21. all[i] = it
  22. }
  23. return all
  24. }
  25. func (me *tDefaultControllerRegistry) Register(it IRestController) {
  26. me.rwmutex.Lock()
  27. defer me.rwmutex.Unlock()
  28. me.items = append(me.items, it)
  29. }
  30. func (me *tDefaultControllerRegistry) Apply(r *gin.Engine) {
  31. me.rwmutex.RLock()
  32. defer me.rwmutex.RLock()
  33. for _, it := range me.items {
  34. it.RegisterRestController(r)
  35. }
  36. }
  37. func newDefaultControllerRegistry() IControllerRegistry {
  38. return &tDefaultControllerRegistry{
  39. new(sync.RWMutex),
  40. []IRestController{},
  41. }
  42. }
  43. var ControllerRegistry = newDefaultControllerRegistry()

(未完待续)