Spring AOP自定义注解启动报错:CommitServiceAdvice非注解类型
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)onCommitServiceAdvice, 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

