在 Quarkus 中实现 AOP(面向切面编程)功能,通常通过使用 CDI(Contexts and Dependency Injection)和拦截器来实现。Quarkus 支持使用标准的 CDI 注解和拦截器机制来进行 AOP 编程。
CDI简介
CDI全称:CONTEXTS AND DEPENDENCY INJECTION
Quarkus DI 解决方案(也称为 ArC)基于 Jakarta 上下文和依赖注入 4.1 规范。它实现了 CDI Lite 规范,并进行了选定的改进,并通过了 CDI Lite TCK。它不实现 CDI Full。另请参阅支持的功能和限制列表
Quarkus DI solution (also called ArC) is based on the Jakarta Contexts and Dependency Injection 4.1 specification. It implements the CDI Lite specification, with selected improvements on top, and passes the CDI Lite TCK. It does not implement CDI Full. See also the list of supported features and limitations.
拦截器简介
拦截器用于将横切关注点与业务逻辑分开。有一个单独的规范 - Java 拦截器 - 定义了基本的编程模型和语义。
Interceptors are used to separate cross-cutting concerns from business logic. There is a separate specification - Java Interceptors - that defines the basic programming model and semantics.
实现步骤
- 创建自定义注解
- 创建拦截器
- 应用拦截器
创建自定义注解
Simple Interceptor Binding Example
import java.lang.annotation.ElementType;
import java.lang.annotation.<