{"record":{"id":"8bbf33fbc91329be","repo":"spring-projects/spring-framework","slug":"spring-aop-cannot-handle-constructor-advice","errorCode":null,"errorMessage":"Spring AOP cannot handle constructor advice","messagePattern":"Spring AOP cannot handle constructor advice","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java","lineNumber":267,"sourceCode":"\t\t\t\treturn null;\n\t\t\t}\n\t\t\tStringTokenizer nameTokens = new StringTokenizer(annotation.getArgumentNames(), \",\");\n\t\t\tint numTokens = nameTokens.countTokens();\n\t\t\tif (numTokens > 0) {\n\t\t\t\tString[] names = new String[numTokens];\n\t\t\t\tfor (int i = 0; i < names.length; i++) {\n\t\t\t\t\tnames[i] = nameTokens.nextToken();\n\t\t\t\t}\n\t\t\t\treturn names;\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\n\t\t@Override\n\t\tpublic @Nullable String @Nullable [] getParameterNames(Constructor<?> ctor) {\n\t\t\tthrow new UnsupportedOperationException(\"Spring AOP cannot handle constructor advice\");\n\t\t}\n\t}\n\n}\n","sourceCodeStart":249,"sourceCodeEnd":272,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java#L249-L272","documentation":"Thrown as an UnsupportedOperationException from AspectJAnnotationParameterNameDiscoverer.getParameterNames(Constructor). Spring AOP only supports method-level advice (@Before, @After, @Around, etc.); AspectJ also allows constructor execution/initialization advice, but Spring's proxy-based AOP cannot intercept constructors. This is triggered if the parameter-name discovery path is exercised for a constructor, which does not occur during normal Spring AOP processing.","triggerScenarios":"A custom ParameterNameDiscoverer wiring that calls getParameterNames(Constructor) on the AspectJAnnotationParameterNameDiscoverer; framework/test code invoking parameter discovery on a constructor of an advice type; programmatic misuse of the internal discoverer.","commonSituations":"Tooling or extensions that reuse Spring's ParameterNameDiscoverer infrastructure on constructors; misconfiguration where a generic discoverer chain is applied to constructors; user attempting to define constructor advice via Spring AOP (not supported).","solutions":["Do not attempt constructor-level advice through Spring AOP; use method-level advice instead.","Avoid calling getParameterNames(Constructor) on the AspectJ annotation parameter discoverer; use a different discoverer for constructors.","If constructor interception is required, use AspectJ compile-time or load-time weaving."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Spring AOP never invokes constructor parameter discovery through this path in normal use.\n// Defense: do not call AspectJAnnotationParameterNameDiscoverer.getParameterNames(Constructor).\n// Use a different ParameterNameDiscoverer (e.g., DefaultParameterNameDiscoverer) for constructors.\n","typeGuard":null,"tryCatchPattern":"// Not applicable in user code; if extending the framework, guard before calling:\nParameterNameDiscoverer discoverer = ...;\nif (executable instanceof Method mth) {\n    return discoverer.getParameterNames(mth); // safe for AspectJ discoverer\n} else {\n    return otherDiscoverer.getParameterNames((Constructor<?>) executable);\n}","preventionTips":["Do not attempt constructor advice in Spring AOP; it is unsupported by design.","Use AspectJ weaving if constructor interception is required.","Avoid reusing the AspectJ annotation parameter discoverer for constructor analysis."],"tags":["spring-aop","aspectj","constructor-advice","unsupported"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}