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

Spring AOP自定义注解启动报错:CommitServiceAdvice非注解类型

解决Spring AOP中"Type referred to is not an annotation type"错误

Hey, let's break down the error you're facing: java.lang.IllegalArgumentException: error Type referred to is not an annotation type: CommitServiceAdvice. The root cause here is straightforward—your aspect class doesn't import the custom CommitServiceAdvice annotation you defined, so AspectJ can't recognize it as a valid annotation type.

Step-by-Step Fixes

1. Add the Missing Annotation Import

In your CommitServiceAspect class, add the import statement for your custom annotation at the top:

import com.sell.annotations.CommitServiceAdvice;

Without this import, the JVM has no idea what CommitServiceAdvice is when parsing the @annotation() expression in your @Around advice, hence the "not an annotation type" error.

2. Fix the argNames Configuration

Your current @Around annotation specifies argNames="mfa", but your advice method has two parameters: ProceedingJoinPoint joinPoint and MessageFlowAggregator mfa. The argNames value needs to match the parameter order exactly. Update it to:

@Around(value = "@annotation(CommitServiceAdvice)", argNames = "joinPoint,mfa")

Alternatively, you can omit argNames entirely if your compiler retains parameter names (most modern setups do), but explicit configuration avoids ambiguity.

3. Align Return Types for Safety

Your advice method returns MessageFlowAggregator, but the target process method returns Object. While joinPoint.proceed() will return the target method's result, this mismatch could cause type conversion issues if the target method ever returns a non-MessageFlowAggregator value. It's safer to change the advice method's return type to Object:

public Object advice(ProceedingJoinPoint joinPoint, MessageFlowAggregator mfa) throws Throwable {
    log.debug(">>> matching advice on {}",joinPoint);
    Object result = joinPoint.proceed();
    log.debug("<<< returning advice on {}",joinPoint);
    return result;
}

Additional Checks to Confirm

  • AOP is Enabled: Since you're using spring-boot-starter-aop, Spring Boot automatically enables AOP via @EnableAspectJAutoProxy—no extra configuration needed here, but double-check your main application class doesn't accidentally disable it.
  • Annotation Retention: You already set @Retention(RetentionPolicy.RUNTIME) on CommitServiceAdvice, which is critical—only runtime-retention annotations can be intercepted by Spring AOP.
  • Component Scanning: Ensure your annotation, aspect, and service classes are all within packages that Spring scans (default behavior covers packages under your main application class, so this is likely fine).

Fixed Aspect Class Code

Here's the full corrected CommitServiceAspect for reference:

package com.sell.business.service.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sell.aggregator.MessageFlowAggregator;
import com.sell.annotations.CommitServiceAdvice; // Added missing import

@Aspect
@Component
public class CommitServiceAspect {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Around(value = "@annotation(CommitServiceAdvice)", argNames = "joinPoint,mfa")
    public Object advice(ProceedingJoinPoint joinPoint, MessageFlowAggregator mfa) throws Throwable {
        log.debug(">>> matching advice on {}",joinPoint);
        Object result = joinPoint.proceed();
        log.debug("<<< returning advice on {}",joinPoint);
        return result;
    }
}

Make these changes, and your AOP setup should work without that annotation type error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:43:40