{"record":{"id":"072cdbd0dc3cdcaa","repo":"spring-projects/spring-framework","slug":"no-default-constructor-on-aspect-class-aspectcla","errorCode":null,"errorMessage":"No default constructor on aspect class: {aspectClass.getName()}","messagePattern":"No default constructor on aspect class: (.+?)","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java","lineNumber":64,"sourceCode":"\t\tAssert.notNull(aspectClass, \"Aspect class must not be null\");\n\t\tthis.aspectClass = aspectClass;\n\t}\n\n\n\t/**\n\t * Return the specified aspect class (never {@code null}).\n\t */\n\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() {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java#L46-L82","documentation":"Thrown by SimpleAspectInstanceFactory.getAspectInstance() when ReflectionUtils.accessibleConstructor() cannot find a no-arg constructor on the aspect class. This factory creates a fresh aspect instance per call via reflection, which requires a default (public or accessible) constructor.","triggerScenarios":"Passing an aspect class to SimpleAspectInstanceFactory that only has parameterized constructors — e.g. new SimpleAspectInstanceFactory(MyAspect.class) where MyAspect has no MyAspect() constructor.","commonSituations":"Aspects designed for dependency injection (with @Autowired fields) that only have a parameterized constructor, or aspect classes intended for compile-time AspectJ weaving (not Spring instantiation).","solutions":["Add a public no-arg constructor to the aspect class","If the aspect needs dependencies, use SingletonAspectInstanceFactory or BeanFactoryAspectInstanceFactory instead, which use a Spring-managed bean","Register the aspect as a Spring @Component/@Bean and reference it rather than having Spring instantiate it per-invocation"],"exampleFix":"// before\npublic class MyAspect {\n    public MyAspect(Dependency dep) { this.dep = dep; } // no no-arg constructor\n}\nnew SimpleAspectInstanceFactory(MyAspect.class); // throws\n\n// after\npublic class MyAspect {\n    public MyAspect() { }\n}\nnew SimpleAspectInstanceFactory(MyAspect.class);","handlingStrategy":"validation","validationCode":"// Validate the aspect class has a no-arg constructor before creating the factory\ntry {\n    aspectClass.getDeclaredConstructor();\n} catch (NoSuchMethodException ex) {\n    throw new IllegalStateException(\n        \"Aspect class \" + aspectClass.getName() + \" has no no-arg constructor; \" +\n        \"add one or use BeanFactoryAspectInstanceFactory\");\n}\nnew SimpleAspectInstanceFactory(aspectClass);","typeGuard":null,"tryCatchPattern":"try {\n    factory.getAspectInstance();\n} catch (AopConfigException ex) {\n    if (ex.getMessage().contains(\"No default constructor\")) {\n        // Add a no-arg constructor or switch to BeanFactoryAspectInstanceFactory\n    }\n    throw ex;\n}","preventionTips":["Ensure every aspect class used with SimpleAspectInstanceFactory has a public no-arg constructor","For aspects needing dependencies, use SingletonAspectInstanceFactory or BeanFactoryAspectInstanceFactory with a Spring-managed bean"],"tags":["spring-aop","aspectj","aspect-instantiation","configuration"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}