spring-projects/spring-framework · error · AopConfigException

Unable to instantiate aspect class: {aspectClass.getName()}

Error message

Unable to instantiate aspect class: {aspectClass.getName()}

What it means

Thrown when accessibleConstructor().newInstance() raises InstantiationException — the aspect class is abstract, an interface, an array class, a primitive type, or void. These types cannot be instantiated at all, so the factory cannot produce an aspect instance.

Source

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

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

	/**

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. Pass a concrete (non-abstract, non-interface) aspect class
  2. Verify the class is a real instantiable class — not an interface, array, primitive, or abstract type
  3. Choose an AspectInstanceFactory implementation appropriate for the aspect's lifecycle

Example fix

// before
new SimpleAspectInstanceFactory(AbstractBaseAspect.class); // throws: abstract

// after
new SimpleAspectInstanceFactory(ConcreteAspect.class); // concrete subclass
Defensive patterns

Strategy: validation

Validate before calling

// Validate the class is concrete and instantiable
int mods = aspectClass.getModifiers();
if (Modifier.isAbstract(mods) || Modifier.isInterface(mods) || aspectClass.isArray() || aspectClass.isPrimitive()) {
    throw new IllegalStateException(
        "Cannot instantiate " + aspectClass.getName() + ": it is abstract, an interface, or a non-class type");
}
new SimpleAspectInstanceFactory(aspectClass);

Try / catch

try {
    factory.getAspectInstance();
} catch (AopConfigException ex) {
    if (ex.getMessage().contains("Unable to instantiate")) {
        // Use a concrete subclass instead
    }
    throw ex;
}

Prevention

When it happens

Trigger: Passing AbstractAspect.class, an interface, or a primitive/array class to SimpleAspectInstanceFactory — e.g. new SimpleAspectInstanceFactory(AbstractBaseAspect.class).

Common situations: Accidentally pointing the factory at an abstract base aspect class, or passing the wrong Class reference (e.g., an interface type instead of the concrete implementation).

Related errors


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