spring-projects/spring-framework · error · IllegalArgumentException

{} is not a valid AspectJ annotation

Error message

{} is not a valid AspectJ annotation

What it means

Thrown inside the AspectJAnnotation constructor when resolving the pointcut expression or argNames from a method-level AspectJ annotation (@Before, @Around, @After, @AfterReturning, @AfterThrowing, @Pointcut) fails. It wraps the underlying reflective exception, signalling that the annotation is malformed or its attributes cannot be read. This is generally a low-level parsing failure indicating annotation attributes are missing or in an unexpected state.

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java:189

		private final Annotation annotation;

		private final AspectJAnnotationType annotationType;

		private final String pointcutExpression;

		private final String argumentNames;

		public AspectJAnnotation(Annotation annotation) {
			this.annotation = annotation;
			this.annotationType = determineAnnotationType(annotation);
			try {
				this.pointcutExpression = resolvePointcutExpression(annotation);
				Object argNames = AnnotationUtils.getValue(annotation, "argNames");
				this.argumentNames = (argNames instanceof String names ? names : "");
			}
			catch (Exception ex) {
				throw new IllegalArgumentException(annotation + " is not a valid AspectJ annotation", ex);
			}
		}

		private AspectJAnnotationType determineAnnotationType(Annotation annotation) {
			AspectJAnnotationType type = annotationTypeMap.get(annotation.annotationType());
			if (type != null) {
				return type;
			}
			throw new IllegalStateException("Unknown annotation type: " + annotation);
		}

		private String resolvePointcutExpression(Annotation annotation) {
			for (String attributeName : EXPRESSION_ATTRIBUTES) {
				Object val = AnnotationUtils.getValue(annotation, attributeName);
				if (val instanceof String str && !str.isEmpty()) {
					return str;
				}
			}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Inspect the offending annotation on the named method: ensure it is a genuine AspectJ annotation with a valid String 'value' or 'pointcut' attribute.
  2. Verify aspectjweaver and spring-aop versions are aligned and present on the classpath.
  3. Recompile the aspect class to eliminate stale or corrupted annotation metadata.
  4. If using meta-annotations, ensure the pointcut expression attribute is resolvable via Spring's AnnotationUtils.
Defensive patterns

Strategy: validation

Validate before calling

import org.springframework.core.annotation.AnnotationUtils;
import org.aspectj.lang.annotation.*;
import java.lang.reflect.Method;

void verifyAdviceAnnotationReadable(Method m) {
  for (Class<?> a : new Class<?>[]{Before.class, Around.class, After.class,
          AfterReturning.class, AfterThrowing.class, Pointcut.class}) {
    Object ann = AnnotationUtils.findAnnotation(m, a);
    if (ann != null) {
      Object v = AnnotationUtils.getValue(ann, "value");
      Object p = AnnotationUtils.getValue(ann, "pointcut");
      if (!(v instanceof String s && !s.isEmpty()) && !(p instanceof String s2 && !s2.isEmpty()))
        throw new IllegalStateException("Advice annotation on " + m + " has no readable pointcut");
    }
  }
}

Try / catch

try {
  factory.getAdvisors(instanceFactory);
} catch (IllegalArgumentException ex) {
  if (ex.getMessage().contains("not a valid AspectJ annotation")) {
    log.error("Malformed advice annotation on aspect method", ex);
  } else throw ex;
}

Prevention

When it happens

Trigger: ReflectiveAspectJAdvisorFactory.getAdvice()/getAdvisor() calling findAspectJAnnotationOnMethod on a method whose AspectJ annotation has attributes that AnnotationUtils.getValue cannot read correctly, or where the annotation proxy throws on access.

Common situations: A corrupted or partially applied annotation, a custom meta-annotation masquerading as an AspectJ advice annotation, classpath/version conflicts in aspectjweaver that break annotation proxies, or reflective manipulation of aspects.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/7f319d709521eb64.json. Report an issue: GitHub.