spring-projects/spring-framework · error · AopConfigException

No default constructor on aspect class: {}

Error message

No default constructor on aspect class: {}

What it means

Thrown by SimpleAspectInstanceFactory.getAspectInstance when ReflectionUtils.accessibleConstructor(aspectClass) reports NoSuchMethodException — the aspect class has no public no-arg constructor. SimpleAspectInstanceFactory instantiates aspects reflectively each time, requiring a default constructor.

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java:64

		Assert.notNull(aspectClass, "Aspect class must not be null");
		this.aspectClass = aspectClass;
	}


	/**
	 * Return the specified aspect class (never {@code null}).
	 */
	public final Class<?> getAspectClass() {
		return this.aspectClass;
	}

	@Override
	public final Object getAspectInstance() {
		try {
			return ReflectionUtils.accessibleConstructor(this.aspectClass).newInstance();
		}
		catch (NoSuchMethodException ex) {
			throw new AopConfigException(
					"No default constructor on aspect class: " + this.aspectClass.getName(), ex);
		}
		catch (InstantiationException ex) {
			throw new AopConfigException(
					"Unable to instantiate aspect class: " + this.aspectClass.getName(), ex);
		}
		catch (IllegalAccessException | InaccessibleObjectException ex) {
			throw new AopConfigException(
					"Could not access aspect constructor: " + this.aspectClass.getName(), ex);
		}
		catch (InvocationTargetException ex) {
			throw new AopConfigException(
					"Failed to invoke aspect constructor: " + this.aspectClass.getName(), ex.getTargetException());
		}
	}

	@Override
	public @Nullable ClassLoader getAspectClassLoader() {

View on GitHub (pinned to e8729d0438)

Solutions

  1. Add a public no-arg constructor to the aspect class.
  2. If the aspect needs dependencies, use SingletonAspectInstanceFactory / BeanFactoryAspectInstanceFactory with a Spring-managed bean instead of SimpleAspectInstanceFactory.
  3. Make nested aspect classes static so an instance can be created without an enclosing instance.

Example fix

// before
public class MyAspect {
    private final Logger logger;
    public MyAspect(Logger logger) { this.logger = logger; }
}

// after: add a no-arg constructor, or better, register as a bean
@Component
public class MyAspect {
    private final Logger logger;
    public MyAspect(Logger logger) { this.logger = logger; }
}
// and use BeanFactoryAspectInstanceFactory / @Component scanning instead of SimpleAspectInstanceFactory
Defensive patterns

Strategy: validation

Validate before calling

try {
    aspectClass.getDeclaredConstructor();
} catch (NoSuchMethodException ex) {
    throw new IllegalStateException("Aspect needs a public no-arg constructor: " + aspectClass);
}

Type guard

boolean hasDefaultCtor = java.lang.reflect.Modifier.isPublic(
    aspectClass.getDeclaredConstructor().getModifiers());

Prevention

When it happens

Trigger: Registering an aspect class via SimpleAspectInstanceFactory (or a config that uses it) where the class lacks a public no-arg constructor: it has only parameterized constructors, a private default constructor, or is an inner non-static class.

Common situations: Aspect class with constructor injection (no longer a simple aspect); aspect declared as a non-static nested class; aspect with only a private constructor; mistakenly registering a singleton bean-backed aspect with the simple factory instead of BeanFactoryAspectInstanceFactory.

Related errors


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