【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 也将应用于它。
相关推荐
zhanggongzichu9 分钟前
小白怎么理解后端分层概念
后端·全栈
鬼蛟23 分钟前
Spring————事务
android·java·spring
西门吹-禅1 小时前
【sap fiori cds up error】
java·服务器·sap cap cds
stark张宇1 小时前
Golang后端面试复盘:从Swoole到IM架构,如何支撑360w用户的实时消息推送?
后端
小码哥_常1 小时前
从0到1:搭建Spring Boot 3企业级认证授权平台
后端
敲代码的嘎仔1 小时前
Java后端面试——SSM框架面试题
java·面试·职场和发展·mybatis·ssm·springboot·八股
小码哥_常1 小时前
告别扫库噩梦!Spring Boot+Redis让订单超时管理飞起来
后端
大傻^2 小时前
Spring AI Alibaba RAG实战:基于向量存储的检索增强生成
java·人工智能·spring
大傻^2 小时前
Spring AI Alibaba 快速入门:基于通义千问的AI应用开发环境搭建
java·人工智能·后端·spring·springai·springaialibaba
伯恩bourne2 小时前
Google Guava:Java 核心工具库的卓越之选
java·开发语言·guava