{"id":"caa807da5f173820","repo":"spring-projects/spring-framework","slug":"no-default-constructor-on-aspect-class","errorCode":null,"errorMessage":"No default constructor on aspect class: {}","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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java#L46-L82","documentation":"Thrown by SimpleAspectInstanceFactory.getAspectInstance when ReflectionUtils.accessibleConstructor(aspectClass) reports NoSuchMethodException — the aspect class has no public no-arg constructor. SimpleAspectInstanceFactory instantiates aspects reflectively each time, requiring a default constructor.","triggerScenarios":"Registering an aspect class via SimpleAspectInstanceFactory (or a config that uses it) where the class lacks a public no-arg constructor: it has only parameterized constructors, a private default constructor, or is an inner non-static class.","commonSituations":"Aspect class with constructor injection (no longer a simple aspect); aspect declared as a non-static nested class; aspect with only a private constructor; mistakenly registering a singleton bean-backed aspect with the simple factory instead of BeanFactoryAspectInstanceFactory.","solutions":["Add a public no-arg constructor to the aspect class.","If the aspect needs dependencies, use SingletonAspectInstanceFactory / BeanFactoryAspectInstanceFactory with a Spring-managed bean instead of SimpleAspectInstanceFactory.","Make nested aspect classes static so an instance can be created without an enclosing instance."],"exampleFix":"// before\npublic class MyAspect {\n    private final Logger logger;\n    public MyAspect(Logger logger) { this.logger = logger; }\n}\n\n// after: add a no-arg constructor, or better, register as a bean\n@Component\npublic class MyAspect {\n    private final Logger logger;\n    public MyAspect(Logger logger) { this.logger = logger; }\n}\n// and use BeanFactoryAspectInstanceFactory / @Component scanning instead of SimpleAspectInstanceFactory","handlingStrategy":"validation","validationCode":"try {\n    aspectClass.getDeclaredConstructor();\n} catch (NoSuchMethodException ex) {\n    throw new IllegalStateException(\"Aspect needs a public no-arg constructor: \" + aspectClass);\n}","typeGuard":"boolean hasDefaultCtor = java.lang.reflect.Modifier.isPublic(\n    aspectClass.getDeclaredConstructor().getModifiers());","tryCatchPattern":null,"preventionTips":["Ensure every aspect registered with SimpleAspectInstanceFactory has a public no-arg constructor.","For aspects needing dependencies, use BeanFactoryAspectInstanceFactory with a Spring bean.","Make nested aspect classes static."],"tags":["spring-aop","aspectj","aspect-instantiation","reflection"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}