spring-projects/spring-framework · error · AopConfigException

No default constructor on aspect class: {aspectClass.getName

Error message

No default constructor on aspect class: {aspectClass.getName()}

What it means

Thrown by SimpleAspectInstanceFactory.getAspectInstance() when ReflectionUtils.accessibleConstructor() cannot find a no-arg constructor on the aspect class. This factory creates a fresh aspect instance per call via reflection, which requires a default (public or accessible) 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 69bf83ad71)

Solutions

  1. Add a public no-arg constructor to the aspect class
  2. If the aspect needs dependencies, use SingletonAspectInstanceFactory or BeanFactoryAspectInstanceFactory instead, which use a Spring-managed bean
  3. Register the aspect as a Spring @Component/@Bean and reference it rather than having Spring instantiate it per-invocation

Example fix

// before
public class MyAspect {
    public MyAspect(Dependency dep) { this.dep = dep; } // no no-arg constructor
}
new SimpleAspectInstanceFactory(MyAspect.class); // throws

// after
public class MyAspect {
    public MyAspect() { }
}
new SimpleAspectInstanceFactory(MyAspect.class);
Defensive patterns

Strategy: validation

Validate before calling

// Validate the aspect class has a no-arg constructor before creating the factory
try {
    aspectClass.getDeclaredConstructor();
} catch (NoSuchMethodException ex) {
    throw new IllegalStateException(
        "Aspect class " + aspectClass.getName() + " has no no-arg constructor; " +
        "add one or use BeanFactoryAspectInstanceFactory");
}
new SimpleAspectInstanceFactory(aspectClass);

Try / catch

try {
    factory.getAspectInstance();
} catch (AopConfigException ex) {
    if (ex.getMessage().contains("No default constructor")) {
        // Add a no-arg constructor or switch to BeanFactoryAspectInstanceFactory
    }
    throw ex;
}

Prevention

When it happens

Trigger: Passing an aspect class to SimpleAspectInstanceFactory that only has parameterized constructors — e.g. new SimpleAspectInstanceFactory(MyAspect.class) where MyAspect has no MyAspect() constructor.

Common situations: Aspects designed for dependency injection (with @Autowired fields) that only have a parameterized constructor, or aspect classes intended for compile-time AspectJ weaving (not Spring instantiation).

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/072cdbd0dc3cdcaa. Report an issue: GitHub.