Documentation
¶
Overview ¶
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持,用于 enhance 框架。
该模块提供完整的 AOP 实现,包括通知、切点、顾问器、动态代理等功能。 支持静态代理代码生成,提升运行时性能。
架构设计 ¶
- Advice: 通知接口(Before/After/AfterReturning/AfterThrowing/Around)
- PointCut: 切点定义,支持方法名、包名、注解匹配
- Advisor: 顾问器,组合切点和通知
- Proxy: 动态代理生成
- Aspect: 切面定义和管理
- 代码生成: 支持静态代理代码生成
使用方式 ¶
直接使用:
// 创建切面
aspect := aop.NewAspect("loggingAspect").
AddAdvice(aop.BeforeAdvice(func(ctx aop.JoinPoint) {
fmt.Println("Before:", ctx.MethodName())
}))
// 创建代理
proxy := aop.NewProxy(target, aspect)
代码生成 ¶
使用 go generate 生成静态代理代码:
//go:generate go run github.com/xudefa/enhance/cmd/goaop
支持的匹配模式 ¶
- 方法名匹配:支持通配符(*)
- 包名匹配:支持包路径前缀匹配
- 注解匹配:基于方法注解进行匹配
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。
Package aop 提供面向切面编程(AOP)支持。 ProxyFactory 结构体定义在 doc.go 中,此处为实现。
Package aop 提供面向切面编程(AOP)支持。 InterfaceProxyWrapper 结构体定义在 doc.go 中,此处为实现。
Package aop 提供面向切面编程(AOP)支持。
Index ¶
- Variables
- func AfterDecorator(f func(args ...any) []any, after func(results []any, args ...any)) func(args ...any) []any
- func AroundDecorator(f func(args ...any) []any, around AroundFunc) func(args ...any) []any
- func AutoRegister(beanID string) error
- func AutoRegisterAll() error
- func AutoScan() error
- func BeforeDecorator(f func(args ...any) []any, before func(args ...any)) func(args ...any) []any
- func CreateProxy(beanID string, target any) any
- func ExecuteChain(jp *MethodInvocation, aspects []*AspectMeta) any
- func GetGeneratedProxy(beanID string) (reflect.Type, bool)
- func GetGlobalAopMetrics() map[string]any
- func GetProxyWithAutoMode(beanID string, target any) any
- func HasGeneratedProxy(beanID string) bool
- func InitializeAop()
- func IsReflectiveProxy(obj any) bool
- func ParseAspectTarget(target string) (structName, methodName string, err error)
- func RegisterAopBeanToGlobal(beanID string, beanType reflect.Type, target any) error
- func RegisterAspectToGlobal(aspect *AspectMeta)
- func RegisterAspectToGlobalContainer(aspect *AspectMeta)
- func RegisterGeneratedProxy(beanID string, proxyType reflect.Type)
- func ResetGlobalAopMetrics()
- func ScanAopBeans(basePath string) error
- func SetDefaultChainExecutor(executor ChainExecutor)
- func SortAspectsByOrder(aspects []*AspectMeta)
- type Advice
- type AdviceType
- type Advisor
- type AopBeanDefinition
- func (d *AopBeanDefinition) WithAopEnabled(enabled bool) *AopBeanDefinition
- func (d *AopBeanDefinition) WithAspects(aspects ...*AspectMeta) *AopBeanDefinition
- func (d *AopBeanDefinition) WithProxyMode(mode AopMode) *AopBeanDefinition
- func (d *AopBeanDefinition) WithProxyType(proxyType reflect.Type) *AopBeanDefinition
- type AopBeanFactory
- type AopBeanPostProcessor
- type AopBeanScanner
- type AopConfig
- type AopContainer
- func (c *AopContainer) DisableAop()
- func (c *AopContainer) EnableAop()
- func (c *AopContainer) GetAspects() []*AspectMeta
- func (c *AopContainer) GetFactory() *AopBeanFactory
- func (c *AopContainer) GetIntegration() *AopIntegration
- func (c *AopContainer) GetProcessor() *AopBeanPostProcessor
- func (c *AopContainer) IsAopEnabled() bool
- func (c *AopContainer) RegisterAopBean(beanDef *AopBeanDefinition, target any) error
- func (c *AopContainer) RegisterAopBeanWithAspects(beanID string, beanType reflect.Type, target any, aspects ...*AspectMeta) error
- func (c *AopContainer) RegisterAopBeanWithID(beanID string, beanType reflect.Type, target any) error
- func (c *AopContainer) RegisterAspect(aspect *AspectMeta)
- func (c *AopContainer) RegisterAspects(aspects ...*AspectMeta)
- type AopContainerBuilder
- func (b *AopContainerBuilder) Build() (*AopContainer, error)
- func (b *AopContainerBuilder) BuildOrPanic() *AopContainer
- func (b *AopContainerBuilder) WithAopMode(mode AopMode) *AopContainerBuilder
- func (b *AopContainerBuilder) WithAspect(aspect *AspectMeta) *AopContainerBuilder
- func (b *AopContainerBuilder) WithAspects(aspects ...*AspectMeta) *AopContainerBuilder
- func (b *AopContainerBuilder) WithBaseContainer(container core.Container) *AopContainerBuilder
- func (b *AopContainerBuilder) WithBean(beanDef *AopBeanDefinition) *AopContainerBuilder
- func (b *AopContainerBuilder) WithBeanWithAspects(beanID string, beanType reflect.Type, target any, aspects ...*AspectMeta) *AopContainerBuilder
- func (b *AopContainerBuilder) WithBeanWithID(beanID string, beanType reflect.Type, target any) *AopContainerBuilder
- func (b *AopContainerBuilder) WithConfig(config *AopConfig) *AopContainerBuilder
- type AopIntegration
- func (i *AopIntegration) CreateProxy(beanID string, target any) any
- func (i *AopIntegration) GetAspects() []*AspectMeta
- func (i *AopIntegration) GetManager() *AopManager
- func (i *AopIntegration) GetMetadataExtractor() *AspectMetadataExtractor
- func (i *AopIntegration) GetProxyFactory() *GeneratedProxyFactory
- func (i *AopIntegration) GetScannedProxy(typeName string) (string, bool)
- func (i *AopIntegration) RegisterAspect(aspect *AspectMeta)
- func (i *AopIntegration) RegisterAspects(aspects ...*AspectMeta)
- func (i *AopIntegration) RegisterProxyType(typeName string, filePath string)
- type AopManager
- type AopMetrics
- type AopMode
- type AopRegistry
- func (r *AopRegistry) GetAspects() []*AspectMeta
- func (r *AopRegistry) GetWeaver(beanID string) (Weaver, bool)
- func (r *AopRegistry) MatchAspectsForType(t reflect.Type) []*AspectMeta
- func (r *AopRegistry) RegisterAspect(aspect *AspectMeta)
- func (r *AopRegistry) RegisterWeaver(beanID string, weaver Weaver)
- func (r *AopRegistry) WeaveIfNeeded(beanID string, target any) any
- type AroundFunc
- type AspectBuilder
- func (b *AspectBuilder) Advice(advice Advice) *AspectBuilder
- func (b *AspectBuilder) After(fn func(JoinPoint)) *AspectBuilder
- func (b *AspectBuilder) Around(fn func(JoinPoint, ProceedFunc) any) *AspectBuilder
- func (b *AspectBuilder) Before(fn func(JoinPoint)) *AspectBuilder
- func (b *AspectBuilder) Build() *AspectMeta
- func (b *AspectBuilder) BuildAndRegister() *AspectMeta
- func (b *AspectBuilder) Instance(instance any) *AspectBuilder
- func (b *AspectBuilder) MatchAll() *AspectBuilder
- func (b *AspectBuilder) MatchByName(name string) *AspectBuilder
- func (b *AspectBuilder) MatchByRegex(pattern string) *AspectBuilder
- func (b *AspectBuilder) MatchInterface(iface any) *AspectBuilder
- func (b *AspectBuilder) Order(order int) *AspectBuilder
- func (b *AspectBuilder) PointCut(pointCut PointCut) *AspectBuilder
- type AspectMeta
- func CreateAfterAspect(methodName string, fn func(JoinPoint), order int) *AspectMeta
- func CreateAroundAspect(methodName string, fn func(JoinPoint, ProceedFunc) any, order int) *AspectMeta
- func CreateAspect(pointCut PointCut, advice Advice, order int) *AspectMeta
- func CreateAspectFromTarget(target string, advice Advice, order int) (*AspectMeta, error)
- func CreateBeforeAspect(methodName string, fn func(JoinPoint), order int) *AspectMeta
- func GetGlobalAspects() []*AspectMeta
- type AspectMetadataExtractor
- type BuildTagChecker
- type ChainExecutor
- type ChainExecutorOption
- type ChainStats
- type ClassMatcher
- type GeneratedProxyFactory
- type GeneratedProxyRegistry
- func (r *GeneratedProxyRegistry) Clear()
- func (r *GeneratedProxyRegistry) Get(beanID string) (reflect.Type, bool)
- func (r *GeneratedProxyRegistry) Has(beanID string) bool
- func (r *GeneratedProxyRegistry) List() []string
- func (r *GeneratedProxyRegistry) Register(beanID string, proxyType reflect.Type)
- type Interceptor
- type InterfaceProxyWrapper
- func (w *InterfaceProxyWrapper) GetAdvisors() []*AspectMeta
- func (w *InterfaceProxyWrapper) GetTarget() any
- func (w *InterfaceProxyWrapper) Invoke(methodName string, args ...any) (any, error)
- func (w *InterfaceProxyWrapper) InvokeContext(ctx context.Context, methodName string, args ...any) (any, error)
- func (w *InterfaceProxyWrapper) SetExecutor(executor ChainExecutor)
- type InvalidTargetFormatError
- type Invocation
- type JoinPoint
- type MethodInvocation
- func (m *MethodInvocation) Args() []any
- func (m *MethodInvocation) Context() context.Context
- func (m *MethodInvocation) Method() any
- func (m *MethodInvocation) Proceed(args ...any) any
- func (m *MethodInvocation) SetContext(ctx context.Context)
- func (m *MethodInvocation) SetProceed(p ProceedFunc)
- func (m *MethodInvocation) Signature() MethodSignature
- func (m *MethodInvocation) Target() any
- func (m *MethodInvocation) This() any
- type MethodMatcher
- type MethodSignature
- type PanicInfo
- type PointCut
- func Compose(pointcuts ...PointCut) PointCut
- func ComposeOr(pointcuts ...PointCut) PointCut
- func MatchAll() PointCut
- func MatchByAnnotation(annotationType reflect.Type) PointCut
- func MatchByClassName(className string) PointCut
- func MatchByMethodSignature(methodName string, paramTypes ...reflect.Type) PointCut
- func MatchByName(name string) PointCut
- func MatchByNamePrefix(prefix string) PointCut
- func MatchByPackage(packagePath string) PointCut
- func MatchByRegex(pattern string) PointCut
- func MatchByReturnType(returnType reflect.Type) PointCut
- func MatchClass(matcher ClassMatcher) PointCut
- func MatchClassMethod(classMatcher ClassMatcher, methodMatcher MethodMatcher) PointCut
- func MatchInterface(y any) PointCut
- func MatchMethod(matcher MethodMatcher) PointCut
- type PointCutFunc
- type PointCutWithClass
- type ProceedFunc
- type ProxyFactory
- type ReflectiveAopProxy
- func (p *ReflectiveAopProxy) Call(methodName string, args ...any) (any, error)
- func (p *ReflectiveAopProxy) CallContext(ctx context.Context, methodName string, args ...any) (any, error)
- func (p *ReflectiveAopProxy) MustCall(methodName string, args ...any) any
- func (p *ReflectiveAopProxy) SetExecutor(executor ChainExecutor)
- func (p *ReflectiveAopProxy) Target() any
- type Weaver
Constants ¶
This section is empty.
Variables ¶
var GlobalAopBeanFactory = NewAopBeanFactory(nil)
GlobalAopBeanFactory 全局AOP Bean工厂
var GlobalAopBeanPostProcessor = NewAopBeanPostProcessor(nil)
GlobalAopBeanPostProcessor 全局AOP Bean后置处理器
var GlobalAopBeanScanner = NewAopBeanScanner(nil)
GlobalAopBeanScanner 全局AOP Bean扫描器
var GlobalAopIntegration = NewAopIntegration(nil)
GlobalAopIntegration 全局AOP集成器
var GlobalAopManager = &AopManager{ config: DefaultAopConfig(), aspects: make([]*AspectMeta, 0), }
GlobalAopManager 全局AOP管理器。
var GlobalAopMetrics = NewAopMetrics()
GlobalAopMetrics 全局AOP指标
var GlobalBuildTagChecker = NewBuildTagChecker()
GlobalBuildTagChecker 全局构建标签检查器
var GlobalGeneratedProxyRegistry = NewGeneratedProxyRegistry()
GlobalGeneratedProxyRegistry 全局代码生成代理注册表
Functions ¶
func AfterDecorator ¶
func AfterDecorator(f func(args ...any) []any, after func(results []any, args ...any)) func(args ...any) []any
AfterDecorator 创建后置装饰器,在调用原函数后执行 after 钩子。
参数:
- f: 被装饰的函数
- after: 后置钩子函数,接收 f 的所有参数和所有返回值
返回值:
- func(args ...any) []any: 装饰后的函数
func AroundDecorator ¶
func AroundDecorator(f func(args ...any) []any, around AroundFunc) func(args ...any) []any
AroundDecorator 创建环绕装饰器,由 around 函数完全控制原函数的执行流程。
参数:
- f: 被装饰的函数
- around: 环绕函数,接收原始函数 f 和原始参数,决定是否调用 f 以及在调用前后插入逻辑
返回值:
- func(args ...any) []any: 装饰后的函数
func BeforeDecorator ¶
BeforeDecorator 创建前置装饰器,在调用原函数前执行 before 钩子。
参数:
- f: 被装饰的函数,签名为 func(args ...any) []any
- before: 前置钩子函数,接收 f 的所有参数
返回值:
- func(args ...any) []any: 装饰后的函数
func ExecuteChain ¶
func ExecuteChain(jp *MethodInvocation, aspects []*AspectMeta) any
ExecuteChain 执行通知链
为代码生成的代理类提供通知链执行功能。 按照切面的 Order 排序,通过默认 ChainExecutor 执行通知链。
参数:
- jp: 方法调用信息
- aspects: 切面元数据列表(指针类型)
返回值:
- any: 方法执行结果
func GetGeneratedProxy ¶
GetGeneratedProxy 获取代码生成的代理类型
func GetProxyWithAutoMode ¶
GetProxyWithAutoMode 使用自动模式获取代理
func IsReflectiveProxy ¶
IsReflectiveProxy 检查对象是否为 ReflectiveAopProxy
func RegisterAopBeanToGlobal ¶
RegisterAopBeanToGlobal 注册AOP Bean到全局容器
func RegisterAspectToGlobal ¶
func RegisterAspectToGlobal(aspect *AspectMeta)
RegisterAspectToGlobal 注册切面到全局集成器
func RegisterAspectToGlobalContainer ¶
func RegisterAspectToGlobalContainer(aspect *AspectMeta)
RegisterAspectToGlobalContainer 注册切面到全局容器
func RegisterGeneratedProxy ¶
RegisterGeneratedProxy 注册代码生成的代理
func SetDefaultChainExecutor ¶
func SetDefaultChainExecutor(executor ChainExecutor)
SetDefaultChainExecutor 设置默认通知链执行器
传入 nil 会被忽略。设置后,所有使用默认执行器的代码(包括 ExecuteChain 和 ReflectiveAopProxy) 都会使用新的执行器。并发安全。
func SortAspectsByOrder ¶
func SortAspectsByOrder(aspects []*AspectMeta)
SortAspectsByOrder 按 Order 升序排序切面列表。
使用标准库 slices.SortFunc 按 Order 升序排列切面,Order 小的在前。 当多个切点匹配同一方法时,通过 Order 控制通知的执行顺序。
Types ¶
type Advice ¶
type Advice interface {
// Type 返回通知类型。
Type() AdviceType
// Apply 应用通知。
//
// 执行通知的增强逻辑。对于 Around 通知,需要通过 proceed 参数
// 调用目标方法或下一个通知。
//
// 参数:
// - jp: 连接点,包含方法调用的上下文信息(方法名、参数、目标对象等)
// - proceed: 继续执行函数,调用它会执行目标方法或下一个通知
//
// 返回值:
// - any: 通知的返回值。对于 Around 通知,通常返回目标方法的执行结果;
// 对于其他通知类型,返回值通常被忽略。
Apply(jp JoinPoint, proceed ProceedFunc) any
}
Advice 通知接口。
定义 AOP 通知的核心行为。通知是对目标方法的增强逻辑, 在方法执行的生命周期中的特定时机介入。
推荐使用 Before/After/Around 等函数式 API 替代直接实现此接口, 以获得更简洁的 Go 惯用法体验。此接口主要用于框架内部实现。
func After ¶
After 创建后置通知
在目标方法执行之后执行增强逻辑,无论方法是否抛出异常。 后置通知在异常通知之前执行。
参数:
- fn: 后置通知函数,接收 JoinPoint 参数
返回值:
- Advice: 后置通知实例
示例:
aop.After(func(jp aop.JoinPoint) {
fmt.Println("方法执行后:", jp.Method().Name)
})
func AfterReturning ¶
AfterReturning 创建返回通知
在目标方法正常返回后执行增强逻辑。 可以访问方法的返回值,适用于结果缓存、响应增强等场景。 如果方法抛出异常,此通知不会执行。
参数:
- fn: 返回通知函数,接收 JoinPoint 和方法返回值
返回值:
- Advice: 返回通知实例
示例:
aop.AfterReturning(func(jp aop.JoinPoint, result any) {
fmt.Println("方法返回:", result)
})
func AfterThrowing ¶
AfterThrowing 创建异常通知
在目标方法抛出异常后执行增强逻辑。 可以访问错误对象,适用于错误日志、异常转换、告警通知等场景。 如果方法正常返回,此通知不会执行。
参数:
- fn: 异常通知函数,接收 JoinPoint 和错误对象
返回值:
- Advice: 异常通知实例
示例:
aop.AfterThrowing(func(jp aop.JoinPoint, err error) {
fmt.Println("方法异常:", err)
})
func Around ¶
func Around(fn func(JoinPoint, ProceedFunc) any) Advice
Around 创建环绕通知
最强大的通知类型,完全控制目标方法的执行。 可以决定是否执行目标方法、何时执行、执行几次,甚至可以替换返回值。
重要:Around 通知必须调用 proceed 函数使调用链继续,否则目标方法不会执行。
参数:
- fn: 环绕通知函数,接收 JoinPoint 和 ProceedFunc
返回值:
- Advice: 环绕通知实例
示例:
aop.Around(func(jp aop.JoinPoint, proceed aop.ProceedFunc) any {
fmt.Println("方法执行前:", jp.Method().Name)
result := proceed()
fmt.Println("方法执行后:", result)
return result
})
type AdviceType ¶
type AdviceType string
AdviceType 通知类型枚举。
定义 AOP 框架中的五种标准通知类型,对应 Spring AOP 的通知模型。 每种通知类型决定了增强逻辑在目标方法执行生命周期中的介入时机。
const ( // AdviceBefore 前置通知。 // // 在目标方法执行之前调用增强逻辑。 // 适用于日志记录、参数校验、权限检查等场景。 // 前置通知无法阻止目标方法的执行。 AdviceBefore AdviceType = "before" // AdviceAfter 后置通知。 // // 在目标方法执行之后调用,无论方法是否抛出异常。 // 适用于资源清理、状态重置等场景。 // 注意:后置通知在异常通知之前执行。 AdviceAfter AdviceType = "after" // AdviceAfterReturning 返回通知。 // // 在目标方法正常返回后调用(未抛出异常)。 // 可以访问方法的返回值,适用于结果缓存、响应增强等场景。 // 如果方法抛出异常,此通知不会执行。 AdviceAfterReturning AdviceType = "after_returning" // AdviceAfterThrowing 异常通知。 // // 在目标方法抛出异常后调用。 // 可以访问错误对象,适用于错误日志、异常转换、告警通知等场景。 // 如果方法正常返回,此通知不会执行。 AdviceAfterThrowing AdviceType = "after_throwing" // AdviceAround 环绕通知。 // // 最强大的通知类型,完全控制目标方法的执行。 // 可以决定是否执行目标方法、何时执行、执行几次,甚至可以替换返回值。 // 必须调用 proceed 函数使调用链继续,否则目标方法不会执行。 // 适用于事务管理、性能监控、重试逻辑等场景。 AdviceAround AdviceType = "around" )
type Advisor ¶
type Advisor interface {
// GetPointCut 获取切点。
GetPointCut() PointCut
// GetAdvice 获取通知。
GetAdvice() Advice
// Order 获取执行顺序。
Order() int
}
Advisor 顾问器接口。
顾问是 AOP 中的基本单元,包含一个切点和一个通知。 类似于 Spring 中的 Advisor 概念。
使用场景:
- 细粒度的切面控制,为每个通知指定独立的切点和执行顺序
- 精确控制通知执行顺序和匹配规则
func NewAdvisor ¶
NewAdvisor 创建顾问
参数:
- pointCut: 切点
- advice: 通知
- order: 可选的执行顺序,默认 0
返回值:
- Advisor: 顾问实例
示例:
advisor := aop.NewAdvisor(
aop.MatchByName("DoSomething"),
aop.Before(func(jp aop.JoinPoint) { fmt.Println("before") }),
1, // order
)
type AopBeanDefinition ¶
type AopBeanDefinition struct {
BeanID string
EnableAop bool
ProxyMode AopMode
TargetType reflect.Type
ProxyType reflect.Type
Aspects []*AspectMeta
}
AopBeanDefinition AOP Bean定义
扩展标准Bean定义,添加AOP相关配置
func NewAopBeanDefinition ¶
func NewAopBeanDefinition(beanID string, beanType reflect.Type) *AopBeanDefinition
NewAopBeanDefinition 创建AOP Bean定义
func (*AopBeanDefinition) WithAopEnabled ¶
func (d *AopBeanDefinition) WithAopEnabled(enabled bool) *AopBeanDefinition
WithAopEnabled 设置启用AOP
func (*AopBeanDefinition) WithAspects ¶
func (d *AopBeanDefinition) WithAspects(aspects ...*AspectMeta) *AopBeanDefinition
WithAspects 设置切面
func (*AopBeanDefinition) WithProxyMode ¶
func (d *AopBeanDefinition) WithProxyMode(mode AopMode) *AopBeanDefinition
WithProxyMode 设置代理模式
func (*AopBeanDefinition) WithProxyType ¶
func (d *AopBeanDefinition) WithProxyType(proxyType reflect.Type) *AopBeanDefinition
WithProxyType 设置代理类型
type AopBeanFactory ¶
type AopBeanFactory struct {
// contains filtered or unexported fields
}
AopBeanFactory AOP Bean工厂
创建AOP代理Bean
func NewAopBeanFactory ¶
func NewAopBeanFactory(integration *AopIntegration) *AopBeanFactory
NewAopBeanFactory 创建AOP Bean工厂
func (*AopBeanFactory) CreateBean ¶
func (f *AopBeanFactory) CreateBean(beanID string, beanDef *AopBeanDefinition, target any) (any, error)
CreateBean 创建Bean
func (*AopBeanFactory) GetProcessor ¶
func (f *AopBeanFactory) GetProcessor() *AopBeanPostProcessor
GetProcessor 获取后置处理器
func (*AopBeanFactory) RegisterBean ¶
func (f *AopBeanFactory) RegisterBean(container core.Container, beanDef *AopBeanDefinition, target any) error
RegisterBean 注册Bean到容器
type AopBeanPostProcessor ¶
type AopBeanPostProcessor struct {
// contains filtered or unexported fields
}
AopBeanPostProcessor AOP Bean后置处理器
在Bean创建后自动应用AOP代理
func NewAopBeanPostProcessor ¶
func NewAopBeanPostProcessor(integration *AopIntegration) *AopBeanPostProcessor
NewAopBeanPostProcessor 创建AOP Bean后置处理器
func (*AopBeanPostProcessor) IsEnabled ¶
func (p *AopBeanPostProcessor) IsEnabled() bool
IsEnabled 检查是否启用
func (*AopBeanPostProcessor) ProcessBean ¶
func (p *AopBeanPostProcessor) ProcessBean(beanID string, bean any) any
ProcessBean 处理Bean
type AopBeanScanner ¶
type AopBeanScanner struct {
// contains filtered or unexported fields
}
AopBeanScanner AOP Bean扫描器
扫描并注册带有AOP注解的Bean
func NewAopBeanScanner ¶
func NewAopBeanScanner(container *AopContainer) *AopBeanScanner
NewAopBeanScanner 创建AOP Bean扫描器
func (*AopBeanScanner) GetContainer ¶
func (s *AopBeanScanner) GetContainer() *AopContainer
GetContainer 获取容器
type AopConfig ¶
AopConfig AOP 配置。
func ConfigureAopManager ¶
func ConfigureAopManager() *AopConfig
ConfigureAopManager 配置AOP管理器
根据构建标签自动配置最优的AOP模式
type AopContainer ¶
AopContainer AOP 容器。
集成 AOP 功能的 IoC 容器。
func CreateAopContainerWithConfig ¶
func CreateAopContainerWithConfig(config *AopConfig) *AopContainer
CreateAopContainerWithConfig 创建AOP容器(指定配置)
func NewAopContainer ¶
func NewAopContainer(baseContainer core.Container) *AopContainer
NewAopContainer 创建AOP容器。
func (*AopContainer) GetAspects ¶
func (c *AopContainer) GetAspects() []*AspectMeta
GetAspects 获取所有切面
func (*AopContainer) GetFactory ¶
func (c *AopContainer) GetFactory() *AopBeanFactory
GetFactory 获取Bean工厂
func (*AopContainer) GetIntegration ¶
func (c *AopContainer) GetIntegration() *AopIntegration
GetIntegration 获取AOP集成器
func (*AopContainer) GetProcessor ¶
func (c *AopContainer) GetProcessor() *AopBeanPostProcessor
GetProcessor 获取后置处理器
func (*AopContainer) IsAopEnabled ¶
func (c *AopContainer) IsAopEnabled() bool
IsAopEnabled 检查AOP是否启用
func (*AopContainer) RegisterAopBean ¶
func (c *AopContainer) RegisterAopBean(beanDef *AopBeanDefinition, target any) error
RegisterAopBean 注册AOP Bean
func (*AopContainer) RegisterAopBeanWithAspects ¶
func (c *AopContainer) RegisterAopBeanWithAspects(beanID string, beanType reflect.Type, target any, aspects ...*AspectMeta) error
RegisterAopBeanWithAspects 注册AOP Bean(带切面)
func (*AopContainer) RegisterAopBeanWithID ¶
func (c *AopContainer) RegisterAopBeanWithID(beanID string, beanType reflect.Type, target any) error
RegisterAopBeanWithID 注册AOP Bean(指定ID)
func (*AopContainer) RegisterAspect ¶
func (c *AopContainer) RegisterAspect(aspect *AspectMeta)
RegisterAspect 注册切面
func (*AopContainer) RegisterAspects ¶
func (c *AopContainer) RegisterAspects(aspects ...*AspectMeta)
RegisterAspects 批量注册切面
type AopContainerBuilder ¶
type AopContainerBuilder struct {
// contains filtered or unexported fields
}
AopContainerBuilder AOP容器构建器
提供流式API构建AOP容器
func NewAopContainerBuilder ¶
func NewAopContainerBuilder() *AopContainerBuilder
NewAopContainerBuilder 创建AOP容器构建器
func (*AopContainerBuilder) Build ¶
func (b *AopContainerBuilder) Build() (*AopContainer, error)
Build 构建AOP容器
func (*AopContainerBuilder) BuildOrPanic ¶
func (b *AopContainerBuilder) BuildOrPanic() *AopContainer
BuildOrPanic 构建AOP容器(失败时panic)
func (*AopContainerBuilder) WithAopMode ¶
func (b *AopContainerBuilder) WithAopMode(mode AopMode) *AopContainerBuilder
WithAopMode 设置AOP模式
func (*AopContainerBuilder) WithAspect ¶
func (b *AopContainerBuilder) WithAspect(aspect *AspectMeta) *AopContainerBuilder
WithAspect 添加切面
func (*AopContainerBuilder) WithAspects ¶
func (b *AopContainerBuilder) WithAspects(aspects ...*AspectMeta) *AopContainerBuilder
WithAspects 批量添加切面
func (*AopContainerBuilder) WithBaseContainer ¶
func (b *AopContainerBuilder) WithBaseContainer(container core.Container) *AopContainerBuilder
WithBaseContainer 设置基础容器
func (*AopContainerBuilder) WithBean ¶
func (b *AopContainerBuilder) WithBean(beanDef *AopBeanDefinition) *AopContainerBuilder
WithBean 添加Bean
func (*AopContainerBuilder) WithBeanWithAspects ¶
func (b *AopContainerBuilder) WithBeanWithAspects(beanID string, beanType reflect.Type, target any, aspects ...*AspectMeta) *AopContainerBuilder
WithBeanWithAspects 添加Bean(带切面)
func (*AopContainerBuilder) WithBeanWithID ¶
func (b *AopContainerBuilder) WithBeanWithID(beanID string, beanType reflect.Type, target any) *AopContainerBuilder
WithBeanWithID 添加Bean(指定ID)
func (*AopContainerBuilder) WithConfig ¶
func (b *AopContainerBuilder) WithConfig(config *AopConfig) *AopContainerBuilder
WithConfig 设置配置
type AopIntegration ¶
type AopIntegration struct {
// contains filtered or unexported fields
}
AopIntegration AOP集成器
提供代码生成和运行时AOP的统一集成
func NewAopIntegration ¶
func NewAopIntegration(config *AopConfig) *AopIntegration
NewAopIntegration 创建AOP集成器
func (*AopIntegration) CreateProxy ¶
func (i *AopIntegration) CreateProxy(beanID string, target any) any
CreateProxy 创建代理对象
func (*AopIntegration) GetAspects ¶
func (i *AopIntegration) GetAspects() []*AspectMeta
GetAspects 获取所有切面
func (*AopIntegration) GetManager ¶
func (i *AopIntegration) GetManager() *AopManager
GetManager 获取AOP管理器
func (*AopIntegration) GetMetadataExtractor ¶
func (i *AopIntegration) GetMetadataExtractor() *AspectMetadataExtractor
GetMetadataExtractor 获取元数据提取器
func (*AopIntegration) GetProxyFactory ¶
func (i *AopIntegration) GetProxyFactory() *GeneratedProxyFactory
GetProxyFactory 获取代理工厂
func (*AopIntegration) GetScannedProxy ¶
func (i *AopIntegration) GetScannedProxy(typeName string) (string, bool)
GetScannedProxy 获取扫描到的代理类型文件路径
func (*AopIntegration) RegisterAspect ¶
func (i *AopIntegration) RegisterAspect(aspect *AspectMeta)
RegisterAspect 注册切面
func (*AopIntegration) RegisterAspects ¶
func (i *AopIntegration) RegisterAspects(aspects ...*AspectMeta)
RegisterAspects 批量注册切面
func (*AopIntegration) RegisterProxyType ¶
func (i *AopIntegration) RegisterProxyType(typeName string, filePath string)
RegisterProxyType 注册代理类型(供扫描器使用)
type AopManager ¶
type AopManager struct {
// contains filtered or unexported fields
}
AopManager AOP 管理器。
func (*AopManager) MatchAspectsForType ¶
func (m *AopManager) MatchAspectsForType(beanType any) []*AspectMeta
MatchAspectsForType 匹配指定类型的切面
func (*AopManager) RegisterAspect ¶
func (m *AopManager) RegisterAspect(aspect *AspectMeta)
RegisterAspect 注册切面
func (*AopManager) RegisterAspects ¶
func (m *AopManager) RegisterAspects(aspects ...*AspectMeta)
RegisterAspects 批量注册切面
type AopMetrics ¶
type AopMetrics struct {
TotalProxies atomic.Int64
GeneratedProxies atomic.Int64
RuntimeProxies atomic.Int64
TotalAspects atomic.Int64
TotalInterceptions atomic.Int64
// contains filtered or unexported fields
}
AopMetrics AOP指标
收集AOP相关的性能指标 使用 atomic 操作优化高并发场景下的计数性能
func (*AopMetrics) RecordAspectRegistered ¶
func (m *AopMetrics) RecordAspectRegistered()
RecordAspectRegistered 记录切面注册
func (*AopMetrics) RecordInterception ¶
func (m *AopMetrics) RecordInterception(latency float64)
RecordInterception 记录拦截
func (*AopMetrics) RecordProxyCreated ¶
func (m *AopMetrics) RecordProxyCreated(isGenerated bool)
RecordProxyCreated 记录代理创建
type AopRegistry ¶
type AopRegistry struct {
// contains filtered or unexported fields
}
AopRegistry AOP 注册表
管理所有切面和织入器的注册中心。 用于在 IoC 容器中集成 AOP 功能。
func NewAopRegistry ¶
func NewAopRegistry() *AopRegistry
NewAopRegistry 创建 AOP 注册表
返回值:
- *AopRegistry: 注册表实例
func (*AopRegistry) GetWeaver ¶
func (r *AopRegistry) GetWeaver(beanID string) (Weaver, bool)
GetWeaver 获取织入器
func (*AopRegistry) MatchAspectsForType ¶
func (r *AopRegistry) MatchAspectsForType(t reflect.Type) []*AspectMeta
MatchAspectsForType 为类型匹配切面
根据类型匹配所有适用的切面,并按 Order 排序。
func (*AopRegistry) RegisterAspect ¶
func (r *AopRegistry) RegisterAspect(aspect *AspectMeta)
RegisterAspect 注册切面
func (*AopRegistry) RegisterWeaver ¶
func (r *AopRegistry) RegisterWeaver(beanID string, weaver Weaver)
RegisterWeaver 注册织入器
func (*AopRegistry) WeaveIfNeeded ¶
func (r *AopRegistry) WeaveIfNeeded(beanID string, target any) any
WeaveIfNeeded 按需织入
如果指定 beanID 有对应的织入器,则织入目标对象。
type AroundFunc ¶
AroundFunc 环绕函数类型,完全控制原函数的执行。
参数:
- originalFunc: 被装饰的原始函数,接受 []any 参数并返回 []any 结果
- args: 原始函数的参数
返回值:
- []any: 最终的结果,由 around 函数决定是否调用 originalFunc 以及如何处理结果
type AspectBuilder ¶
type AspectBuilder struct {
// contains filtered or unexported fields
}
AspectBuilder 切面构建器
提供流式API构建切面
func (*AspectBuilder) Advice ¶
func (b *AspectBuilder) Advice(advice Advice) *AspectBuilder
Advice 设置通知
func (*AspectBuilder) After ¶
func (b *AspectBuilder) After(fn func(JoinPoint)) *AspectBuilder
After 设置后置通知
func (*AspectBuilder) Around ¶
func (b *AspectBuilder) Around(fn func(JoinPoint, ProceedFunc) any) *AspectBuilder
Around 设置环绕通知
func (*AspectBuilder) Before ¶
func (b *AspectBuilder) Before(fn func(JoinPoint)) *AspectBuilder
Before 设置前置通知
func (*AspectBuilder) BuildAndRegister ¶
func (b *AspectBuilder) BuildAndRegister() *AspectMeta
BuildAndRegister 构建并注册切面
func (*AspectBuilder) Instance ¶
func (b *AspectBuilder) Instance(instance any) *AspectBuilder
Instance 设置切面实例
func (*AspectBuilder) MatchByName ¶
func (b *AspectBuilder) MatchByName(name string) *AspectBuilder
MatchByName 设置按名称匹配的切点
func (*AspectBuilder) MatchByRegex ¶
func (b *AspectBuilder) MatchByRegex(pattern string) *AspectBuilder
MatchByRegex 设置按正则匹配的切点
func (*AspectBuilder) MatchInterface ¶
func (b *AspectBuilder) MatchInterface(iface any) *AspectBuilder
MatchInterface 设置按接口匹配的切点
func (*AspectBuilder) PointCut ¶
func (b *AspectBuilder) PointCut(pointCut PointCut) *AspectBuilder
PointCut 设置切点
type AspectMeta ¶
type AspectMeta struct {
Instance any // 切面实例对象
PointCut PointCut // 切点定义,匹配目标方法
Advice Advice // 通知,包含增强逻辑
Order int // 执行顺序,数字越小越先执行
}
AspectMeta 切面元数据。
存储切面的实例、切点、通知和执行顺序。 切面是 AOP 的核心数据结构,定义了"在什么地方、做什么增强"。
func CreateAfterAspect ¶
func CreateAfterAspect(methodName string, fn func(JoinPoint), order int) *AspectMeta
CreateAfterAspect 创建后置切面的便捷函数
func CreateAroundAspect ¶
func CreateAroundAspect(methodName string, fn func(JoinPoint, ProceedFunc) any, order int) *AspectMeta
CreateAroundAspect 创建环绕切面的便捷函数
func CreateAspect ¶
func CreateAspect(pointCut PointCut, advice Advice, order int) *AspectMeta
CreateAspect 创建切面的便捷函数
func CreateAspectFromTarget ¶
func CreateAspectFromTarget(target string, advice Advice, order int) (*AspectMeta, error)
CreateAspectFromTarget 从目标字符串创建切面
func CreateBeforeAspect ¶
func CreateBeforeAspect(methodName string, fn func(JoinPoint), order int) *AspectMeta
CreateBeforeAspect 创建前置切面的便捷函数
type AspectMetadataExtractor ¶
type AspectMetadataExtractor struct{}
AspectMetadataExtractor 切面元数据提取器
从代码生成的代理中提取切面元数据
func NewAspectMetadataExtractor ¶
func NewAspectMetadataExtractor() *AspectMetadataExtractor
NewAspectMetadataExtractor 创建切面元数据提取器
func (*AspectMetadataExtractor) Extract ¶
func (e *AspectMetadataExtractor) Extract(proxyType reflect.Type) []*AspectMeta
Extract 从代理类型提取切面元数据
func (*AspectMetadataExtractor) ExtractFromBeanID ¶
func (e *AspectMetadataExtractor) ExtractFromBeanID(beanID string) []*AspectMeta
ExtractFromBeanID 从bean ID提取切面元数据
type BuildTagChecker ¶
type BuildTagChecker struct{}
BuildTagChecker 构建标签检查器
检查当前构建是否包含特定标签
func (*BuildTagChecker) GetOptimalMode ¶
func (c *BuildTagChecker) GetOptimalMode() AopMode
GetOptimalMode 获取最优模式
func (*BuildTagChecker) IsGeneratedMode ¶
func (c *BuildTagChecker) IsGeneratedMode() bool
IsGeneratedMode 检查是否为代码生成模式
func (*BuildTagChecker) IsRuntimeMode ¶
func (c *BuildTagChecker) IsRuntimeMode() bool
IsRuntimeMode 检查是否为运行时模式
type ChainExecutor ¶
type ChainExecutor interface {
// Execute 执行通知链。
//
// 参数:
// - inv: 调用信息
// - aspects: 切面元数据列表
// - targetFunc: 目标方法调用函数
//
// 返回值:
// - any: 方法执行结果
Execute(inv Invocation, aspects []*AspectMeta, targetFunc func(...any) any) any
}
ChainExecutor 通知链执行器接口。
定义通知链的执行策略。默认实现支持 panic 恢复、自定义拦截器和 context 传播。 可通过实现此接口自定义执行策略(如异步执行、限流等)。
func DefaultChainExecutor ¶
func DefaultChainExecutor() ChainExecutor
DefaultChainExecutor 获取默认通知链执行器
func NewChainExecutor ¶
func NewChainExecutor(opts ...ChainExecutorOption) ChainExecutor
NewChainExecutor 创建通知链执行器
默认启用 panic 恢复。可通过选项自定义行为。
示例:
executor := aop.NewChainExecutor(
aop.WithInterceptor(tracingInterceptor),
aop.WithInterceptor(metricsInterceptor),
)
aop.SetDefaultChainExecutor(executor)
type ChainExecutorOption ¶
type ChainExecutorOption func(*chainExecutorConfig)
ChainExecutorOption 执行器选项函数。
用于通过函数式选项模式配置 ChainExecutor。
func WithInterceptor ¶
func WithInterceptor(i Interceptor) ChainExecutorOption
WithInterceptor 添加自定义拦截器。
拦截器按添加顺序嵌套:第一个添加的拦截器在最外层。 拦截器可以修改调用信息、观察结果、处理 panic 等。
func WithRecovery ¶
func WithRecovery() ChainExecutorOption
WithRecovery 启用 panic 恢复。
启用后,目标方法的 panic 会被捕获, afterThrowing 通知会正常执行,然后重新抛出 panic。
type ChainStats ¶
type ChainStats struct {
TotalExecutions atomic.Int64 // 总执行次数
TotalPanics atomic.Int64 // 总 panic 次数
TotalInterceptors atomic.Int64 // 总拦截器调用次数
}
ChainStats 通知链统计信息
用于收集通知链执行的运行时统计,所有字段均为原子操作,并发安全。
字段说明:
- TotalExecutions: 总执行次数
- TotalPanics: 总 panic 次数
- TotalInterceptors: 总拦截器调用次数
var GlobalChainStats ChainStats
GlobalChainStats 全局通知链统计
type GeneratedProxyFactory ¶
type GeneratedProxyFactory struct {
// contains filtered or unexported fields
}
GeneratedProxyFactory 代码生成代理工厂
创建代码生成的代理对象
func NewGeneratedProxyFactory ¶
func NewGeneratedProxyFactory() *GeneratedProxyFactory
NewGeneratedProxyFactory 创建代码生成代理工厂
func (*GeneratedProxyFactory) Create ¶
func (f *GeneratedProxyFactory) Create(beanID string, target any) (any, error)
Create 创建代理对象
func (*GeneratedProxyFactory) CreateOrFallback ¶
func (f *GeneratedProxyFactory) CreateOrFallback(beanID string, target any, fallback Weaver) any
CreateOrFallback 创建代理或回退到运行时代理
type GeneratedProxyRegistry ¶
type GeneratedProxyRegistry struct {
// contains filtered or unexported fields
}
GeneratedProxyRegistry 代码生成代理注册表
管理代码生成的代理对象,提供查找和获取功能
func NewGeneratedProxyRegistry ¶
func NewGeneratedProxyRegistry() *GeneratedProxyRegistry
NewGeneratedProxyRegistry 创建代码生成代理注册表
func (*GeneratedProxyRegistry) Get ¶
func (r *GeneratedProxyRegistry) Get(beanID string) (reflect.Type, bool)
Get 获取代理类型
func (*GeneratedProxyRegistry) Has ¶
func (r *GeneratedProxyRegistry) Has(beanID string) bool
Has 检查是否存在代理
func (*GeneratedProxyRegistry) List ¶
func (r *GeneratedProxyRegistry) List() []string
List 列出所有注册的bean ID
type Interceptor ¶
type Interceptor func(inv Invocation, next func(Invocation) any) any
Interceptor 拦截器函数类型。
在通知链执行前后提供额外的处理逻辑,采用中间件模式。 inv 为当前调用信息,next 为下一个处理函数。
示例:
aop.WithInterceptor(func(inv aop.Invocation, next func(aop.Invocation) any) any {
start := time.Now()
result := next(inv)
slog.Info("method called", "name", inv.Signature().Name(), "duration", time.Since(start))
return result
})
type InterfaceProxyWrapper ¶
type InterfaceProxyWrapper struct {
// contains filtered or unexported fields
}
InterfaceProxyWrapper 接口代理包装器(实现 doc.go 中定义的结构体)。
通过反射转发所有接口方法调用,支持 AOP 切面织入。 由于 Go 运行时无法动态替换接口方法,此包装器提供显式的 Invoke/InvokeContext 方法。
使用方式:
wrapper := aop.NewInterfaceProxyWrapper(target, advisors, iface) result, err := wrapper.InvokeContext(ctx, "MethodName", arg1, arg2)
设计模式: Proxy
func NewInterfaceProxyWrapper ¶
func NewInterfaceProxyWrapper(target any, advisors []*AspectMeta, iface reflect.Type) *InterfaceProxyWrapper
NewInterfaceProxyWrapper 创建接口代理包装器。
func (*InterfaceProxyWrapper) GetAdvisors ¶
func (w *InterfaceProxyWrapper) GetAdvisors() []*AspectMeta
GetAdvisors 获取切面列表
func (*InterfaceProxyWrapper) GetTarget ¶
func (w *InterfaceProxyWrapper) GetTarget() any
GetTarget 获取原始目标对象
func (*InterfaceProxyWrapper) Invoke ¶
func (w *InterfaceProxyWrapper) Invoke(methodName string, args ...any) (any, error)
Invoke 调用接口方法
func (*InterfaceProxyWrapper) InvokeContext ¶
func (w *InterfaceProxyWrapper) InvokeContext(ctx context.Context, methodName string, args ...any) (any, error)
InvokeContext 带上下文的方法调用
func (*InterfaceProxyWrapper) SetExecutor ¶
func (w *InterfaceProxyWrapper) SetExecutor(executor ChainExecutor)
SetExecutor 设置通知链执行器
type InvalidTargetFormatError ¶
type InvalidTargetFormatError struct {
// contains filtered or unexported fields
}
InvalidTargetFormatError 无效目标格式错误
func (*InvalidTargetFormatError) Error ¶
func (e *InvalidTargetFormatError) Error() string
type Invocation ¶
type Invocation interface {
JoinPoint
// Proceed 继续执行。
//
// 调用此方法可以执行目标方法或通知链中的下一个通知。
// 可以传递自定义参数,这些参数会传递给下游的调用。
Proceed(args ...any) any
// SetContext 设置调用上下文。
SetContext(ctx context.Context)
}
Invocation 调用信息接口。
继承自 JoinPoint,并添加了 Proceed 方法。 用于在 Around 通知中控制方法的执行流程。
type JoinPoint ¶
type JoinPoint interface {
// Method 获取被拦截的方法。
Method() any
// Args 获取方法调用时的参数。
Args() []any
// Signature 获取方法签名。
Signature() MethodSignature
// This 获取代理对象。
This() any
// Target 获取目标对象。
Target() any
// Context 获取调用上下文。
Context() context.Context
}
JoinPoint 连接点接口。
AOP 核心概念,代表程序执行的某个位置。在 enhance AOP 框架中, 连接点通常指方法调用,通知(Advice)可以通过 JoinPoint 访问方法调用的上下文。
type MethodInvocation ¶
type MethodInvocation struct {
MethodName string // 方法名称
Func any // 目标方法(函数值)
Params []any // 方法调用参数列表
Object any // 目标对象(被代理的原始对象)
Proxy any // 代理对象本身,未设置时 This() 返回 nil
Ctx context.Context // 上下文信息,未设置时 Context() 返回 context.Background()
// contains filtered or unexported fields
}
MethodInvocation 方法调用信息。
用于代码生成的代理类,包含方法调用所需的所有信息。 新增字段 Proxy、Ctx 可按需设置,未设置时保持零值兼容。
func (*MethodInvocation) Context ¶
func (m *MethodInvocation) Context() context.Context
Context 获取上下文
如果已设置上下文,则返回该上下文;否则返回 context.Background()。
func (*MethodInvocation) Proceed ¶
func (m *MethodInvocation) Proceed(args ...any) any
Proceed 继续执行
如果已通过 SetProceed 设置了执行函数,则调用它; 否则通过反射调用目标方法。
func (*MethodInvocation) SetContext ¶
func (m *MethodInvocation) SetContext(ctx context.Context)
SetContext 设置上下文
设置后,后续通知链中的 JoinPoint.Context() 将返回新的上下文。
func (*MethodInvocation) SetProceed ¶
func (m *MethodInvocation) SetProceed(p ProceedFunc)
SetProceed 设置继续执行函数
在Around通知中,用于设置继续执行目标方法或下一个通知的函数。
func (*MethodInvocation) Signature ¶
func (m *MethodInvocation) Signature() MethodSignature
Signature 获取方法签名
type MethodSignature ¶
type MethodSignature interface {
// Name 获取方法名。
Name() string
// DeclaringType 获取方法声明的类型。
DeclaringType() reflect.Type
}
MethodSignature 方法签名接口。
描述方法的元数据信息,包括方法名和声明类型。
func NewMethodSignature ¶
func NewMethodSignature(name string, t reflect.Type) MethodSignature
NewMethodSignature 创建方法签名。
参数:
- name: 方法名
- t: 方法声明的类型
返回值:
- MethodSignature: 方法签名实例
type PointCut ¶
type PointCut interface {
// MatchClass 匹配类。
//
// 检查给定类型是否匹配切点条件。
// 如果返回 true,表示该类型的所有方法都可能被拦截(还需通过方法匹配)。
// 如果返回 false,则该类型的所有方法都不会被拦截。
MatchClass(c reflect.Type) bool
// MatchMethod 匹配方法。
//
// 检查给定方法是否匹配切点条件。
// 只有匹配的方法才会被代理拦截。
MatchMethod(m reflect.Method) bool
// String 返回切点的字符串表示。
//
// 用于调试和日志输出,返回切点的类型和匹配规则。
String() string
}
PointCut 切点接口。
定义 AOP 中用于匹配目标方法的规则。切点决定了哪些类或方法需要被拦截, 是 AOP 框架的核心组件之一。
匹配流程:
- 调用 MatchClass 检查目标类型是否匹配(如果类匹配器存在)
- 调用 MatchMethod 检查目标方法是否匹配(如果方法匹配器存在)
- 如果都匹配(或对应匹配器为 nil),则该方法会被拦截
func Compose ¶
Compose 组合多个切点(AND 逻辑)
只有当所有切点都匹配时,才认为匹配。
参数:
- pointcuts: 切点列表
返回值:
- PointCut: 组合后的切点
示例:
// 匹配 Service 类中以 Get 开头的方法
aop.Compose(
aop.MatchByClassName("*Service"),
aop.MatchByNamePrefix("Get"),
)
func ComposeOr ¶
ComposeOr 组合多个切点(OR 逻辑)
只要有一个切点匹配,就认为匹配。
参数:
- pointcuts: 切点列表
返回值:
- PointCut: 组合后的切点
示例:
// 匹配 GetUser 或 UpdateUser 方法
aop.ComposeOr(
aop.MatchByName("GetUser"),
aop.MatchByName("UpdateUser"),
)
func MatchAll ¶
func MatchAll() PointCut
MatchAll 匹配所有
返回匹配所有类和方法的切点。
返回值:
- PointCut: 匹配所有目标的切点
示例:
// 拦截所有方法 aop.MatchAll()
func MatchByAnnotation ¶
MatchByAnnotation 按注解类型匹配
匹配带有指定注解类型的方法。
参数:
- annotationType: 注解类型
返回值:
- PointCut: 匹配带注解方法的切点
注意:
- 此方法通过方法名前缀来匹配
func MatchByClassName ¶
MatchByClassName 按类名匹配
匹配指定类名的所有方法。
参数:
- className: 类名(支持通配符 *)
返回值:
- PointCut: 匹配指定类名的切点
示例:
// 匹配所有 Service 结尾的类
aop.MatchByClassName("*Service")
// 精确匹配 UserService 类
aop.MatchByClassName("UserService")
func MatchByMethodSignature ¶
MatchByMethodSignature 按方法签名匹配
匹配具有指定方法签名的方法(方法名 + 参数类型)。 这是最精确的匹配方式,类似 Spring 的 execution 表达式。
参数:
- methodName: 方法名
- paramTypes: 参数类型列表(可选,nil 表示只匹配方法名)
返回值:
- PointCut: 匹配指定方法签名的切点
示例:
// 精确匹配 GetUser(id int64) 方法
aop.MatchByMethodSignature("GetUser", reflect.TypeOf(int64(0)))
// 匹配所有名为 Save 的方法(不限参数)
aop.MatchByMethodSignature("Save", nil)
func MatchByName ¶
MatchByName 按方法名匹配
匹配指定名称的方法。
参数:
- name: 方法名
返回值:
- PointCut: 匹配指定方法名的切点
示例:
// 只拦截 DoSomething 方法
aop.MatchByName("DoSomething")
func MatchByNamePrefix ¶
MatchByNamePrefix 按方法名前缀匹配
匹配指定前缀的方法。
参数:
- prefix: 方法名前缀
返回值:
- PointCut: 匹配指定前缀的切点
示例:
// 拦截所有以 Do 开头的方法
aop.MatchByNamePrefix("Do")
func MatchByPackage ¶
MatchByPackage 按包路径匹配
匹配指定包路径下的所有类和方法。
参数:
- packagePath: 包路径前缀(如 "github.com/myapp/service")
返回值:
- PointCut: 匹配指定包的切点
示例:
// 拦截 service 包下的所有方法
aop.MatchByPackage("github.com/myapp/service")
func MatchByRegex ¶
MatchByRegex 按正则表达式匹配
匹配符合正则表达式的方法名。
参数:
- pattern: 正则表达式
返回值:
- PointCut: 匹配正则表达式的切点
示例:
// 拦截所有以 do 或 Do 开头的方法
aop.MatchByRegex("(?i)^do.*")
func MatchByReturnType ¶
MatchByReturnType 按返回值类型匹配
匹配返回指定类型的方法。
参数:
- returnType: 返回值类型
返回值:
- PointCut: 匹配指定返回值类型的切点
示例:
// 匹配所有返回 error 的方法 aop.MatchByReturnType(reflect.TypeOf((*error)(nil)).Elem())
func MatchClass ¶
func MatchClass(matcher ClassMatcher) PointCut
MatchClass 匹配类
返回只匹配类的切点,不匹配具体方法。
参数:
- matcher: 类匹配函数
返回值:
- PointCut: 匹配给定类的切点
示例:
aop.MatchClass(func(t reflect.Type) bool {
return t.Name() == "UserService"
})
func MatchClassMethod ¶
func MatchClassMethod(classMatcher ClassMatcher, methodMatcher MethodMatcher) PointCut
MatchClassMethod 匹配类和方法的组合切点
同时指定类和方法匹配条件。
参数:
- classMatcher: 类匹配函数
- methodMatcher: 方法匹配函数
返回值:
- PointCut: 组合切点
func MatchInterface ¶
MatchInterface 按接口类型匹配
匹配实现了指定接口的类型。
参数:
- y: 接口类型,传入接口变量即可
返回值:
- PointCut: 匹配实现接口的类的切点
示例:
// 拦截所有实现 ServiceInterface 接口的类 aop.MatchInterface((*ServiceInterface)(nil))
func MatchMethod ¶
func MatchMethod(matcher MethodMatcher) PointCut
MatchMethod 匹配方法
返回只匹配方法的切点,不匹配具体类。
参数:
- matcher: 方法匹配函数
返回值:
- PointCut: 匹配给定方法的切点
示例:
aop.MatchMethod(func(m reflect.Method) bool {
return m.Name == "DoSomething"
})
type PointCutFunc ¶
PointCutFunc 函数式切点适配器
Go 惯用法:使用函数类型替代接口实现,符合 Go 标准库中 http.HandlerFunc 的设计模式。 将函数适配为 PointCut 接口,仅匹配方法(类匹配始终返回 true)。
func (PointCutFunc) MatchClass ¶
func (f PointCutFunc) MatchClass(c reflect.Type) bool
MatchClass 实现 PointCut 接口(始终返回 true)
func (PointCutFunc) MatchMethod ¶
func (f PointCutFunc) MatchMethod(m reflect.Method) bool
MatchMethod 实现 PointCut 接口
type PointCutWithClass ¶
type PointCutWithClass struct {
Class ClassMatcher
Match MethodMatcher
}
PointCutWithClass 带类匹配的函数式切点
同时支持类和方法匹配的函数式切点适配器。 当需要同时指定类和方法匹配规则时使用。
func (PointCutWithClass) MatchClass ¶
func (p PointCutWithClass) MatchClass(c reflect.Type) bool
MatchClass 实现 PointCut 接口
func (PointCutWithClass) MatchMethod ¶
func (p PointCutWithClass) MatchMethod(m reflect.Method) bool
MatchMethod 实现 PointCut 接口
type ProceedFunc ¶
ProceedFunc 继续执行函数类型。
在 Around 通知中,调用此函数可以继续执行目标方法或下一个通知。 参数为传递给目标方法的参数,返回值为目标方法的返回值。
type ProxyFactory ¶
type ProxyFactory struct {
// contains filtered or unexported fields
}
ProxyFactory 代理工厂(实现 doc.go 中定义的结构体)。
负责创建 AOP 代理对象。根据目标对象的类型(接口或结构体),创建相应的代理。
使用示例:
factory := aop.NewProxyFactory(&UserService{})
factory.SetAspects(aspects)
proxy := factory.GetProxy()
func NewProxyFactory ¶
func NewProxyFactory(target any) *ProxyFactory
NewProxyFactory 创建代理工厂
参数:
- target: 目标对象,可以是结构体指针或接口实现
返回值:
- *ProxyFactory: 代理工厂实例
示例:
factory := aop.NewProxyFactory(&UserService{})
func (*ProxyFactory) GetProxy ¶
func (p *ProxyFactory) GetProxy() any
GetProxy 获取代理对象
根据目标对象的类型,创建并返回代理对象。 如果没有匹配的切面,则返回原对象。
返回值:
- any: 代理对象或原对象
func (*ProxyFactory) SetAspects ¶
func (p *ProxyFactory) SetAspects(aspects []*AspectMeta)
SetAspects 设置切面
参数:
- aspects: 切面元数据列表
func (*ProxyFactory) SetExecutor ¶
func (p *ProxyFactory) SetExecutor(executor ChainExecutor)
SetExecutor 设置通知链执行器
设置后,由此工厂创建的 ReflectiveAopProxy 将使用指定的执行器。 传入 nil 表示使用全局默认执行器。
type ReflectiveAopProxy ¶
type ReflectiveAopProxy struct {
// contains filtered or unexported fields
}
ReflectiveAopProxy 基于反射的 AOP 代理。
由于 Go 运行时无法动态替换结构体方法,ReflectiveAopProxy 通过反射 拦截方法调用并执行通知链。用户通过 Call/CallContext 方法调用目标方法, AOP 通知会自动执行。
使用方式:
proxy := weaver.Weave(target).(*aop.ReflectiveAopProxy)
result, err := proxy.Call("MethodName", arg1, arg2)
result, err := proxy.CallContext(ctx, "MethodName", arg1, arg2)
func AsReflectiveProxy ¶
func AsReflectiveProxy(obj any) (*ReflectiveAopProxy, bool)
AsReflectiveProxy 将对象转换为 ReflectiveAopProxy
func (*ReflectiveAopProxy) Call ¶
func (p *ReflectiveAopProxy) Call(methodName string, args ...any) (any, error)
Call 通过反射调用目标方法并执行通知链
func (*ReflectiveAopProxy) CallContext ¶
func (p *ReflectiveAopProxy) CallContext(ctx context.Context, methodName string, args ...any) (any, error)
CallContext 通过反射调用目标方法并执行通知链(带 context)
参数:
- ctx: 上下文,可通过 JoinPoint.Context() 在通知中获取
- methodName: 方法名
- args: 方法参数
返回值:
- any: 方法返回值(多返回值时返回 []any)
- error: 调用错误(仅表示方法查找失败,不包含目标方法的 panic)
func (*ReflectiveAopProxy) MustCall ¶
func (p *ReflectiveAopProxy) MustCall(methodName string, args ...any) any
MustCall 调用目标方法,panic on error
func (*ReflectiveAopProxy) SetExecutor ¶
func (p *ReflectiveAopProxy) SetExecutor(executor ChainExecutor)
SetExecutor 设置通知链执行器
type Weaver ¶
type Weaver interface {
// Weave 织入目标对象。
//
// 将已注册的切面织入目标对象,返回代理对象。
//
// 参数:
// - target: 目标对象,可以是结构体指针或接口实现
//
// 返回值:
// - any: 代理对象(如果匹配到切面)或原对象
//
// 注意:
// - 返回的对象类型可能和原对象不同(代理类型)
// - 使用代理对象调用方法时,匹配的通知会自动执行
Weave(target any) any
// AddAspects 添加切面。
//
// 添加一个或多个切面到织入器。
// 添加后,这些切面会应用于后续 Weave 调用。
//
// 参数:
// - aspects: 一个或多个切面元数据
AddAspects(aspects ...*AspectMeta)
}
Weaver 织入器接口。
负责将切面织入目标对象,生成代理对象。 类似于 Spring 中的 AopProxyFactory。
工作流程:
- 创建织入器: NewWeaver()
- 添加切面: AddAspects(aspect1, aspect2, ...)
- 织入目标: Weave(target) -> 返回代理对象
织入规则:
- 如果目标对象没有任何匹配的切面,返回原对象(不进行代理)
- 如果目标对象有匹配的切面,创建代理对象
- 代理对象的方法调用会触发匹配的通知