chain

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package chain 提供 AOP 拦截器链实现,用于 enhance 框架。

该模块用于管理多个 Advice 的执行链,支持拦截器链的构建和执行。 参考 Spring AOP 的拦截器链设计。

架构设计

  • InterceptorChain: 拦截器链接口,定义链式执行逻辑
  • DefaultInterceptorChain: 默认拦截器链实现
  • MethodInvocation: 方法调用接口,表示当前方法调用上下文

核心功能

  • 链式执行: 按顺序执行多个拦截器
  • 拦截器管理: 支持添加、删除、插入拦截器
  • 方法调用: 支持方法调用的拦截和增强
  • 异常处理: 支持拦截器链中的异常传播

使用方式

创建拦截器链:

chain := chain.NewInterceptorChain()
chain.AddInterceptor(loggingInterceptor)
chain.AddInterceptor(authInterceptor)
chain.AddInterceptor(transactionInterceptor)

执行方法调用:

result, err := chain.Proceed(methodInvocation)

执行顺序

拦截器按添加顺序依次执行,支持 Around 类型的拦截器:

  • Before: 在目标方法执行前调用
  • After: 在目标方法执行后调用
  • Around: 包裹目标方法,可在前后执行逻辑

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdviceChain

type AdviceChain struct {
	// contains filtered or unexported fields
}

AdviceChain 通知链

func NewAdviceChain

func NewAdviceChain(advisors []Advisor) *AdviceChain

NewAdviceChain 创建通知链

func (*AdviceChain) CreateInvocation

func (c *AdviceChain) CreateInvocation(target any, method reflect.Method, args []reflect.Value) *MethodInvocation

CreateInvocation 创建方法调用

type Advisor

type Advisor struct {
	Pointcut aop.PointCut
	Advice   aop.Advice
}

Advisor 顾问

func (*Advisor) Matches

func (a *Advisor) Matches(method reflect.Method) bool

Matches 检查方法是否匹配

type MethodInvocation

type MethodInvocation struct {
	TargetObj any
	MethodObj reflect.Method
	ArgsList  []reflect.Value
	Chain     *AdviceChain
	Index     int
}

MethodInvocation 方法调用实现

func (*MethodInvocation) Args

func (m *MethodInvocation) Args() []any

Args 实现 aop.JoinPoint.Args

func (*MethodInvocation) Context

func (m *MethodInvocation) Context() context.Context

Context 实现 aop.JoinPoint.Context

func (*MethodInvocation) Method

func (m *MethodInvocation) Method() any

Method 实现 aop.JoinPoint.Method

func (*MethodInvocation) Proceed

func (m *MethodInvocation) Proceed(args ...any) any

Proceed 实现 aop.Invocation.Proceed

func (*MethodInvocation) SetContext

func (m *MethodInvocation) SetContext(ctx context.Context)

SetContext 实现 aop.Invocation.SetContext

func (*MethodInvocation) Signature

func (m *MethodInvocation) Signature() aop.MethodSignature

Signature 实现 aop.JoinPoint.Signature

func (*MethodInvocation) Target

func (m *MethodInvocation) Target() any

Target 实现 aop.JoinPoint.Target

func (*MethodInvocation) This

func (m *MethodInvocation) This() any

This 实现 aop.JoinPoint.This

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL