spring-projects/spring-framework · error · AopConfigException

Could not access aspect constructor: {}

Error message

Could not access aspect constructor: {}

What it means

Thrown by SimpleAspectInstanceFactory.getAspectInstance when accessing the constructor is denied — IllegalAccessException or InaccessibleObjectException. The constructor exists but is not accessible: it is private/protected and the JVM module system (JPMS) blocks reflective access even after setAccessible(true).

Source

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

	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();
	}

	/**
	 * 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.

View on GitHub (pinned to e8729d0438)

Solutions

  1. Make the aspect's constructor public.
  2. If on JDK 16+, add the required --add-opens JVM argument for the aspect's package/module, or use a Spring-managed bean (BeanFactoryAspectInstanceFactory) which bypasses the issue.
  3. Register the aspect as a Spring @Component bean so Spring creates it via the container rather than reflectively.

Example fix

// before
public class MyAspect {
    private MyAspect() {}
}

// after
public class MyAspect {
    public MyAspect() {}
}
Defensive patterns

Strategy: validation

Validate before calling

Constructor<?> ctor = aspectClass.getDeclaredConstructor();
if (!Modifier.isPublic(ctor.getModifiers())) {
    throw new IllegalStateException("Aspect constructor must be public: " + aspectClass);
}

Type guard

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

Prevention

When it happens

Trigger: An aspect class whose only constructor is non-public and the runtime refuses reflective access; running on a recent JDK with strong encapsulation where the aspect is in an unnamed or exported module that disallows deep reflection; --illegal-access deny.

Common situations: Aspect with a private constructor; JDK 16+ without --add-opens for the aspect's module; library aspects shipped without open packages.

Related errors


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