spring-projects/spring-framework · error · IllegalArgumentException

Class [{}] is not a valid aspect type

Error message

Class [{}] is not a valid aspect type

What it means

Thrown by AspectJProxyFactory.createAspectMetadata() when AjTypeSystem reports the class (and none of its superclasses up to Object) as not being an aspect. This means the class lacks a valid @Aspect annotation (or is an ajc-compiled aspect being ignored due to the spring.aop.ajc.ignore flag). Only classes recognized as AspectJ aspects can be added to an AspectJProxyFactory.

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java:136

	 * @see AspectJProxyUtils#makeAdvisorChainAspectJCapableIfNecessary(List)
	 */
	private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) {
		List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory);
		Class<?> targetClass = getTargetClass();
		Assert.state(targetClass != null, "Unresolvable target class");
		advisors = AopUtils.findAdvisorsThatCanApply(advisors, targetClass);
		AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors);
		AnnotationAwareOrderComparator.sort(advisors);
		addAdvisors(advisors);
	}

	/**
	 * Create an {@link AspectMetadata} instance for the supplied aspect type.
	 */
	private AspectMetadata createAspectMetadata(Class<?> aspectClass, String aspectName) {
		AspectMetadata am = new AspectMetadata(aspectClass, aspectName);
		if (!am.getAjType().isAspect()) {
			throw new IllegalArgumentException("Class [" + aspectClass.getName() + "] is not a valid aspect type");
		}
		return am;
	}

	/**
	 * Create a {@link MetadataAwareAspectInstanceFactory} for the supplied aspect type. If the aspect type
	 * has no per clause, then a {@link SingletonMetadataAwareAspectInstanceFactory} is returned, otherwise
	 * a {@link PrototypeAspectInstanceFactory} is returned.
	 */
	private MetadataAwareAspectInstanceFactory createAspectInstanceFactory(
			AspectMetadata am, Class<?> aspectClass, String aspectName) {

		MetadataAwareAspectInstanceFactory instanceFactory;
		if (am.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
			// Create a shared aspect instance.
			Object instance = getSingletonAspectInstance(aspectClass);
			instanceFactory = new SingletonMetadataAwareAspectInstanceFactory(instance, aspectName);
		}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Annotate the class with org.aspectj.lang.annotation.@Aspect.
  2. If the class is ajc-compiled and spring.aop.ajc.ignore is true, either unset that flag or convert to annotation style.
  3. For non-@Aspect advice (e.g. MethodInterceptor), use ProxyFactory.addAdvice() instead of AspectJProxyFactory.addAspect().

Example fix

// before
public class MyInterceptor { @Before("execution(* *(..))") public void before() {} }
factory.addAspect(MyInterceptor.class);
// after
@Aspect
public class MyInterceptor { @Before("execution(* *(..))") public void before() {} }
factory.addAspect(MyInterceptor.class);
Defensive patterns

Strategy: validation

Validate before calling

import org.aspectj.lang.annotation.Aspect;

void ensureAspect(Class<?> c) {
  if (c.getAnnotation(Aspect.class) == null)
    throw new IllegalArgumentException(c + " must be annotated @Aspect to use with AspectJProxyFactory");
}

Type guard

boolean isAtAspect(Class<?> c) {
  return c.isAnnotationPresent(org.aspectj.lang.annotation.Aspect.class);
}

Prevention

When it happens

Trigger: Calling AspectJProxyFactory.addAspect(Class) or addAspect(Object) with a class that has no @Aspect annotation, or with an ajc-compiled aspect while spring.aop.ajc.ignore=true is set.

Common situations: Passing a plain advice bean (implementing MethodInterceptor etc.) instead of an @Aspect class. Forgetting the @Aspect annotation. Enabling spring.aop.ajc.ignore and then trying to add a code-style (ajc) aspect programmatically.

Related errors


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