{"id":"4fb60eb14d954965","repo":"spring-projects/spring-framework","slug":"could-not-access-aspect-constructor","errorCode":null,"errorMessage":"Could not access aspect constructor: {}","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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java#L54-L90","documentation":"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).","triggerScenarios":"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.","commonSituations":"Aspect with a private constructor; JDK 16+ without --add-opens for the aspect's module; library aspects shipped without open packages.","solutions":["Make the aspect's constructor public.","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.","Register the aspect as a Spring @Component bean so Spring creates it via the container rather than reflectively."],"exampleFix":"// before\npublic class MyAspect {\n    private MyAspect() {}\n}\n\n// after\npublic class MyAspect {\n    public MyAspect() {}\n}","handlingStrategy":"validation","validationCode":"Constructor<?> ctor = aspectClass.getDeclaredConstructor();\nif (!Modifier.isPublic(ctor.getModifiers())) {\n    throw new IllegalStateException(\"Aspect constructor must be public: \" + aspectClass);\n}","typeGuard":"boolean accessible = java.lang.reflect.Modifier.isPublic(\n    aspectClass.getDeclaredConstructor().getModifiers());","tryCatchPattern":null,"preventionTips":["Make aspect constructors public.","On JDK 16+, add --add-opens for the aspect's package if it must stay non-public.","Prefer Spring-managed aspect beans to avoid reflective access issues."],"tags":["spring-aop","aspectj","aspect-instantiation","reflection","jpms"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}