{"record":{"id":"3cd52041d0874e21","repo":"spring-projects/spring-framework","slug":"could-not-access-aspect-constructor-aspectclass","errorCode":null,"errorMessage":"Could not access aspect constructor: {aspectClass.getName()}","messagePattern":"Could not access aspect constructor: (.+?)","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java","lineNumber":72,"sourceCode":"\tpublic final Class<?> getAspectClass() {\n\t\treturn this.aspectClass;\n\t}\n\n\t@Override\n\tpublic final Object getAspectInstance() {\n\t\ttry {\n\t\t\treturn ReflectionUtils.accessibleConstructor(this.aspectClass).newInstance();\n\t\t}\n\t\tcatch (NoSuchMethodException ex) {\n\t\t\tthrow new AopConfigException(\n\t\t\t\t\t\"No default constructor on aspect class: \" + this.aspectClass.getName(), ex);\n\t\t}\n\t\tcatch (InstantiationException ex) {\n\t\t\tthrow new AopConfigException(\n\t\t\t\t\t\"Unable to instantiate aspect class: \" + this.aspectClass.getName(), ex);\n\t\t}\n\t\tcatch (IllegalAccessException | InaccessibleObjectException ex) {\n\t\t\tthrow new AopConfigException(\n\t\t\t\t\t\"Could not access aspect constructor: \" + this.aspectClass.getName(), ex);\n\t\t}\n\t\tcatch (InvocationTargetException ex) {\n\t\t\tthrow new AopConfigException(\n\t\t\t\t\t\"Failed to invoke aspect constructor: \" + this.aspectClass.getName(), ex.getTargetException());\n\t\t}\n\t}\n\n\t@Override\n\tpublic @Nullable ClassLoader getAspectClassLoader() {\n\t\treturn this.aspectClass.getClassLoader();\n\t}\n\n\t/**\n\t * Determine the order for this factory's aspect instance,\n\t * either an instance-specific order expressed through implementing\n\t * the {@link org.springframework.core.Ordered} interface,\n\t * or a fallback order.","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java#L54-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Make the aspect's no-arg constructor public","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)","Move the aspect to an open package or make it a Spring-managed bean"],"exampleFix":"// before\npublic class MyAspect {\n    private MyAspect() { } // not accessible\n}\n\n// after\npublic class MyAspect {\n    public MyAspect() { }\n}","handlingStrategy":"validation","validationCode":"// Validate the no-arg constructor is accessible\ntry {\n    Constructor<?> ctor = aspectClass.getDeclaredConstructor();\n    if (!Modifier.isPublic(ctor.getModifiers()) && !ctor.canAccess(null)) {\n        // Spring will try setAccessible(true), but module restrictions may still block it\n        logger.warn(\"Aspect constructor is non-public; may fail under JPMS restrictions\");\n    }\n} catch (NoSuchMethodException ex) {\n    // handled by error 36\n}","typeGuard":null,"tryCatchPattern":"try {\n    factory.getAspectInstance();\n} catch (AopConfigException ex) {\n    if (ex.getCause() instanceof InaccessibleObjectException) {\n        // Add --add-opens to the JVM args, or make the constructor public\n    } else if (ex.getMessage().contains(\"Could not access\")) {\n        // Make the constructor public\n    }\n    throw ex;\n}","preventionTips":["Make the aspect's no-arg constructor public","Under JPMS, add --add-opens <module>/<package>=ALL-UNNAMED or declare the package as open in module-info.java","Prefer placing aspects in the unnamed module or an open module to avoid reflection restrictions"],"tags":["spring-aop","aspectj","aspect-instantiation","module-system"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}