您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么使用golang仿spring ioc/aop基于注解的靜態代碼增強器/生成器”,在日常操作中,相信很多人在怎么使用golang仿spring ioc/aop基于注解的靜態代碼增強器/生成器問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用golang仿spring ioc/aop基于注解的靜態代碼增強器/生成器”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
手擼golang spring ioc/aop 之2
Spring基于J2EE技術實現了一套輕量級的 Java Web Service系統應用框架。 它有很多優秀的特性,很多公司都選擇把 Spring作為產品或項目的基礎開發架構。 Spring的特性包括輕量、控制反轉 (Inversion of Control, IoC)、 面向容器、面向切面(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
構思app的運行模式:
本地standlone模式運行
提供基于cli的命令行實時交互
生成旁路代碼:只掃描源代碼,不修改源代碼,增強后的代碼加統一后綴
設計cli交互指令集:
config save:保存配置
config saveas <name>:另存配置
watch add <dir>:添加代碼掃描目錄
watch del <dir>:移除代碼掃描目錄
watch list:顯示當前掃描的代碼目錄集合
gen:生成增強代碼,也就是掃描所有注解,并生成增強類和增強方法
config/IConfiguration:配置接口
command/ICmd:指令接口
command/ICmdBuilder:指令構建器接口
command/ICmdContext:指令執行上下文接口
config_cmd/SaveCmd: 保存配置
config_cmd/SaveASCmd: 另存配置
watch_cmd/AddCmd: 添加監視
watch_cmd/DelCmd: 移除監視
watch_cmd/ListCmd: 顯示已監視目錄的列表
gen_cmd/GenCmd: 生成增強類和增強方法
model/IEventDrivenModel:“事件驅動”的邏輯編排模型
logger: 日志接口,略
配置接口
package config // IConfiguration defines system configuration interface type IConfiguration interface { GetProjectName() string SetProjectName(string) GetWatchPaths() []string AddWatchPath(string) DelWatchPath(string) Save() error SaveAS(string) error }
指令接口
package command import "fmt" // ICmd defines cli command interface type ICmd interface { fmt.Stringer // Apply apply current command into use Apply(ICmdContext) error }
指令構建器接口
package command // ICmdBuilder parse input string and create an ICmd instance type ICmdBuilder interface { Build(string) (error, ICmd) }
指令執行上下文接口
package command import "learning/gooop/spring/autogen/config" // ICmdContext provides context info for all commands type ICmdContext interface { GetConfiguration() config.IConfiguration }
保存配置
package config_cmd import ( "learning/gooop/spring/autogen/command" ) // SaveCmd calls service.Save() to save current configuration, in JSON format type SaveCmd int // SaveCmdBuilder parse cli input and build a SaveCmd instance type SaveCmdBuilder int const gSaveCmdString = "config save" var gSaveCmdInstance = new(SaveCmd) func (me *SaveCmd) String() string { return gSaveCmdString } func (me *SaveCmd) Apply(c command.ICmdContext) error { // todo: fixme panic("implements me") } func (me *SaveCmdBuilder) Build(line string) (error, command.ICmd) { if line != gSaveCmdString { return nil, nil } return nil, gSaveCmdInstance }
另存配置
package config_cmd import ( "errors" "learning/gooop/spring/autogen/command" "strings" ) // SaveASCmd calls service.SaveAS() to save current config into specific file, in JSON format type SaveASCmd struct { file string } // SaveASCmdBuilder parse cli input and returns a SaveASCmd instance type SaveASCmdBuilder int const gSaveASCmdPrefix = "config saveas " func (me *SaveASCmd) String() string { return gSaveASCmdPrefix + me.file } func (me *SaveASCmd) Apply(c command.ICmdContext) error { // todo: fixme panic("implements me") } func (me *SaveASCmdBuilder) Build(line string) (error, command.ICmd) { if !strings.HasPrefix(line, gSaveASCmdPrefix) { return nil, nil } file := strings.TrimSpace(line[len(gSaveASCmdPrefix):]) if len(file) <= 0 { return errors.New("empty file path"), nil } return nil, &SaveASCmd{file } }
添加監視
package watch_cmd import ( "learning/gooop/spring/autogen/command" "os" "strings" ) // AddCmd calls service.WatchAdd() to add dir to watch list type AddCmd struct { dir string } type AddCmdBuilder int var gAddCmdPrefix = "watch add " func (me *AddCmd) String() string { return gAddCmdPrefix + me.dir } func (me *AddCmd) Apply(c command.ICmdContext) error { // todo: fixme panic("implements me") } func (me *AddCmdBuilder) Build(line string) (error, command.ICmd) { // check prefix if !strings.HasPrefix(line, gAddCmdPrefix) { return nil, nil } // get dir dir := strings.TrimSpace(line[len(gAddCmdPrefix):]) // check dir _,e := os.Stat(dir) if e != nil { return e, nil } // ok return nil, &AddCmd{dir } }
移除監視
package watch_cmd import ( "learning/gooop/spring/autogen/command" "os" "strings" ) // DelCmd calls service.WatchDel() to remove dir from watch list type DelCmd struct { dir string } type DelCmdBuilder int var gDelCmdPrefix = "watch del " func (me *DelCmd) String() string { return gDelCmdPrefix + me.dir } func (me *DelCmd) Apply(c command.ICmdContext) error { // todo: fixme panic("implements me") } func (me *DelCmdBuilder) Build(line string) (error, command.ICmd) { // check prefix if !strings.HasPrefix(line, gDelCmdPrefix) { return nil, nil } // get dir dir := strings.TrimSpace(line[len(gAddCmdPrefix):]) // check dir _,e := os.Stat(dir) if e != nil { return e, nil } // ok return nil, &DelCmd{ dir } }
顯示已監視目錄的列表
package watch_cmd import ( "learning/gooop/spring/autogen/command" ) // ListCmd calls service.WatchList type ListCmd int // ListCmdBuilder parse cli input and try to build a ListCmd instance type ListCmdBuilder int const gListCmdString1 = "watch list" const gListCmdString2 = "watch ls" var gListCmdSingleton = new(ListCmd) func (me *ListCmd) String() string { return gListCmdString1 } func (me *ListCmd) Apply(c command.ICmdContext) error { // todo: panic("implements me") } func (me *ListCmdBuilder) Build(line string) (error, command.ICmd) { if line != gListCmdString1 && line != gListCmdString2 { return nil, nil } return nil, gListCmdSingleton }
生成增強類和增強方法
package gen_cmd import ( "learning/gooop/spring/autogen/command" ) // GenCmd calls service.Gen() to generate enhanced code files at once type GenCmd int // GenCmdBuilder parse cli input and try to build a GenCmd instance type GenCmdBuilder int const gGenCmdString = "gen" var gGenCmdSingleton = new(GenCmd) func (me *GenCmd) String() string { return gGenCmdString } func (me *GenCmd) Apply(c command.ICmdContext) error { panic("implements me") } func (me *GenCmdBuilder) Build(line string) (error, command.ICmd) { if line != gGenCmdString { return nil, nil } return nil, gGenCmdSingleton }
“事件驅動”的邏輯編排模型
package model // IEventDrivenModel defines an event driven model for code arrangement type IEventDrivenModel interface { Hook(e string, handleFunc TEventHandleFunc) Fire(e string, args ...interface{}) FireAsync(e string, args ...interface{}) } type TEventHandleFunc func(e string, args ...interface{}) type TEventDrivenModel struct { items map[string][]TEventHandleFunc } func (me *TEventDrivenModel) Hook(e string, handler TEventHandleFunc) { if me.items == nil { me.items = make(map[string][]TEventHandleFunc) } arr, ok := me.items[e] if ok { me.items[e] = append(arr, handler) } else { me.items[e] = []TEventHandleFunc{handler} } } func (me *TEventDrivenModel) Fire(e string, args ...interface{}) { if handlers, ok := me.items[e]; ok { for _, it := range handlers { it(e, args...) } } } func (me *TEventDrivenModel) FireAsync(e string, args ...interface{}) { go me.Fire(e, args...) }
到此,關于“怎么使用golang仿spring ioc/aop基于注解的靜態代碼增強器/生成器”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。