這篇文章主要介紹“怎么使用golang仿spring ioc/aop基于注解的靜態(tài)代碼增強(qiáng)器/生成器”,在日常操作中,相信很多人在怎么使用golang仿spring ioc/aop基于注解的靜態(tài)代碼增強(qiáng)器/生成器問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”怎么使用golang仿spring ioc/aop基于注解的靜態(tài)代碼增強(qiáng)器/生成器”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
我們一直強(qiáng)調(diào)成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)對(duì)于企業(yè)的重要性,如果您也覺得重要,那么就需要我們慎重對(duì)待,選擇一個(gè)安全靠譜的網(wǎng)站建設(shè)公司,企業(yè)網(wǎng)站我們建議是要么不做,要么就做好,讓網(wǎng)站能真正成為企業(yè)發(fā)展過程中的有力推手。專業(yè)的建站公司不一定是大公司,創(chuàng)新互聯(lián)建站作為專業(yè)的網(wǎng)絡(luò)公司選擇我們就是放心。
手?jǐn)]golang spring ioc/aop 之2
Spring基于J2EE技術(shù)實(shí)現(xiàn)了一套輕量級(jí)的 Java Web Service系統(tǒng)應(yīng)用框架。 它有很多優(yōu)秀的特性,很多公司都選擇把 Spring作為產(chǎn)品或項(xiàng)目的基礎(chǔ)開發(fā)架構(gòu)。 Spring的特性包括輕量、控制反轉(zhuǎn) (Inversion of Control, IoC)、 面向容器、面向切面(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
構(gòu)思app的運(yùn)行模式:
本地standlone模式運(yùn)行
提供基于cli的命令行實(shí)時(shí)交互
生成旁路代碼:只掃描源代碼,不修改源代碼,增強(qiáng)后的代碼加統(tǒng)一后綴
設(shè)計(jì)cli交互指令集:
config save:保存配置
config saveas <name>:另存配置
watch add <dir>:添加代碼掃描目錄
watch del <dir>:移除代碼掃描目錄
watch list:顯示當(dāng)前掃描的代碼目錄集合
gen:生成增強(qiáng)代碼,也就是掃描所有注解,并生成增強(qiáng)類和增強(qiáng)方法
config/IConfiguration:配置接口
command/ICmd:指令接口
command/ICmdBuilder:指令構(gòu)建器接口
command/ICmdContext:指令執(zhí)行上下文接口
config_cmd/SaveCmd: 保存配置
config_cmd/SaveASCmd: 另存配置
watch_cmd/AddCmd: 添加監(jiān)視
watch_cmd/DelCmd: 移除監(jiān)視
watch_cmd/ListCmd: 顯示已監(jiān)視目錄的列表
gen_cmd/GenCmd: 生成增強(qiáng)類和增強(qiáng)方法
model/IEventDrivenModel:“事件驅(qū)動(dòng)”的邏輯編排模型
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 }
指令構(gòu)建器接口
package command // ICmdBuilder parse input string and create an ICmd instance type ICmdBuilder interface { Build(string) (error, ICmd) }
指令執(zhí)行上下文接口
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 } }
添加監(jiān)視
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 } }
移除監(jiān)視
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 } }
顯示已監(jiān)視目錄的列表
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 }
生成增強(qiáng)類和增強(qiáng)方法
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 }
“事件驅(qū)動(dòng)”的邏輯編排模型
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...) }
到此,關(guān)于“怎么使用golang仿spring ioc/aop基于注解的靜態(tài)代碼增強(qiáng)器/生成器”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
分享標(biāo)題:怎么使用golang仿springioc/aop基于注解的靜態(tài)代碼增強(qiáng)器/生成器
文章位置:http://chinadenli.net/article42/gicghc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司、定制開發(fā)、Google、微信公眾號(hào)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)