您好,登錄后才能下訂單哦!
本篇內容主要講解“如何用golang仿spring ioc/aop實現藍圖效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何用golang仿spring ioc/aop實現藍圖效果”吧!
Spring的主要特性: 1. 控制反轉(Inversion of Control, IoC) 2. 面向容器 3. 面向切面(AspectOriented Programming, AOP) 源碼gitee地址: https://gitee.com/ioly/learning.gooop 原文鏈接: https://my.oschina.net/ioly
參考spring常用注解,使用golang編寫“基于注解的靜態代碼增強器/生成器”
配置: ComponentScan,Configuration, Bean
Bean聲明:Component, Service, Controller
Bean注入:Autowried
AOP注解:Before, After, Around, PointCut
前兩天都是在整外圍接口,沒說清楚到底要搞啥
今天以@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控制器注冊表及其默認實現
訂單服務控制器
package controller import ( "learning/gooop/spring/demo/order/dto" "learning/gooop/spring/demo/order/entity" "learning/gooop/spring/demo/order/service" ) // OrderController handles rest requests for CRUD orders // @RestController // @RequestMapping path=/order type OrderController struct { @Autowried orderService service.IOrderService } // Save create or update an order // @PostMapping func (me *OrderController) Save(head *entity.OrderHeadEntity, items []*entity.OrderItemEntity) error { return me.orderService.Save(head, items) } // View gets order and order items // @GetMapping func (me *OrderController) View(orderID int) (error, *dto.OrderDTO) { return me.orderService.Get(orderID) } // Query query order headers by custom conditions // @GetMapping func (me *OrderController) Query(customerID int, statusFlag int, dateFrom string, dateTo string, pageNO int, pageSize int) (error, []*dto.OrderHeadDTO) { return me.orderService.Query(customerID, statusFlag, dateFrom, dateTo, pageNO, pageSize) }
增強后的訂單服務控制器
添加SetOrderService方法以便依賴注入
添加xxx_Enhanced方法,以集成到gin框架
添加RegisterRestController方法,以注冊到Bean容器
添加init()方法,以便Bean容器引用
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 } e = me.Save(r.Order, r.Items) if e != nil { c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()}) return } 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()}) } 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) { return me.orderService.Query(customerID, statusFlag, dateFrom, dateTo, pageNO, pageSize) } // 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 } e, d := me.Query(r.CustomerID, r.StatusFlag, r.DateFrom, r.DateTO, r.PageNO, r.PageSize) if e != nil { c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()}) return } 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) }
訂單持久化服務接口
package service import ( "learning/gooop/spring/demo/order/dto" "learning/gooop/spring/demo/order/entity" ) type IOrderService interface { Save(head *entity.OrderHeadEntity, items []*entity.OrderItemEntity) error Get(orderID int) (error, *dto.OrderDTO) Query(customerID int, statusFlag int, dateFrom string, dateTo string, pageNO int, pageSize int) (error, []*dto.OrderHeadDTO) }
bean注冊表接口
package bean type IBeanRegistry interface { All() []interface{} }
RESTFul控制器接口
package controller import "github.com/gin-gonic/gin" type IRestController interface { RegisterRestController(r *gin.Engine) }
RESTFul控制器注冊表及其默認實現
package controller import ( "github.com/gin-gonic/gin" "learning/gooop/spring/demo/framework/bean" "sync" ) type IControllerRegistry interface { bean.IBeanRegistry Register(it IRestController) Apply(r *gin.Engine) } type tDefaultControllerRegistry struct { rwmutex *sync.RWMutex items []IRestController } func (me *tDefaultControllerRegistry) All() []interface{} { me.rwmutex.RLock() defer me.rwmutex.RUnlock() all := make([]interface{}, len(me.items)) for i, it := range me.items { all[i] = it } return all } func (me *tDefaultControllerRegistry) Register(it IRestController) { me.rwmutex.Lock() defer me.rwmutex.Unlock() me.items = append(me.items, it) } func (me *tDefaultControllerRegistry) Apply(r *gin.Engine) { me.rwmutex.RLock() defer me.rwmutex.RLock() for _, it := range me.items { it.RegisterRestController(r) } } func newDefaultControllerRegistry() IControllerRegistry { return &tDefaultControllerRegistry{ new(sync.RWMutex), []IRestController{}, } } var ControllerRegistry = newDefaultControllerRegistry()
到此,相信大家對“如何用golang仿spring ioc/aop實現藍圖效果”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。