{"record":{"id":"a8b457c434886dd7","repo":"spring-projects/spring-framework","slug":"failed-to-invoke-aspect-constructor-aspectclass","errorCode":null,"errorMessage":"Failed to invoke aspect constructor: {aspectClass.getName()}","messagePattern":"Failed to invoke aspect constructor: (.+?)","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java","lineNumber":76,"sourceCode":"\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.\n\t * @see org.springframework.core.Ordered\n\t * @see #getOrderForAspectClass\n\t */\n\t@Override","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java#L58-L94","documentation":"Thrown when the aspect's no-arg constructor was found and is accessible but threw an exception when invoked (InvocationTargetException). Spring unwraps the original target exception and re-wraps it as an AopConfigException so the caller sees the real cause.","triggerScenarios":"An aspect whose no-arg constructor throws — e.g. public MyAspect() { throw new IllegalStateException(\"config not loaded\"); } or a constructor that reads from a missing resource.","commonSituations":"Aspects that do heavy initialization in the constructor (loading config files, connecting to resources), or constructor logic that fails due to missing dependencies or environment misconfiguration.","solutions":["Inspect AopConfigException.getCause() to find and fix the root cause exception thrown by the aspect constructor","Move risky initialization out of the constructor into a @PostConstruct method or lazy initialization","Ensure all resources the constructor needs (config files, env vars, system properties) are available at aspect creation time"],"exampleFix":"// before\npublic class MyAspect {\n    public MyAspect() {\n        this.config = Files.readString(Path.of(\"config.json\")); // throws if missing\n    }\n}\n\n// after\npublic class MyAspect {\n    private volatile Config config;\n    public MyAspect() { } // safe no-arg constructor\n    @PostConstruct\n    void init() { this.config = Files.readString(Path.of(\"config.json\")); }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    Object aspect = factory.getAspectInstance();\n} catch (AopConfigException ex) {\n    Throwable rootCause = ex.getCause(); // this is the original exception from the constructor\n    logger.error(\"Aspect constructor threw: \" + rootCause.getMessage(), rootCause);\n    // Fix the root cause in the aspect constructor (e.g., missing config, NPE)\n    throw ex;\n}","preventionTips":["Keep aspect constructors lightweight — avoid I/O, resource loading, or dependency access","Move complex initialization to @PostConstruct methods or lazy initialization triggered after construction","Inspect AopConfigException.getCause() to find the real exception thrown by the aspect constructor"],"tags":["spring-aop","aspectj","aspect-instantiation","constructor"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}