spring-projects/spring-framework · error · AopConfigException

Failed to invoke aspect constructor: {}

Error message

Failed to invoke aspect constructor: {}

What it means

Thrown by SimpleAspectInstanceFactory.getAspectInstance when the aspect's constructor itself throws an exception (InvocationTargetException). Spring unwraps and re-throws the original cause inside an AopConfigException. The constructor is accessible and invoked, but the aspect's own initialization logic failed.

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 e8729d0438)

Solutions

  1. Inspect the cause of the AopConfigException (ex.getCause()) to find the real failure in the constructor.
  2. Fix the underlying initialization error in the aspect constructor.
  3. Move heavy/fragrant initialization out of the constructor into a @PostConstruct or lazy init, or inject dependencies via a Spring-managed bean instead of reflective instantiation.

Example fix

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

// after
public class MyAspect {
    public MyAspect() {}
    @PostConstruct void init() {
        this.config = Files.readString(Path.of("config.properties"));
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Best prevention: keep constructors side-effect free.
// Validate any required config in @PostConstruct so constructor never throws.

Try / catch

try {
    factory.getAspectInstance();
} catch (AopConfigException ex) {
    Throwable root = ex.getCause(); // the real constructor failure
    log.error("Aspect constructor threw: {}", root.toString());
    throw ex;
}

Prevention

When it happens

Trigger: The aspect constructor throws a NullPointerException, IllegalStateException, a failed dependency lookup, resource initialization error, or any runtime exception during instantiation.

Common situations: Aspect constructor reads a config file or system property that is missing; constructor dereferences a null field; aspect relies on static state not yet initialized; constructor performs validation that fails.

Related errors


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