Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to scan with golang imitating spring ioc/aop

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to scan with golang imitating spring ioc/aop". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to scan with golang imitating spring ioc/aop".

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 5)

Q: what are you doing?

A: scan the go code file

Q: be more specific?

A: recursively scan all go code in the specified directory and extract structure, field, method and annotation information

Q: what's the idea?

A:

Parse comments per method

Parse comments per field

Parse comments per struct

Read go file > parse package > parse import > parse struct list > resolve field list > parse method list

Q: how to analyze it?

A: read text > clear comments > progressive regular scan

Q: clear comments?

A: because anything can be written in a comment, clearing the comment can reduce the possibility of parsing errors

Q: how to clear it?

A:

LINE_COMMENT ='/ /'[^\ r\ n] *

PARA_COMMENT ='/'. '* /'

Q: scan package?

A:PACKAGE = 'package' SPACE+\ w +

Q: scan import?

A:

SINGLE_IMPORT = 'import' SPACE+ PACKAGE_LITERAL

PACKAGE_LITERAL = (PACKAGE_ALIAS SPACE+)? "\ w + (/\ w +) *"

MULTI_IMPORT = 'import' SPACE+' (\ r\ n' (SPACE* PACKAGE_LITERAL) +'\ r\ n)'

Q: scan struct?

A:

STRUCT_START = 'type' SPACE+\ w + SPACE+' struct' SPACE+'{'

STRUCT_END ='}'

Q: scan field?

A:

STRUCT_FIELD = SPACE+\ w + FIELD_TYPE

FIELD_TYPE = BASIC_TYPE | CUSTOM_TYPE | POINTER_TYPE | ARRAY_TYPE | MAP_TYPE

BASIC_TYPE = 'int' |' string' | 'bool' |' uint' | 'rune' |' byte' | 'int8' |' uint8' | 'int16' |' uint16' | 'int32' |' uint32' | 'int64' |' uint64' | 'float32' |' float64'

CUSTOM_TYPE = (PACKAGE_ALIAS.)?\ w +

POINTER_TYPE ='* 'FIELD_TYPE

ARRAY_TYPE ='[] 'FIELD_TYPE

MAP_TYPE = 'map [' FIELD_TYPE'] 'FIELD_TYPE

Q: scan method?

A:

METHOD_START = 'func' SPACE+ RECEIVER METHOD_ARGS METHOD_RETURNS?' {'

RECEIVER ='('RECEIVER_VAR *? RECEIVER_TYPE')'

RECEIVER_VAR =\ w +

RECEIVER_TYPE =\ w +

METHOD_ARGS ='('SPACE* METHOD_ARG? (SPACE*', 'SPACE* METHOD_ARG) *'

METHOD_ARG =\ w + SPACE* FIELD_TYPE

RETURNS = SINGLE_RETURN | MULTI_RETURN

SINGLE_RETURN = FIELD_TYPE

METHOD_RETURNS ='('SPACE* RETURN_ARG? (SPACE*', 'SPACE* RETURN_ARG) *'

RETURN_ARG = NAMED_RETURN_ARG | UNNAMED_RETURN_ARG

NAMED_RETURN_ARG =\ w + SPACE* FIELD_TYPE

UNNAMED_RETURN_ARG = FIELD_TYPE

Q: extract comments?

A:

ANNOTATION ='@'\ w + (SPACE+ ANNOTATION_ATTRIBUTE+)?

ANNOTATION_ATTRIBUTE = AA_KEY'= 'AA_VALUE

AA_KEY =\ w +

AA_VALUE = [^\ w,] +

Extract consecutive comment lines before the specified struct/field/method

Extract comments that begin with'/ / @'as comment lines

Extract specific notes

Thank you for your reading, the above is the content of "how to scan with golang imitating spring ioc/aop". After the study of this article, I believe you have a deeper understanding of how to scan with golang imitating spring ioc/aop, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report