spring-projects/spring-framework · error · AopConfigException

Failed to invoke aspect constructor: {aspectClass.getName()}

Error message

Failed to invoke aspect constructor: {aspectClass.getName()}

What it means

Thrown when the aspect's no-arg constructor was found and is accessible but threw an exception when invoked (InvocationTargetException). Spring unwraps the original target exception and re-wraps it as an AopConfigException so the caller sees the real cause.

Source

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

	@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() {
		return this.aspectClass.getClassLoader();
	}

	/**
	 * Determine the order for this factory's aspect instance,
	 * either an instance-specific order expressed through implementing
	 * the {@link org.springframework.core.Ordered} interface,
	 * or a fallback order.
	 * @see org.springframework.core.Ordered
	 * @see #getOrderForAspectClass
	 */
	@Override

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. Inspect AopConfigException.getCause() to find and fix the root cause exception thrown by the aspect constructor
  2. Move risky initialization out of the constructor into a @PostConstruct method or lazy initialization
  3. Ensure all resources the constructor needs (config files, env vars, system properties) are available at aspect creation time

Example fix

// before
public class MyAspect {
    public MyAspect() {
        this.config = Files.readString(Path.of("config.json")); // throws if missing
    }
}

// after
public class MyAspect {
    private volatile Config config;
    public MyAspect() { } // safe no-arg constructor
    @PostConstruct
    void init() { this.config = Files.readString(Path.of("config.json")); }
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    Object aspect = factory.getAspectInstance();
} catch (AopConfigException ex) {
    Throwable rootCause = ex.getCause(); // this is the original exception from the constructor
    logger.error("Aspect constructor threw: " + rootCause.getMessage(), rootCause);
    // Fix the root cause in the aspect constructor (e.g., missing config, NPE)
    throw ex;
}

Prevention

When it happens

Trigger: An aspect whose no-arg constructor throws — e.g. public MyAspect() { throw new IllegalStateException("config not loaded"); } or a constructor that reads from a missing resource.

Common situations: Aspects that do heavy initialization in the constructor (loading config files, connecting to resources), or constructor logic that fails due to missing dependencies or environment misconfiguration.

Related errors


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