{"id":"2665cf993b7ddb8e","repo":"spring-projects/spring-framework","slug":"failed-to-invoke-aspect-constructor","errorCode":null,"errorMessage":"Failed to invoke aspect constructor: {}","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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java#L58-L94","documentation":"Thrown by SimpleAspectInstanceFactory.getAspectInstance when the aspect's constructor itself throws an exception (InvocationTargetException). Spring unwraps and re-throws the original cause inside an AopConfigException. The constructor is accessible and invoked, but the aspect's own initialization logic failed.","triggerScenarios":"The aspect constructor throws a NullPointerException, IllegalStateException, a failed dependency lookup, resource initialization error, or any runtime exception during instantiation.","commonSituations":"Aspect constructor reads a config file or system property that is missing; constructor dereferences a null field; aspect relies on static state not yet initialized; constructor performs validation that fails.","solutions":["Inspect the cause of the AopConfigException (ex.getCause()) to find the real failure in the constructor.","Fix the underlying initialization error in the aspect constructor.","Move heavy/fragrant initialization out of the constructor into a @PostConstruct or lazy init, or inject dependencies via a Spring-managed bean instead of reflective instantiation."],"exampleFix":"// before\npublic class MyAspect {\n    public MyAspect() {\n        this.config = Files.readString(Path.of(\"missing.properties\")); // throws\n    }\n}\n\n// after\npublic class MyAspect {\n    public MyAspect() {}\n    @PostConstruct void init() {\n        this.config = Files.readString(Path.of(\"config.properties\"));\n    }\n}","handlingStrategy":"try-catch","validationCode":"// Best prevention: keep constructors side-effect free.\n// Validate any required config in @PostConstruct so constructor never throws.","typeGuard":null,"tryCatchPattern":"try {\n    factory.getAspectInstance();\n} catch (AopConfigException ex) {\n    Throwable root = ex.getCause(); // the real constructor failure\n    log.error(\"Aspect constructor threw: {}\", root.toString());\n    throw ex;\n}","preventionTips":["Keep aspect constructors free of logic that can fail; use @PostConstruct for initialization.","Inspect getCause() of AopConfigException to find the real error.","Prefer Spring-managed aspect beans so the container handles lifecycle."],"tags":["spring-aop","aspectj","aspect-instantiation","reflection","runtime"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}