本篇內(nèi)容主要講解“如何用golang仿spring ioc/aop實(shí)現(xiàn)藍(lán)圖效果”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何用golang仿spring ioc/aop實(shí)現(xiàn)藍(lán)圖效果”吧!
創(chuàng)新互聯(lián)長(zhǎng)期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新華企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,新華網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
Spring的主要特性: 1. 控制反轉(zhuǎn)(Inversion of Control, IoC) 2. 面向容器 3. 面向切面(AspectOriented Programming, AOP) 源碼gitee地址: https://gitee.com/ioly/learning.gooop 原文鏈接: https://my.oschina.net/ioly
參考spring常用注解,使用golang編寫“基于注解的靜態(tài)代碼增強(qiáng)器/生成器”
配置: ComponentScan,Configuration, Bean
Bean聲明:Component, Service, Controller
Bean注入:Autowried
AOP注解:Before, After, Around, PointCut
前兩天都是在整外圍接口,沒(méi)說(shuō)清楚到底要搞啥
今天以@RestController為例,把項(xiàng)目目標(biāo)理清楚:
以gin框架為基礎(chǔ),描述一個(gè)訂單CRUD服務(wù)
以@RestController為例,手寫增強(qiáng)前后的代碼,描繪如何增強(qiáng)
增強(qiáng)前,應(yīng)當(dāng)盡可能簡(jiǎn)潔,降低框架侵入性
增強(qiáng)后,與框架很好的結(jié)合,把臟活累活默默干好
OrderController: 訂單服務(wù)控制器
OrderController_Enhanced:增強(qiáng)后的訂單服務(wù)控制器
添加SetOrderService方法以便依賴注入
添加xxx_Enhanced方法,以集成到gin框架
添加RegisterRestController方法,以注冊(cè)到Bean容器
添加init()方法,以便Bean容器引用
IOrderService:訂單持久化服務(wù)接口
MockOrderService:訂單持久化服務(wù)的實(shí)現(xiàn),碼略
dto/, entity/: 訂單服務(wù)的實(shí)體類和數(shù)值類,碼略
IBeanRegistry:bean注冊(cè)表接口
IRestController:RESTFul控制器接口
IControllerRegistry:RESTFul控制器注冊(cè)表及其默認(rèn)實(shí)現(xiàn)
訂單服務(wù)控制器
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) }
增強(qiáng)后的訂單服務(wù)控制器
添加SetOrderService方法以便依賴注入
添加xxx_Enhanced方法,以集成到gin框架
添加RegisterRestController方法,以注冊(cè)到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) }
訂單持久化服務(wù)接口
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注冊(cè)表接口
package bean type IBeanRegistry interface { All() []interface{} }
RESTFul控制器接口
package controller import "github.com/gin-gonic/gin" type IRestController interface { RegisterRestController(r *gin.Engine) }
RESTFul控制器注冊(cè)表及其默認(rèn)實(shí)現(xiàn)
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()
到此,相信大家對(duì)“如何用golang仿spring ioc/aop實(shí)現(xiàn)藍(lán)圖效果”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!