In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use golang to imitate spring ioc/aop to achieve blueprint effect", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use golang to imitate spring ioc/aop to achieve blueprint effect"!
The main features of SpringSpring: 1. Control inversion (Inversion of Control, IoC) 2. Facing Container 3. Aspect-oriented (AspectOriented Programming, AOP) source gitee address: https://gitee.com/ioly/learning.gooop original text link: https://my.oschina.net/ioly target
Refer to the common notes in spring and write "Annotation-based static Code Enhancer / Generator" in golang.
Configuration: ComponentScan,Configuration, Bean
Bean statement: Component, Service, Controller
Bean injection: Autowried
AOP comments: Before, After, Around, PointCut
Subgoal (Day 4)
I was working on the peripheral interface two days ago, but I didn't make it clear what I was going to do.
Today, take @ RestController as an example to clarify the objectives of the project:
Describe an order CRUD service based on the gin framework
Take @ RestController as an example, handwritten code before and after enhancement, describing how to enhance
Before enhancement, it should be as concise as possible to reduce the intrusiveness of the framework.
After the enhancement, it is well combined with the framework to do the dirty work silently.
Design
OrderController: order Service Controller
OrderController_Enhanced: enhanced order service controller
Add SetOrderService methods for dependency injection
Add xxx_Enhanced methods to integrate into the gin framework
Add a RegisterRestController method to register with the Bean container
Add the init () method so that the Bean container references
IOrderService: order persistence service interface
MockOrderService: implementation of order persistence service, code strategy
Entity class and numerical class of dto/, entity/: order service, code outline
IBeanRegistry:bean registry interface
IRestController:RESTFul controller interface
IControllerRegistry:RESTFul Controller Registry and its default implementation
OrderController.go
Order service controller
Package controllerimport ("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=/ordertype OrderController struct {@ Autowried orderService service.IOrderService} / / Save create or update an order// @ PostMappingfunc (me * OrderController) Save (head * entity.OrderHeadEntity) Items [* entity.OrderItemEntity) error {return me.orderService.Save (head, items)} / / View gets order and order items// @ GetMappingfunc (me * OrderController) View (orderID int) (error, * dto.OrderDTO) {return me.orderService.Get (orderID)} / / Query query order headers by custom conditions// @ GetMappingfunc (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)} OrderController_Enhanced.go
Enhanced order service controller
Add SetOrderService methods for dependency injection
Add xxx_Enhanced methods to integrate into the gin framework
Add a RegisterRestController method to register with the Bean container
Add the init () method so that the Bean container references
Package controllerimport ("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=/ordertype OrderController_ Enhanced struct {/ / Autowired orderService service.IOrderService} / / SetOrderService is auto generated setter method for injecting service.IOrderService into me.orderServicefunc (me * OrderController_Enhanced) SetOrderService (it interface {}) {me.orderService = it. (service.IOrderService)} / / Save create or update an order// PostMappingfunc (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.Savetype OrderController_Save_ParamsDTO struct {Order * entity.OrderHeadEntity Items [] * entity.OrderItemEntity} / / View_Enhanced is the enhanced version of Savefunc (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// GetMappingfunc (me * OrderController_Enhanced) View (orderID int) (error, * dto.OrderDTO) {return me.orderService.Get (orderID)} / / View_Enhanced is the enhanced version of Viewfunc (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// GetMappingfunc (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 PagedQuerytype OrderController_Query_ParamsDTO struct {CustomerID int StatusFlag int DateFrom string DateTO string PageNO int PageSize int} / / Query_Enhanced is the enhanced version of PagedQueryfunc (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 interfacefunc (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.ControllerRegistryfunc init () {it: = new (OrderController_Enhanced) controller.ControllerRegistry.Register (it)} IOrderService.go
Order persistence service interface
Package serviceimport ("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)} IBeanRegistry.go
Bean registry interface
Package beantype IBeanRegistry interface {All () [] interface {}} IRestController.go
RESTFul controller interface
Package controllerimport "github.com/gin-gonic/gin" type IRestController interface {RegisterRestController (r * gin.Engine)} IControllerRegistry.go
RESTFul Controller Registry and its default implementation
Package controllerimport ("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 () so far I believe you have a deeper understanding of "how to use golang to imitate spring ioc/aop to achieve blueprint effect". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.