You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus构建Guice方法拦截器报错:找不到bindInterceptor方法

解决Quarkus中自定义拦截器编译错误问题

问题分析

你尝试用Guice的MethodInterceptor和自定义注解实现方法前后的光标切换逻辑,但Quarkus构建时出现编译错误,核心原因是Quarkus默认采用Jakarta CDI作为依赖注入规范,而非Google Guice,直接使用Guice的bindInterceptor方法在Quarkus环境中不被支持,导致方法签名匹配失败。

解决方案:改用Quarkus CDI拦截器规范

1. 修改自定义注解(添加CDI拦截器绑定)

import jakarta.interceptor.InterceptorBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface WaitCursor {}

2. 重写拦截器类(适配CDI规范)

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
@WaitCursor // 绑定到自定义注解
public class WaitCursorInterceptor {

    @AroundInvoke
    public Object handleWaitCursor(InvocationContext ctx) throws Exception {
        // 显示等待光标
        MainUI.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        try {
            // 执行目标方法
            return ctx.proceed();
        } finally {
            // 确保异常时也能恢复光标状态
            MainUI.getInstance().setCursor(Cursor.getDefaultCursor());
        }
    }
}

3. 清理冗余代码

  • 删除原有的Guice相关类WaitCursorModule
  • 移除项目中Google Guice的依赖(如果存在),避免与Quarkus CDI冲突

注意事项

  • 确保拦截器和注解类在Quarkus的默认扫描路径下(src/main/java下的包都会被自动扫描)
  • 若拦截器未生效,可在application.properties中添加配置:quarkus.cdi.interceptors.enabled=true(Quarkus 2.x版本默认已启用)

内容的提问来源于stack exchange,提问作者Danilo Köber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 00:01:07