【Quarkus】基于CDI和拦截器实现AOP功能

Quarkus 基于CDI和拦截器实现AOP功能

在 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.1specification. 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.

实现步骤

  1. 创建自定义注解
  2. 创建拦截器
  3. 应用拦截器
创建自定义注解

Simple Interceptor Binding Example

java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import jakarta.interceptor.InterceptorBinding;

@InterceptorBinding 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR}) 
@Inherited 
public @interface Logged {
}
  • @InterceptorBinding 这是一个拦截器绑定注解。请参阅以下示例了解其使用方法。
  • @Target 拦截器绑定注解始终放在拦截器类型上,并且可以放在目标类型或方法上。
  • @Inherited 拦截器绑定通常是@Inherited,但不是必须如此。
创建拦截器

Simple Interceptor Example

java 复制代码
import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Logged 
@Priority(Interceptor.Priority.APPLICATION)
@Interceptor 
public class LoggingInterceptor {

   @Inject 
   Logger logger;

   @AroundInvoke 
   Object logInvocation(InvocationContext context) {
      // ...log before
      Object ret = context.proceed(); 
      // ...log after
      return ret;
   }

}
  • @Logged 拦截器绑定注解用于将拦截器绑定到 bean。只需使用 @Logged 注释 bean 类,如以下示例所示。
  • @Priority 启用拦截器并影响拦截器的顺序。优先级值较小的拦截器首先被调用。
  • @Interceptor 标记拦截器组件。
  • @Inject 拦截器可以注入依赖项。
  • @AroundInvoke 表示插入业务方法的方法。
  • Object ret = context.proceed(); 继续执行拦截器链中的下一个拦截器或调用被拦截的业务方法。

💡 拦截器的实例是它们拦截的 bean 实例的依赖对象,即为每个拦截的 bean 创建一个新的拦截器实例。

应用拦截器

Simple Example of Interceptor Usage

java 复制代码
import jakarta.enterprise.context.ApplicationScoped;

@Logged  
@ApplicationScoped
public class MyService {
   void doSomething() {
       ...
   }
}
  • 拦截器绑定注解放在一个bean类上,这样所有的业务方法都会被拦截。注解也可以放在单独的方法上,在这种情况下,只有被注解的方法才会被拦截。
  • 请记住,@Logged 注释是@Inherited。如果有一个继承自 MyService 的 bean 类,则 LoggingInterceptor 也将应用于它。
相关推荐
HalvmånEver14 小时前
7.高并发内存池大页内存申请释放以及使用定长内存池脱离new
java·spring boot·spring
凤山老林14 小时前
SpringBoot 使用 H2 文本数据库构建轻量级应用
java·数据库·spring boot·后端
清汤饺子14 小时前
用 Cursor 半年了,效率还是没提升?是因为你没用对这 7 个功能
前端·后端·cursor
雨夜之寂14 小时前
Browser Use + DeepSeek,我踩了哪些坑
后端·面试
wefly201714 小时前
开发者效率神器!jsontop.cn一站式工具集,覆盖开发全流程高频需求
前端·后端·python·django·flask·前端开发工具·后端开发工具
赶路人儿15 小时前
UTC时间和时间戳介绍
java·开发语言
dreamread15 小时前
【SpringBoot整合系列】SpringBoot3.x整合Swagger
java·spring boot·后端
6+h15 小时前
【java】基本数据类型与包装类:拆箱装箱机制
java·开发语言·python
把你毕设抢过来15 小时前
基于Spring Boot的社区智慧养老监护管理平台(源码+文档)
数据库·spring boot·后端
一直都在57215 小时前
Spring面经
java·后端·spring