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

@Retryable注解应用于非Spring Bean类方法不生效问题求助

问题根因

@Retryable基于Spring AOP代理实现,仅对Spring容器管理的Bean的公共方法生效,你当前标注注解的OAuth1RestClient不属于Spring Bean,Spring无法生成代理对象切入重试逻辑,因此不会触发重试。

修复步骤
  • 第一步:将OAuth1RestClient纳入Spring容器管理
    两种实现方式二选一:
    1. 在类上添加@Component注解
    @Component // 新增注解
    public class OAuth1RestClient extends OAuthRestClient {
      // 原有代码保持不变
    }
    
    1. 在配置类中通过@Bean注册实例
    @Configuration
    @EnableRetry
    public class RetryConfiguration {
        @Bean
        public OAuth1RestClient oAuth1RestClient() {
            return new OAuth1RestClient();
        }
    }
    
  • 第二步:通过Spring注入的方式调用方法
    调用executeRequest方法时,禁止手动new OAuth1RestClient()创建实例,必须通过@Autowired或者构造函数注入的方式获取Spring容器中的实例,调用注入实例的方法才会触发代理逻辑。
其他注意事项
  • 不要在类内部直接调用@Retryable标注的方法:同一个类内部方法调用走的是原生对象,不会触发代理切入。
  • 检查依赖是否完整:spring-retry需要AOP依赖支撑,若未引入spring-aspects需要补充pom依赖:
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
</dependency>
  • 确认异常未被吞掉:如果myService.execute(request)抛出的异常在executeRequest方法内部被try-catch捕获且未重新抛出,重试逻辑也不会触发。

内容的提问来源于stack exchange,提问作者Joy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 04:15:04