spring-projects/spring-framework · error · AopConfigException

Could not access aspect constructor: {aspectClass.getName()}

Error message

Could not access aspect constructor: {aspectClass.getName()}

What it means

Thrown when the aspect's no-arg constructor exists but is not accessible — it is private or package-private (and the caller is in a different package), or the Java Platform Module System raises InaccessibleObjectException even though Spring attempts setAccessible(true). This is an access control failure.

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 69bf83ad71)

Solutions

  1. Make the aspect's no-arg constructor public
  2. If using Java modules, add --add-opens to open the aspect's package to Spring (or declare the package as open in module-info.java)
  3. Move the aspect to an open package or make it a Spring-managed bean

Example fix

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

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

Strategy: validation

Validate before calling

// Validate the no-arg constructor is accessible
try {
    Constructor<?> ctor = aspectClass.getDeclaredConstructor();
    if (!Modifier.isPublic(ctor.getModifiers()) && !ctor.canAccess(null)) {
        // Spring will try setAccessible(true), but module restrictions may still block it
        logger.warn("Aspect constructor is non-public; may fail under JPMS restrictions");
    }
} catch (NoSuchMethodException ex) {
    // handled by error 36
}

Try / catch

try {
    factory.getAspectInstance();
} catch (AopConfigException ex) {
    if (ex.getCause() instanceof InaccessibleObjectException) {
        // Add --add-opens to the JVM args, or make the constructor public
    } else if (ex.getMessage().contains("Could not access")) {
        // Make the constructor public
    }
    throw ex;
}

Prevention

When it happens

Trigger: An aspect class with a private or package-private no-arg constructor, or an aspect in a named Java module that does not open its package to Spring's reflection.

Common situations: Java 9+ module system restrictions where the aspect's module does not open the package, aspect classes in restricted libraries, or accidentally non-public constructors.

Related errors


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